0

i'm working on a spring boot project where i should call a rest api using Feign via Spring Cloud, i can call the rest api using feignClient without any problem, now the rest api that i call needs a JWT to let me consume it, to send a JWT from my code i used RequestInterceptor and this my code :

class AuthInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        template.header("Authorization", "Bearer eyJraWQiOiJOcTVZWmUwNF8tazZfR3RySDZkenBWbHhkY1uV_1wSxWPGZui-t1Zf2BkbqZ_h44RkjVtQquIe0Yz9efWS6QZQ");

    }

}

i put manually the JWT in the code and this work fine ...

my issue is : the JWT expire after 30 min and i should call manually another rest api that generate a JWT then i hardcode it in my code...

my question is : there any solution to call programmatically the api that generate JWT then inject this JWT in the Interceptor?

Thanks in advance.

Best Regards.

James
  • 1,190
  • 5
  • 27
  • 52

1 Answers1

1

Get the Token from the current HttpServletRequest header.

public void apply(RequestTemplate template) {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes())
            .getRequest();
    String jwtToken = request.getHeader(HttpHeaders.AUTHORIZATION);
    if (jwtToken != null) {
        template.header(HttpHeaders.AUTHORIZATION, jwtToken);
    }
}
GnanaJeyam
  • 2,780
  • 16
  • 27
  • jeyama95 , thank you for your time , but my issue is , to get tje jwt i should make another Feign client call to another rest api that generate jwt as response then i should use this jwt in the Interceptor to call my target api that need this jwt – James Aug 08 '19 at 12:48
  • Then after getting the from the response. Set that token in the feign client using @Headers('Authoration : {token}') but for this you need to send the token as a method Param in the feign client method. – GnanaJeyam Aug 08 '19 at 13:34