在微服务中使用Fegin进行内部调用无法获取用户信息

这种是因为我们在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.TokenType.BEARER.getValue(), tokenValue));
        }
    }

}

二、开启服务降级熔断

        此时就是不同线程,线程之间是相互隔离的,所以子线程无法获取主线程的内容,此时可以使用RequestContextHolder ,RequestContextHolder 维护了两个容器,一个是不能跨线程的ThreadLocal,一个是实现了InheritableThreadLocal的NamedInheritableThreadLocal。InheritableThreadLocal是可以把父线程的数据传递到子线程的,基于这个原理RequestContextHolder把调用方的请求信息带进了子线程,借助于这个原理就能实现令牌中继了。

@Configuration
public class MyOAuthRequestInterceptor implements RequestInterceptor {

	 @Override
    public void apply(RequestTemplate template) {
 		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
		if (Objects.nonNull(requestAttributes)) {
			String authorizationHeader = requestAttributes.getRequest().getHeader(HttpHeaders.AUTHORIZATION);
				template.header(HttpHeaders.AUTHORIZATION,authorizationHeader);
		}
    }

}

  • 7
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

刘个Java

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值