目標は、RequestInterceptor を使用してセキュリティ コンテキストからいくつかのデータをアタッチすることですが、SecurityContextHolder.getContext().getAuthentication()
null ではないにもかかわらず、呼び出しが常に null を返すという問題があります (100% と確信しています)。
私が理解しているように、それはインターセプターが作成され、他のスレッドで実行されているためです。
この問題を解決して、セキュリティ コンテキストから実際のデータを取得するにはどうすればよいでしょうか?
私のサービス:
@FeignClient(value = "api", configuration = { FeignConfig.class })
public interface DocumentService {
@RequestMapping(value = "/list", method = RequestMethod.GET)
DocumentListOperation list();
}
私の FeignConfig クラス:
@Bean
public RequestInterceptor requestInterceptor() {
return new HeaderInterceptor(userService);
}
public class HeaderInterceptor implements RequestInterceptor {
private UserService userService;
public HeaderInterceptor(UserService userService) {
this.userService = userService;
}
@Override
public void apply(RequestTemplate requestTemplate) {
Authentication a = SecurityContextHolder.getContext().getAuthentication()
requestTemplate.header("authentication", a.toString());
}
}