Цель состоит в том, чтобы прикрепить некоторые данные из контекста безопасности с помощью RequestInterceptor, но проблема в том, что вызов SecurityContextHolder.getContext().getAuthentication()
всегда возвращает значение null, даже если оно не равно нулю (я уверен на 100%).
Насколько я понимаю, это потому, что Interceptor создается и запускается в другом потоке.
Как я могу решить эту проблему и получить фактические данные из контекста безопасности?
Мой сервис:
@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());
}
}