add Authorization token using Feign client

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

add Authorization token using Feign client

问题

@GetMapping("/api/users/find")
@Headers("Authorization: Bearer {token}")
Optional<UserDTO> findUserByEmail(
    @Param("token") String token, @RequestParam("email") String email);
@GetMapping("/api/users/find")
Optional<UserDTO> findUserByEmail(
    @RequestHeaders("Authorization") String token, @RequestParam("email") String email);
@GetMapping("/api/users/find")
Optional<UserDTO> findUserByEmail(
    @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.

    @GetMapping(&quot;/api/users/find&quot;)
    @Headers(&quot;Authorization: Bearer {token}&quot;)
    Optional&lt;UserDTO&gt; findUserByEmail(
        @Param(&quot;token&quot;) String token, @RequestParam(&quot;email&quot;) String email);
    @GetMapping(&quot;/api/users/find&quot;)
    Optional&lt;UserDTO&gt; findUserByEmail(
        @RequestHeaders(&quot;Authorization&quot;) String token, @RequestParam(&quot;email&quot;) String email);
    @GetMapping(&quot;/api/users/find&quot;)
    Optional&lt;UserDTO&gt; findUserByEmail(
        @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

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

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

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

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

Your this code is absolutely correct.

    @GetMapping(&quot;/api/users/find&quot;)
    Optional&lt;UserDTO&gt; findUserByEmail(
        @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

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

答案3

得分: 0

创建类似这样的头部并传递给你的Feign客户端

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

示例1

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

@Component
public class AuthFeignInterceptor implements RequestInterceptor {

    @Override
    public void apply(RequestTemplate template) {
        final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        if (requestAttributes != null) {
            final HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
            template.header(HttpHeaders.AUTHORIZATION, httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION));
        }
    }
}

示例2


<details>
<summary>英文:</summary>

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;


[Example 1](https://github.com/DeepuGeorgeJacob1/school-management/blob/main/docs/FeignClientJDBCUserSecurityS1.md)

Or very simple add intercepter

@Component
public class AuthFeignInterceptor implements RequestInterceptor {

@Override
public void apply(RequestTemplate template) {
    final RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
    if (requestAttributes != null) {
        final HttpServletRequest httpServletRequest = ((ServletRequestAttributes) requestAttributes).getRequest();
        template.header(HttpHeaders.AUTHORIZATION, httpServletRequest.getHeader(HttpHeaders.AUTHORIZATION));
    }
}

}


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

</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:

确定