这种是因为我们在A服务调用B服务时,并不能把当前线程的用户信息带过去,因为SecurityContextHolder.getContext().getAuthentication()取值,是取的自己本地线程的。Feign集成Hystrix默认是关闭Hystrix的,只有在配置文件中设置feign.hystrix.enabled=true才会开启Hystrix。开启Hystrix后feign之间的方法调用就会默认启动新的线程执行,和主程序不在一个线程中,因此如果上下文中存在ThreadLocal变量,在该方法中就失效了。
一、不开启服务降级熔断
可以直接使用SecurityContextHolder.getContext().getAuthentication();获取当前token并传递给下游服务
@Configuration
public class MyOAuthRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof JwtAuthenticationToken){
JwtAuthenticationToken jwtAuthenticationToken = (JwtAuthenticationToken) authentication;
String tokenValue = jwtAuthenticationToken.getToken().getTokenValue();
template.header(HttpHeaders.AUTHORIZATION,
String.format("%s %s", OAuth2AccessToken.To