Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/393.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring RestTemplate在使用授权标头发送时出现401未经授权的错误_Java_Spring_Rest_Resttemplate_Spring Restcontroller - Fatal编程技术网

Java Spring RestTemplate在使用授权标头发送时出现401未经授权的错误

Java Spring RestTemplate在使用授权标头发送时出现401未经授权的错误,java,spring,rest,resttemplate,spring-restcontroller,Java,Spring,Rest,Resttemplate,Spring Restcontroller,我试图用下面的代码在我的SpringREST应用程序中点击一个MicrosoftFlowPostURL,但它给了我401错误。 我的代码: 是因为我的目标url是https,而我的本地主机是http吗 有人能告诉我出了什么问题吗?有两点可供您检查此问题: 如果目标服务器是http服务器,则不应在url中使用https 确保authorization值为base64编码 更新: 由于目标服务器是https服务器,问题在于您没有将sslinfo配置到RestTempalte中。您可以参考以下代码段以

我试图用下面的代码在我的SpringREST应用程序中点击一个MicrosoftFlowPostURL,但它给了我401错误。 我的代码:

是因为我的目标url是https,而我的本地主机是http吗


有人能告诉我出了什么问题吗?

有两点可供您检查此问题:

  • 如果目标服务器是http服务器,则不应在url中使用https
  • 确保
    authorization
    值为
    base64
    编码
  • 更新:

    由于目标服务器是https服务器,问题在于您没有将
    ssl
    info配置到
    RestTempalte
    中。您可以参考以下代码段以获取ssl restTemplate:

    @Configuration
    public class RestClientConfig {
        @Bean
        public RestOperations restOperations(ClientHttpRequestFactory clientHttpRequestFactory) throws Exception {
            return new RestTemplate(clientHttpRequestFactory);
        }
    
        @Bean
        public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) {
            return new HttpComponentsClientHttpRequestFactory(httpClient);
        }
    
        @Bean
        public HttpClient httpClient(@Value("${keystore.file}") String file, @Value("${keystore.pass}") String password) throws Exception {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream inStream = new FileInputStream(file);
            try {
                trustStore.load(inStream, password.toCharArray());
            } finally {
                inStream.close();
            }
    
            SSLContext sslcontext =
                    SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
            SSLConnectionSocketFactory sslsf =
                    new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1.2"}, null,
                                                   null);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        }
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }
    
    测试用例:

    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = RestClientConfig.class)
    public class RestClientTest {
    
        @Autowired
        private RestOperations rest;
    
        private HttpHeaders getHeaders(){
            String plainCredentials="admin:admin";
            String base64Credentials = Base64.getEncoder().encodeToString(plainCredentials.getBytes());
    
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Basic " + base64Credentials);
            return headers;
        }
    
        @Test
        public void test() {
            HttpEntity<String> request = new HttpEntity<String>(getHeaders());
            ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET, request, String.class);
            System.out.println(response.getBody());
        }
    }
    
    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes=RestClientConfig.class)
    公共类RestClientTest{
    @自动连线
    私人休息;
    私有HttpHeaders getHeaders(){
    String plainCredentials=“admin:admin”;
    字符串base64Credentials=Base64.getEncoder().encodeToString(plainCredentials.getBytes());
    HttpHeaders=新的HttpHeaders();
    添加(“授权”、“基本”+base64凭证);
    返回标题;
    }
    @试验
    公开无效测试(){
    HttpEntity请求=新的HttpEntity(getHeaders());
    ResponseEntity response=rest.exchange(url,HttpMethod.GET,request,String.class);
    System.out.println(response.getBody());
    }
    }
    
    在哪一点引发异常?在RestTemplate中或在输入方法之前?在“RestTemplate.exchange”处,它是Microsoft Flow的REST URL,是https,授权中也包含base64编码值。仍然存在相同的问题…现在我遇到此错误,我不知道任何SSL操作或技巧,请帮助<代码>严重:路径为[/inventoryhooks]的上下文中Servlet[webhooks]的Servlet.service()引发异常[请求处理失败;嵌套异常为org.springframework.web.client.ResourceAccessException:请求发布时发生I/O错误”https://prod-03.centralindia.logic.azure.com/workflows/a":sun.security.validator.validator异常:PKIX路径生成失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径
    @Configuration
    public class RestClientConfig {
        @Bean
        public RestOperations restOperations(ClientHttpRequestFactory clientHttpRequestFactory) throws Exception {
            return new RestTemplate(clientHttpRequestFactory);
        }
    
        @Bean
        public HttpComponentsClientHttpRequestFactory clientHttpRequestFactory(HttpClient httpClient) {
            return new HttpComponentsClientHttpRequestFactory(httpClient);
        }
    
        @Bean
        public HttpClient httpClient(@Value("${keystore.file}") String file, @Value("${keystore.pass}") String password) throws Exception {
            KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
            FileInputStream inStream = new FileInputStream(file);
            try {
                trustStore.load(inStream, password.toCharArray());
            } finally {
                inStream.close();
            }
    
            SSLContext sslcontext =
                    SSLContexts.custom().loadTrustMaterial(trustStore, new TrustSelfSignedStrategy()).build();
            SSLConnectionSocketFactory sslsf =
                    new SSLConnectionSocketFactory(sslcontext, new String[]{"TLSv1.2"}, null,
                                                   null);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        }
    
        @Bean
        public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            return new PropertySourcesPlaceholderConfigurer();
        }
    }
    
    @RunWith(SpringRunner.class)
    @ContextConfiguration(classes = RestClientConfig.class)
    public class RestClientTest {
    
        @Autowired
        private RestOperations rest;
    
        private HttpHeaders getHeaders(){
            String plainCredentials="admin:admin";
            String base64Credentials = Base64.getEncoder().encodeToString(plainCredentials.getBytes());
    
            HttpHeaders headers = new HttpHeaders();
            headers.add("Authorization", "Basic " + base64Credentials);
            return headers;
        }
    
        @Test
        public void test() {
            HttpEntity<String> request = new HttpEntity<String>(getHeaders());
            ResponseEntity<String> response = rest.exchange(url, HttpMethod.GET, request, String.class);
            System.out.println(response.getBody());
        }
    }