如何将标题添加到RESTTEMPLATE中

发布于 2025-02-11 15:31:36 字数 874 浏览 1 评论 0原文

我正在尝试将标头添加到RESTTEMPLATE中,其中一种方法Exchange。 在标题中,我正在放置令牌访问,我们可以访问它。 我遇到的错误是我未获得授权,401状态正在回馈。有什么建议我做错了什么?

 HttpHeaders headersAuth = new HttpHeaders();
        headersAuth.set(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<?> entityAuth =  new HttpEntity<>(headersAuth);
        String urlTemplateAuth = UriComponentsBuilder.fromHttpUrl("some url")
                .encode()
                .toUriString();
        Map<String,String> queryParamsAuth = new HashMap<>();
        queryParamsAuth.put("Authorization",tokenValue); //here is my token access


            ResponseEntity <UserGetPhoneDto> userGetPhoneDtoResponseEntity = restTemplate.exchange(urlTemplateAuth,HttpMethod.GET,entityAuth,UserGetPhoneDto.class,queryParamsAuth); 
//here i am getting error of 401 status

I am trying to add a header into the restTemplate, with one of its methods exchange.
In header i am putting the token access, which we can access with.
The error i am getting is that i am not Authorized, 401 status is giving back . Any advices what i am doing wrong?

 HttpHeaders headersAuth = new HttpHeaders();
        headersAuth.set(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_VALUE);
        HttpEntity<?> entityAuth =  new HttpEntity<>(headersAuth);
        String urlTemplateAuth = UriComponentsBuilder.fromHttpUrl("some url")
                .encode()
                .toUriString();
        Map<String,String> queryParamsAuth = new HashMap<>();
        queryParamsAuth.put("Authorization",tokenValue); //here is my token access


            ResponseEntity <UserGetPhoneDto> userGetPhoneDtoResponseEntity = restTemplate.exchange(urlTemplateAuth,HttpMethod.GET,entityAuth,UserGetPhoneDto.class,queryParamsAuth); 
//here i am getting error of 401 status

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

故人爱我别走 2025-02-18 15:31:36

您尚未将令牌设置为标题,而是将其设置在查询参数中。
您可以使用HeaderSauth.setBearerauth()设置携带者令牌,或使用setBasicauth()设置用户名和密码。
将其放在标题中的另一种方法:

headersAuth.set(HttpHeaders.AUTHORIZATION, token);

OAuth2服务器也可以在查询参数中检索您的令牌,并使用名称为“ Access_Token”。

You have not set token to header yet, you set it in your query parameter.
You can use headersAuth.setBearerAuth() to set bearer token, or use setBasicAuth() to set username and password.
Another way to put it in your header:

headersAuth.set(HttpHeaders.AUTHORIZATION, token);

OAuth2 server can retrieve your token in query parameter with name 'access_token' too.

秋叶绚丽 2025-02-18 15:31:36

您的代码不会将令牌放入请求标题中。

    HttpHeaders headersAuth = new HttpHeaders();
    
    headersAuth.set(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_VALUE);
    
    String urlTemplateAuth = UriComponentsBuilder.fromHttpUrl("some url")
            .encode()
            .toUriString();
    Map<String,String> queryParamsAuth = new HashMap<>();
    headersAuth.put("Authorization",tokenValue); //here is put it into headers
    HttpEntity<?> entityAuth =  new HttpEntity<>(headersAuth);//build entity by header
   //exchange...

您可以阅读此文档以了解有关HTTP的更多信息。
https://developer.mozilla.mozilla.org/en-us/us/docs/web/web/ http

Your code doesn't put the token into the request header.

    HttpHeaders headersAuth = new HttpHeaders();
    
    headersAuth.set(HttpHeaders.ACCEPT,MediaType.APPLICATION_JSON_VALUE);
    
    String urlTemplateAuth = UriComponentsBuilder.fromHttpUrl("some url")
            .encode()
            .toUriString();
    Map<String,String> queryParamsAuth = new HashMap<>();
    headersAuth.put("Authorization",tokenValue); //here is put it into headers
    HttpEntity<?> entityAuth =  new HttpEntity<>(headersAuth);//build entity by header
   //exchange...

You can read this document to learn more about http.
https://developer.mozilla.org/en-US/docs/Web/HTTP

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文