spring cloud 调用feign 如何让spring security 拦截器拦截除了通过FeignClientInterceptor接口拦截器中添加Spring Security的认证信息外还有其他方式,请展示完整代码
时间: 2023-02-18 21:49:53 浏览: 391
。
可以使用Spring Security提供的FeignRequestInterceptor拦截器,以下是完整的代码示例:
@Bean
public FeignRequestInterceptor feignRequestInterceptor() {
return new FeignRequestInterceptor(new BasicAuthentication("username", "password"));
}
@Bean
public RequestInterceptor requestInterceptor() {
return requestTemplate -> {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && authentication.getDetails() instanceof OAuth2AuthenticationDetails) {
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails();
requestTemplate.header("Authorization", "Bearer " + details.getTokenValue());
}
};
}
阅读全文