【问题标题】:org.springframework.web.client.HttpClientErrorException: 401 Unauthorizedorg.springframework.web.client.HttpClientErrorException: 401 未经授权
【发布时间】:2017-02-22 20:00:32
【问题描述】:

我有网络服务网址:

http://myservice.local/aprovalanduser/?format=json&Name=India

当我使用

调用此 URL 时
resttemplate httpsrestTemplate.getForObject(uri, userdetails[].class)

我收到错误:

org.springframework.web.client.HttpClientErrorException: 401 Unauthorized

在网络服务方法中:

method: "GET", 
data: xmlData, 
contentType: "application/xml", 
dataType: "xml", 
async: true, 
crossDomain: false,

我只为 XML 设置标题,如下所示:

headers.setContentType(MediaType.APPLICATION_XML);

【问题讨论】:

    标签: spring spring-security spring-boot


    【解决方案1】:

    Http 状态码 401 表示您需要提供凭据才能访问该服务。您提供凭据的方式取决于服务使用的身份验证机制

    例如,如果它使用基本身份验证,那么您需要添加一个带有基本前缀的授权请求标头和一个 base64 编码的用户名和密码组合,由:

    【讨论】:

    • 这正是我想要的。指向页面/文档的链接以及如何执行此操作的示例会很有帮助
    【解决方案2】:

    这是@ekem chitsiga 建议的基本身份验证代码

    String plainCreds = "username:password";
    byte[] plainCredsBytes = plainCreds.getBytes();
    byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
    String base64Creds = new String(base64CredsBytes);
    
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Basic " + base64Creds);
    HttpEntity<String> request = new HttpEntity<String>(headers);
    
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://myservice.local/aprovalanduser/?format=json&Name=India";
    ResponseEntity<Object> response = restTemplate.exchange(url, HttpMethod.GET, request, Object.class);
    response.getBody();
    

    【讨论】:

      【解决方案3】:
      String plainCreds_usuario = Constants.CREDENCIAL_REST_API_ODATA_USUARIO;
              String plainCreds_password = Constants.CREDENCIAL_REST_API_ODATA_PASSWORD;
              
              String plainCreds = plainCreds_usuario+":"+plainCreds_password;
              
              byte[] plainCredsBytes = plainCreds.getBytes();
              byte[] base64CredsBytes = Base64.encodeBase64(plainCredsBytes);
              String base64Creds = new String(base64CredsBytes);
      
              HttpHeaders headers = new HttpHeaders();
              headers.add("Authorization", "Basic " + base64Creds);
              HttpEntity<String> request = new HttpEntity<String>(headers);
      
              RestTemplate restTemplate = new RestTemplate();
              ResponseEntity<Object> response = restTemplate.exchange(Constants.getRutaApiOdataHttp() + Constants.WS_REST_API_ODATA_GET_EMPRESAS, HttpMethod.GET, request, Object.class);
              response.getBody();
              
              if(response != null && response.getBody() != null){             
                  System.out.println("YES");
                  System.out.println(response.getBody().toString());
              }
              else
              {
                  System.out.println("NONES");
              }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-03-06
        • 2018-05-19
        • 2020-06-11
        • 2018-10-08
        • 2018-01-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多