即使在 RestTemplate 的 Basic Authentication header 中发送了正确的凭据后,仍出现 401 Unauthorized 错误

Getting 401 Unauthorized error even after sending correct credentials in Basic Authentication header of RestTemplate

以下是正在触发的包装器 API 代码 :-

logger.info("Inside Method");
    String integrationUrl=null;
    String integrationUsername = null;
    String integrationPassword = null;
    
        if(StringUtils.isNotBlank(System.getenv("PushGDSDataUrl")))
            integrationUrl = System.getenv("PushGDSDataUrl");
        else
            integrationUrl = "https://test.connect.boomi.com/ws/simple/createCommentsx-test";
        
        if(StringUtils.isNotBlank(System.getenv("PushGDSDataUrlUsername")))
            integrationUsername = System.getenv("PushGDSDataUrlUsername");
        else
            integrationUsername = "<integration-username>";
                                   
        
        if(StringUtils.isNotBlank(System.getenv("PushGDSDataUrlPwd")))
            integrationPassword = System.getenv("PushGDSDataUrlPwd");
        else
            integrationPassword = "<integration-password>";
                                   
    
    RestTemplate restTemplate = new RestTemplate();
    HttpHeaders headers = new HttpHeaders();
    headers.setContentType(MediaType.APPLICATION_JSON);
   // headers.set("dcuid", integrationUsername);
    //headers.set("dcpwd", integrationPassword);
  //  headers.set("Content-Type", "application/json");
    
    String auth = integrationUsername + ":" + integrationPassword;
    byte[] encodedAuth = Base64.encodeBase64( 
       auth.getBytes(Charset.forName("US-ASCII")) );
    String authHeader = "Basic " + new String( encodedAuth );
    headers.set( "Authorization", authHeader );
   // headers.set("Body","{\r\n\"class_name\":\"Server\",\r\n\"company_name:\"ABC\"\r\n}\r\n");
    
    
    GDSPushData newObj = new GDSPushData();
    newObj.setClass_name(className);
    newObj.setCompany_name(companyName);
    
    String jsonInString = new Gson().toJson(newObj);
    JSONObject mJSONObject = new JSONObject(jsonInString);
    logger.info("Request is set");
    
    



    HttpEntity<String> entityReq = new HttpEntity<String>(mJSONObject.toString(),headers);
    
    logger.info("Request is converted");
    
    
        
         URI targetUrl = UriComponentsBuilder.fromUriString(integrationUrl).build().encode().toUri(); //Build base url
            
            
            logger.info("target Url"+targetUrl);
            
          
            
            ResponseEntity<String> result = restTemplate.exchange(integrationUrl, HttpMethod.POST, entityReq, String.class);
            
            //String result = restTemplate.getForObject(targetUrl, String.class);



            logger.info(result.getStatusCodeValue());
            logger.info(result.getBody());

当我们在指定相同凭据后使用邮递员触发上面代码中提到的集成 url 时,它工作正常,我们得到 200 OK 响应代码,当相同代码从 Spring Java 应用程序然后我们收到 401 未经授权的错误。

请尝试使用以下代码

HttpClient client = HttpClientBuilder.create().build();
HttpPost post = new HttpPost("<Your_URL>");

 List<NameValuePair> arguments = new ArrayList<>();
arguments.add(new BasicNameValuePair("class_name", className));
arguments.add(new BasicNameValuePair("company_name", companyName));

 try{
String auth = integrationUsername + ":" + integrationPassword;
byte[] encodedAuth = Base64.encodeBase64(
auth.getBytes(Charset.forName("US-ASCII")) );
String authHeader = "Basic " + new String( encodedAuth );

post.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
post.setHeader(HttpHeaders.ACCEPT, "application/json");
post.setHeader(HttpHeaders.AUTHORIZATION,authHeader);
post.setEntity(new UrlEncodedFormEntity(arguments));
HttpResponse response = client.execute(post);