zoukankan      html  css  js  c++  java
  • 使用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")));
      }
      ...
    }
    
  • 相关阅读:
    第二阶段冲刺记录八
    用户体验评价
    找水王
    第二阶段冲刺记录七
    第二阶段冲刺记录六
    第14周学习进度
    第二阶段冲刺记录五
    第二阶段冲刺记录四
    第一阶段意见评论
    IP协议号大全
  • 原文地址:https://www.cnblogs.com/luguojun/p/14294729.html
Copyright © 2011-2022 走看看