简体   繁体   English

HttpClientErrorException:401未经授权的基本身份验证

[英]HttpClientErrorException: 401 Unauthorized basic authentication

Im trying to get the url with basic authentication. 我正在尝试通过基本身份验证获取网址。 I set the user/password as given below. 我按如下所示设置用户名/密码。 The same credential is working in postman. 邮递员也使用相同的凭证。

String RELATIVE_IDENTITY_URL  = "http://my_url/api/core/v3/people/email/abc@example.com";
    RestTemplate restTemplate;
    Credentials credentials;

    //1. Set credentials
    credentials = new UsernamePasswordCredentials("admin", "admin");

    CredentialsProvider credsProvider = new BasicCredentialsProvider();
    credsProvider.setCredentials( AuthScope.ANY, credentials);

    //2. Bind credentialsProvider to httpClient
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
    CloseableHttpClient httpClient = httpClientBuilder.build();

    HttpComponentsClientHttpRequestFactory factory = new  
            HttpComponentsClientHttpRequestFactory(httpClient);

    //3. create restTemplate
    restTemplate = new RestTemplate();
    restTemplate.setRequestFactory(factory);

    //4. restTemplate execute
    String url = RELATIVE_IDENTITY_URL;

    String xml = restTemplate.getForObject(url,String.class); 
    System.out.println("Done");

I think the credentials are not set correctly. 我认为凭据设置不正确。 What is wrong here.? 这是怎么了? Error: 错误:

Exception in thread "main" org.springframework.web.client.HttpClientErrorException: 401 Unauthorized
    at org.springframework.web.client.DefaultResponseErrorHandler.handleError(DefaultResponseErrorHandler.java:91)
    at org.springframework.web.client.RestTemplate.handleResponse(RestTemplate.java:667)
    at org.springframework.web.client.RestTemplate.doExecute(RestTemplate.java:620)
    at org.springframework.web.client.RestTemplate.execute(RestTemplate.java:580)
    at org.springframework.web.client.RestTemplate.getForObject(RestTemplate.java:287)
    at com.src.AuthRestService.main(AuthRestService.java:85)    

您缺少auth标头,并在其余模板执行中设置了凭据。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中使用基本身份验证错误“HTTP / 1.1 401 Unauthorized” - EWS 2010 - Error “HTTP/1.1 401 Unauthorized” with basic authentication in android - EWS 2010 Spring 安全基本身份验证 - 401 未经授权使用正确的凭据 - Spring security Basic Authentication - 401 Unauthorized with correct credentials Springboot - 使用 JWT 身份验证的 websocket 未经授权的 401 - Springboot - websocket unauthorized 401 with JWT authentication 401未经授权 - 401 Unauthorized 带有2条腿的oauth错误的Google Tasks Authentication:401未经授权 - Google Tasks Authentication with 2 legged oauth error: 401 Unauthorized 身份验证错误:无法应对任何这些挑战:{} Android - 401未经授权 - Authentication error: Unable to respond to any of these challenges: {} Android - 401 Unauthorized 尝试通过https和基本身份验证获取http响应 - 获取“未授权” - Trying to get http response over https and basic authentication - getting “Unauthorized” 处理Spring Security中基本身份验证的未经授权的错误消息 - Handle unauthorized error message for Basic Authentication in Spring Security 如何在tomcat 8.5中配置基本身份验证? HTTP错误401 - How to configure basic authentication in tomcat 8.5? HTTP Error 401 "Spring Boot 2:如果附加了 Authorization 标头,Basic Http Auth 会导致未受保护的端点以 401“Unauthorized”响应" - Spring Boot 2: Basic Http Auth causes unprotected endpoints to respond with 401 "Unauthorized" if Authorization header is attached
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM