Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/13.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
Spring RestTemplate接收到;401“未经授权”;_Spring_Spring Boot_Resttemplate_Spring Rest - Fatal编程技术网

Spring RestTemplate接收到;401“未经授权”;

Spring RestTemplate接收到;401“未经授权”;,spring,spring-boot,resttemplate,spring-rest,Spring,Spring Boot,Resttemplate,Spring Rest,我在Spring 4中使用以下内容通过RestTemplate检索JSON: protected DocInfoResponse retrieveData(String urlWithAuth) { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add("Authorization", "Basic " + auth.getS

我在Spring 4中使用以下内容通过RestTemplate检索JSON:

protected DocInfoResponse retrieveData(String urlWithAuth) {
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + auth.getSig());
    HttpEntity<String> request = new HttpEntity<String>(headers);
    ResponseEntity<DocInfoResponse> response = restTemplate.exchange(urlWithAuth, HttpMethod.GET, request, DocInfoResponse.class);
    return response.getBody();
}

有人能告诉我为什么会收到异常吗?

在调查了我自己的问题后,我意识到FireFox RESTClient是成功的,因为我连接到了目标URL。我认为我使用的基本Auth毕竟不是那么基本

最后,我读了我试图连接的应用程序的文档,意识到他们提出了一种连接令牌机制。现在它起作用了

在阅读了您的代码之后,我说它看起来很好,尽管我不确定您调用的对象是什么
auth

第一件事:尝试从任何客户端访问您的服务,如web浏览器、邮递员或RESTClient。确保您在未连接到应用程序的情况下成功检索信息


根据结果,我建议您尝试手动加密您的
授权
令牌(您可以很容易地在该网站上找到帖子,向您展示如何进行加密),或者尝试其他连接机制。

我发现我最初在上面发布的问题是由于
auth
参数上发生双重加密。我通过使用
UriComponentsBuilder
并在
exchange()
上显式调用
encode()
解决了这个问题

(auth.appendAuth()在
urlString
中添加目标服务所需的其他
.queryParams()


执行此操作的调用是
retrieveData(buildUrl(urlString))

对于基本身份验证,创建授权标头的过程相对简单,因此几乎可以通过几行代码手动完成:

 HttpHeaders createHeaders(String username, String password){
   return new HttpHeaders() {{
         String auth = username + ":" + password;
         byte[] encodedAuth = Base64.encodeBase64( 
            auth.getBytes(Charset.forName("US-ASCII")) );
         String authHeader = "Basic " + new String( encodedAuth );
         set( "Authorization", authHeader );
      }};
}
然后,发送请求变得同样简单:

RestTemplate restTemplate = new RestTemplate();    
restTemplate.exchange
     (uri, HttpMethod.POST, new HttpEntity<T>(createHeaders(username, password)), clazz);
RestTemplate RestTemplate=new RestTemplate();
restTemplate.exchange
(uri,HttpMethod.POST,新的HttpEntity(createHeaders(用户名、密码)),clazz);

您是否尝试过通过浏览器或邮递员访问相同的内容?它是否与此身份验证一起工作?是的,我已获得相同的URL以在浏览器中成功返回预期结果。这肯定是一个身份验证问题。您正在生成用于身份验证的数字签名吗?签名URL是否特定?正在生成数字签名。这在'auth.getSig()'调用中。站点文档中没有指定它是特定于URL的。在这两种情况下(有效的URL和无效的URL)都使用相同的签名/算法。您可以共享另一种有效的方法吗?在我的情况下,授权字符串被双重加密。我通过做一些类似于你建议的手动加密的事情来解决这个问题。。。使用“UriComponentsBuilder”。很高兴这有帮助!你的问题也很有帮助:)
UriComponentsBuilder buildUrl(String urlString) {
    UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl(urlString);

    return auth.appendAuth(builder);
}
 HttpHeaders createHeaders(String username, String password){
   return new HttpHeaders() {{
         String auth = username + ":" + password;
         byte[] encodedAuth = Base64.encodeBase64( 
            auth.getBytes(Charset.forName("US-ASCII")) );
         String authHeader = "Basic " + new String( encodedAuth );
         set( "Authorization", authHeader );
      }};
}
RestTemplate restTemplate = new RestTemplate();    
restTemplate.exchange
     (uri, HttpMethod.POST, new HttpEntity<T>(createHeaders(username, password)), clazz);