Feign设置assessToken
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.OAuth2RestTemplate; import feign.RequestInterceptor;
import feign.RequestTemplate; /**
*
*用于解决fegin无法传递授权信息
* @author cicoding
* @date 2018年5月30日
* @Copyright
*
* <pre>
* =================Modify Record=================
* Modifier date Content
* cicoding 2019年5月26日 新增
*
* </pre>
*/
@Configuration
public class FeignRequestInterceptor implements RequestInterceptor {
private final Logger logger = LoggerFactory.getLogger(getClass()); private static final String AUTHORIZATION_HEADER = "Authorization"; private static final String BEARER_TOKEN_TYPE = "Bearer"; @Autowired
private OAuth2RestTemplate oAuth2RestTemplate; @Override
public void apply(RequestTemplate requestTemplate) {
String accessToken = WebContextUtil.getAccessToken();
if(accessToken == null){
accessToken =oAuth2RestTemplate.getAccessToken().getValue();
}
logger.debug("RequestInterceptorConfig accessToken :" +accessToken);
requestTemplate.header(AUTHORIZATION_HEADER,
String.format("%s %s",
BEARER_TOKEN_TYPE,
accessToken));
}
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.client.DefaultOAuth2RequestAuthenticator;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsResourceDetails; /**
*
*用于解决fegin无法传递授权信息
* @author cicoding
* @date 2018年5月30日
* @Copyright
*
* <pre>
* =================Modify Record=================
* Modifier date Content
* cicoding 2019年5月26日 新增
*
* </pre>
*/
@Configuration
public class OAuth2RestTemplateConfiguration {
@Bean
public OAuth2RestTemplate oauth2RestTemplate() {
ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();
resourceDetails.setClientId("webapp");
resourceDetails.setClientSecret("webapp");
resourceDetails.setId("service-user");
resourceDetails.setAccessTokenUri("http://127.0.0.1:9060/oauth/token");
OAuth2RestTemplate oAuth2RestTemplate = new OAuth2RestTemplate(resourceDetails);
oAuth2RestTemplate.setAuthenticator(new DefaultOAuth2RequestAuthenticator());
return oAuth2RestTemplate;
}
}
Feign设置assessToken的更多相关文章
- Spring Cloud中Feign如何统一设置验证token
代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...
- SpringCloud系列——Feign 服务调用
前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...
- SpringCloud微服务基础 Eureka、Feign、Ribbon、Zuul、Hystrix、配置中心的基础使用
1.单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N ...
- Feign 失败降级未生效和超时配置优先级问题
一.问题: 生产环境服务A 通过feign调用 服务B,服务A报警信息如下: 详细分析发现问题 (1)服务A调用服务B失败,未触发声明的失败降级操作 (2)同时配置ribbon和feign超时时间,优 ...
- Feign负载均衡
Feign是一个声明式的Web Service客户端,比Ribbon好用,默认也是轮巡.我们只需要使用Feign创建一个接口,并用注解就好了.如果你基于spring cloud发布一个接口,实际上就是 ...
- Spring Cloud 各组件调优参数
Spring Cloud整合了各种组件,每个组件往往还有各种参数.本文来详细探讨Spring Cloud各组件的调优参数. Tomcat配置参数 1 server: 2 tomcat: 3 max-c ...
- zuul超时及重试配置
配置实例 ##timeout config hystrix: command: default: execution: timeout: enabled: true isolation: thread ...
- spring cloud 2.x版本 Gateway熔断、限流教程
前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...
- 【springcloud】常见面试题总结
1.springcloud与dubbo的区别? https://jingyan.baidu.com/article/b0b63dbf3784294a483070fa.html 1.1 springcl ...
随机推荐
- Linux 下复制整个文件夹的命令
在 Linux 下复制整个文件夹,包括它的子文件夹及其隐藏文件的方法是: cp -r /etc/skel /home/user 或者 mkdir /home/<new_user> cp - ...
- 通俗易懂spring之singleton和prototype
关于spring bean作用域,基于不同的容器,会有所不同,如BeanFactory和ApplicationContext容器就有所不同,在本篇文章,主要讲解基于ApplicationContext ...
- 【Java】Java 单文件下载及重命名
代码(仅供参考): /* * 另存为 */ @RequestMapping("/saveAs.do") public @ResponseBody void saveAs(Strin ...
- EF Core设置字段默认时间
---恢复内容开始--- 在EF的官方文档上只提到了用 Fluent API来设置默认值. 但是我们日常开发中,会把公用字段都写成基类.比如行创建时间 在需要默认时间的字段加上一个特性 [Databa ...
- 清除SQL Server Management Studio的最近服务器列表
C:\Users\dell\AppData\Roaming\Microsoft\SQL Server Management Studio\12.0\SqlStudio.bin
- jsp JavaBean el表达式
JSP三大指令 一个jsp页面中,可以有0~N个指令的定义! 1. page --> 最复杂:<%@page language="java" info="xx ...
- 数据结构(java)
数据结构1.什么是数据结构?数据结构有哪些? 数据结构是指数据在内存中存放的机制. 不同的数据结构在数据的查询,增删该的情况下性能是不一样的. 数据结构是可以模拟业务场景. 常见的数据结构有:栈,队列 ...
- cocos2dx 3.2 内存管理
一.引用计数(cocos2d-x3.2的Node类中用到) 概念:记录当前对象被引用的次数.当次数为0时释放. 1 . retain 与 release 每调用一次retain()使计数+1 每调用 ...
- F#周报2019年第42期
新闻 TypeShape的性能 .NET Core 3.0包含.NET Framework API移植项目 宣告.NET Core 3.1预览版1 .NET Core 3.1预览版1中ASP.NET ...
- 初识域渗透利器Empire
Empire 是一款类似Metasploit 的渗透测试框架,基于python 编写,Empire是一个纯粹的PowerShell 后开发代理,建立在密码安全通信和灵活的架构上.Empire 实现了无 ...