Propagate HTTP header (JWT Token) over services using spring rest template

后端 未结 3 1592
萌比男神i
萌比男神i 2020-12-14 04:23

I have a microservice architecture, both of them securized by spring security an JWT tokens.

So, when I call my first microservice, I want to take the JWT token and

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-14 04:58

    Basically your token should be located in the header of the request, like for example: Authorization: Bearer . For getting it you can retrieve any header value by @RequestHeader() in your controller:

    @GetMapping("/someMapping")
    public String someMethod(@RequestHeader("Authorization") String token) {
    
    }
    

    Now you can place the token within the header for the following request:

    HttpHeaders headers = new HttpHeaders();
    headers.set("Authorization", token);
    
    HttpEntity entityReq = new HttpEntity(request, headers);
    

    Now you can pass the HttpEntity to your rest template:

    template.exchange("RestSvcUrl", HttpMethod.POST, entityReq, SomeResponse.class);
    

    Hope I could help

提交回复
热议问题