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")));
      }
      ...
    }
    
  • 相关阅读:
    python流程控制
    数据类型
    python之初接触
    使用 HttpWebRequest 向网站提交数据
    在 ASP.NET MVC4 中使用 NInject
    第一篇 微信商城 开发前的准备工作
    (一)模块基础
    函数之递归、匿名函数及内置方法
    装饰器的使用原理
    mybatis返回list
  • 原文地址:https://www.cnblogs.com/luguojun/p/14294729.html
Copyright © 2011-2022 走看看