jpganz18
jpganz18

Reputation: 5858

how can I get the current Bearer token of my request?

I am trying to do something relatively simple.

I have a request that receives an oauth token (Bearer asdf22324...) I need it to pass it to my feign client to be able to request another service using the same token.

Is it possible to do that? Any ideas/examples?

I have tried this way:

@PostMapping("/login")
ResponseEntity<Void> loginUser(@RequestHeader("Authorization") String authHeader);

But authHeader is always empty...

I also tried the interceptor, but I'm not sure how it would affect others (and not sure how to invoke it either).

Feign Client - Dynamic Authorization Header

Ideas?

Upvotes: 6

Views: 18853

Answers (1)

the_tech_maddy
the_tech_maddy

Reputation: 597

May be you can try implementing the Filter (say TokenFilter) and inside it you can read the Authorization header from the HttpServletRequest and extract the token.

String authTokenHeader = request.getHeader("Authorization");

// parse the token
// there can be type of token passed. So you may need to take the substring after the type and the whitespace. Eg: Bearer <TOKEN>

Upvotes: 7

Related Questions