add Authorization token using Feign client

huangapple go评论115阅读模式
英文:

add Authorization token using Feign client

问题

  1. @GetMapping("/api/users/find")
  2. @Headers("Authorization: Bearer {token}")
  3. Optional<UserDTO> findUserByEmail(
  4. @Param("token") String token, @RequestParam("email") String email);
  1. @GetMapping("/api/users/find")
  2. Optional<UserDTO> findUserByEmail(
  3. @RequestHeaders("Authorization") String token, @RequestParam("email") String email);
  1. @GetMapping("/api/users/find")
  2. Optional<UserDTO> findUserByEmail(
  3. @HeaderMap Map<String, Object> headers, @RequestParam("email") String email);
英文:

I have two service connected to a registry, one of them need to query some data from the other, the token need to be passed to the endpoint.

I Have tried the following but it didn't work, the endpoint act as if no token is provided.

  1. @GetMapping(&quot;/api/users/find&quot;)
  2. @Headers(&quot;Authorization: Bearer {token}&quot;)
  3. Optional&lt;UserDTO&gt; findUserByEmail(
  4. @Param(&quot;token&quot;) String token, @RequestParam(&quot;email&quot;) String email);
  1. @GetMapping(&quot;/api/users/find&quot;)
  2. Optional&lt;UserDTO&gt; findUserByEmail(
  3. @RequestHeaders(&quot;Authorization&quot;) String token, @RequestParam(&quot;email&quot;) String email);
  1. @GetMapping(&quot;/api/users/find&quot;)
  2. Optional&lt;UserDTO&gt; findUserByEmail(
  3. @HeaderMap Map&lt;String, Object&gt; headers , @RequestParam(&quot;email&quot;) String email);

答案1

得分: 2

应该像这样工作:@RequestHeader(value = "Authorization") String authorization,但请确保传递正确的值,必须类似于Bearer token

英文:

Should work like this @RequestHeader(value = &quot;Authorization&quot;) String authorization, but make sure you pass the right value, must be something like Bearer token.

答案2

得分: 1

你的这段代码是完全正确的。

  1. @GetMapping("/api/users/find")
  2. Optional<UserDTO> findUserByEmail(
  3. @RequestHeaders("Authorization") String token, @RequestParam("email") String email);

当你调用这个特定的方法时,在 token 的值前面加上 "Bearer "。

  1. token = "Bearer " + token;
  2. findUserByEmail(token, email);
英文:

Your this code is absolutely correct.

  1. @GetMapping(&quot;/api/users/find&quot;)
  2. Optional&lt;UserDTO&gt; findUserByEmail(
  3. @RequestHeaders(&quot;Authorization&quot;) String token, @RequestParam(&quot;email&quot;) String email);

Just when you are calling this particular method add "Bearer " in front of the value of the token

  1. token = &quot;Bearer &quot; + token;
  2. findUserByEmail(token,email);

答案3

得分: 0

  1. 创建类似这样的头部并传递给你的Feign客户端
  2. ```java
  3. private HttpHeaders getHeaders(final HttpServletRequest httpServletRequest) {
  4. final HttpHeaders headers = new HttpHeaders();
  5. headers.add("authorization", httpServletRequest.getHeader("authorization"));
  6. return headers;

示例1

或者非常简单地添加拦截器

  1. @Component
  2. public class AuthFeignInterceptor implements RequestInterceptor {
  3. @Override
  4. public void apply(RequestTemplate template) {
  5. final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  6. if (requestAttributes != null) {
  7. final HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
  8. template.header(HttpHeaders.AUTHORIZATION, httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION));
  9. }
  10. }
  11. }

示例2

  1. <details>
  2. <summary>英文:</summary>
  3. Create Header like this and pass to your feign client

private HttpHeaders getHeaders(final HttpServletRequest httpServletRequest) {
final HttpHeaders headers = new HttpHeaders();
headers.add("authorization", httpServletRequest.getHeader("authorization"));
return headers;

  1. [Example 1](https://github.com/DeepuGeorgeJacob1/school-management/blob/main/docs/FeignClientJDBCUserSecurityS1.md)
  2. Or very simple add intercepter

@Component
public class AuthFeignInterceptor implements RequestInterceptor {

  1. @Override
  2. public void apply(RequestTemplate template) {
  3. final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
  4. if (requestAttributes != null) {
  5. final HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
  6. template.header(HttpHeaders.AUTHORIZATION, httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION));
  7. }
  8. }

}

  1. [Example 2](https://github.com/DeepuGeorgeJacob1/school-management/blob/main/docs/FeignClientJDBCUserSecurityS2.md)
  2. </details>

huangapple
  • 本文由 发表于 2020年8月28日 06:22:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63624910.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定