Spring Cloud fegin 在oauth2 中无法传递授权信息
- 在SpringCloud 使用Security+Oauth2 时候再去使用fegin,发现出现401未授权 经过查阅资料,找到以下解决办法“`
/**
* Created by zhuwj on 2018/1/18.
* 用于解决fegin无法传递授权信息
*/
@Configuration
public class RequestInterceptorConfig implements RequestInterceptor {
private final Logger logger = LoggerFactory.getLogger(getClass());
private static final String AUTHORIZATION_HEADER = "Authorization";
private static final String BEARER_TOKEN_TYPE = "Bearer";
@Override
public void apply(RequestTemplate requestTemplate) {
String accessToken = WebContextUtil.getAccessToken();
logger.debug("RequestInterceptorConfig accessToken :" +accessToken);
requestTemplate.header(AUTHORIZATION_HEADER,
String.format("%s %s",
BEARER_TOKEN_TYPE,
accessToken));
}
源码 可以进行测试