• 使用openfeign传递oauth2令牌


    通过RequestInterceptor拦截Feign请求并装填OAuth2 Token

    public class OAuth2FeignRequestInterceptor implements RequestInterceptor {
        private static final String AUTHORIZATION_HEADER = "Authorization";
    
        private static final String BEARER_TOKEN_TYPE = "Bearer";
    
        private final OAuth2RestTemplate oAuth2RestTemplate;
    
        public OAuth2FeignRequestInterceptor(OAuth2RestTemplate oAuth2RestTemplate) {
            this.oAuth2RestTemplate = oAuth2RestTemplate;
        }
    
        @Override
        public void apply(RequestTemplate requestTemplate) {
            requestTemplate.header(AUTHORIZATION_HEADER,
                    String.format("%s %s",
                            BEARER_TOKEN_TYPE,
                            oAuth2RestTemplate.getAccessToken().toString()));
        }
    }
    

    上面的方法通过OAuth2RestTemplate获取token, 也可以直接从请求中获取token

    RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
    if (requestAttributes != null) {
      HttpServletRequest request = ((ServletRequestAttributes) requestAttributes).getRequest();
      String token = request.getHeader("Authorization");
      if(StringUtils.isBlank(token)){
      	token = String.format("%s %s",
    	    "Bearer",
    	    request.getParameter("access_token")));
      }
      ...
    }
    
  • 相关阅读:
    内存对齐
    C++中构造函数
    计算机视觉领域的大牛主页
    各种银行卡的收费情况
    常识
    毕业生必须知道
    计算机视觉领域资料
    人际关系
    生活常识
    可使用在项目的web gantt甘特图有哪些?
  • 原文地址:https://www.cnblogs.com/luguojun/p/16132791.html
Copyright © 2020-2023  润新知