英文:
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("/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);
答案1
得分: 2
应该像这样工作:@RequestHeader(value = "Authorization") String authorization
,但请确保传递正确的值,必须类似于Bearer token
。
英文:
Should work like this @RequestHeader(value = "Authorization") 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("/api/users/find")
Optional<UserDTO> findUserByEmail(
@RequestHeaders("Authorization") String token, @RequestParam("email") String email);
Just when you are calling this particular method add "Bearer " in front of the value of the token
token = "Bearer " + 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;
或者非常简单地添加拦截器
@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));
}
}
}
<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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论