feign之间传递oauth2-token的问题和解决

在微服务架构里,服务与服务之间的调用一般用feign就可以实现,它是一种可视化的rpc,并且集成了ribbon的负载均衡能力,所以很受欢迎。

授权服务

在授权服务里,用户通过用户名密码,或者手机和验证码等方式登陆之后,在http头里会有授权的标识,在客户端调用时,需要添加当时有效的token才可以正常访问被授权的页面。

Content-Type:application/json
Authorization:Bearer d79c064c-8675-4047-a119-fac692e447e8

而在业务层里,服务与服务之间使用feign来实现调用,而授权的代码我们可以通过拦截器实现,在feign请求之前,把当前服务的token添加到目标服务的请求头就可以了,一般是这样实现的。

/**
 * 发送FeignClient设置Header信息.
 * http://www.itmuch.com/spring-cloud-sum/hystrix-threadlocal/
 * Hystrix传播ThreadLocal对象
 */
@Component
public class TokenFeignClientInterceptor implements RequestInterceptor {

  /**
   * token放在请求头.
   *
   * @param requestTemplate 请求参数
   */
  @Override
  public void apply(RequestTemplate requestTemplate) {
    RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
    if (requestAttributes != null) {
      HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
      String token = request.getHeader(TokenContext.KEY_OAUTH2_TOKEN);
      requestTemplate.header(TokenContext.KEY_OAUTH2_TOKEN,
          new String[] {token});
    }
  }
}

上面的拦截器代码没有什么问题,也很好理解,但事实上,当你的feign开启了hystrix功能,如果开启了,需要把hystrix的策略进行修改,默认是THREAD的,这个级别时ThreadLocal是空的,所以你的授权不能传给feign的拦截器.

hystrix.command.default.execution.isolation.strategy: SEMAPHORE 

资源项目里获取当前用户

在另一个项目,需要其它获取当前用户,需要使用下面的代码。

  • 先配置一个授权的地址
security:
  oauth2:
    resource:
      id: user
      user-info-uri: http://${auth.host:localhost}:${auth.port:8002}/user # 这里是授权服务的地址,即auth-service
      prefer-token-info: false

auth:
  host: localhost  #这个不可以用eureka里的服务名,只能使用docker-compose里的服务名
  port: 8002

  • 下面的代码用来获取当前用户
 Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
    String user = objectMapper.writeValueAsString(authentication.getPrincipal());

本讲主要对feign的请求头传递信息进行讲解,开发时遇到的坑总节了一下,分享给大家!

posted @ 2019-07-03 18:29  张占岭  阅读(9950)  评论(0编辑  收藏  举报