注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

流星永恒的博客

JSF,Facelets,Rich(Prime)Faces,和java的笔记

 
 
 

日志

 
 

Feign relay the oauth2 token  

2017-02-09 16:56:58|  分类: webservice |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
OAuth2FeignAutoConfiguration
@Configuration
@ConditionalOnClass({Feign.class})
@ConditionalOnProperty(value = "feign.oauth2.enabled", matchIfMissing = true)
public class OAuth2FeignAutoConfiguration {
@Bean
public RequestInterceptor oauth2FeignRequestInterceptor() {
return new OAuth2FeignRequestInterceptor();
}
}
OAuth2FeignRequestInterceptor
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails;

/**
* Created by daniely on 2017/2/6.
*/
@Slf4j
public class OAuth2FeignRequestInterceptor implements RequestInterceptor {
private static final String AUTHORIZATION_HEADER = "Authorization";

private static final String BEARER_TOKEN_TYPE = "Bearer";

public OAuth2FeignRequestInterceptor() {
}

@Override
public void apply(RequestTemplate template) {
if (template.headers().containsKey(AUTHORIZATION_HEADER)) {
log.warn("The Authorization token has been already set");
} else {
boolean relayed = false;
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication instanceof AbstractAuthenticationToken) {
AbstractAuthenticationToken aat = (AbstractAuthenticationToken) authentication;
OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) aat.getDetails();
String type = details.getTokenType();
String jwt = details.getTokenValue();
if (OAuth2AccessToken.BEARER_TYPE.equalsIgnoreCase(type) && jwt != null) {
relayed = true;
log.debug("The Authorization token has added in header, token:{}", jwt);
template.header("Authorization", String.format("%s %s", OAuth2AccessToken.BEARER_TYPE, jwt));
}
}
if (!relayed) {
log.warn("Not relay the JWT for service: {}", template.url());
}
}
}
}

  评论这张
 
阅读(60)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017