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的更多相关文章

  1. Spring Cloud中Feign如何统一设置验证token

    代码地址:https://github.com/hbbliyong/springcloud.git 原理是通过每个微服务请求之前都从认证服务获取认证之后的token,然后将token放入到请求头中带过 ...

  2. SpringCloud系列——Feign 服务调用

    前言 前面我们已经实现了服务的注册与发现(请戳:SpringCloud系列——Eureka 服务注册与发现),并且在注册中心注册了一个服务myspringboot,本文记录多个服务之间使用Feign调 ...

  3. SpringCloud微服务基础 Eureka、Feign、Ribbon、Zuul、Hystrix、配置中心的基础使用

    1.单点系统架构 传统项目架构 传统项目分为三层架构,将业务逻辑层.数据库访问层.控制层放入在一个项目中. 优点:适合于个人或者小团队开发,不适合大团队开发. 分布式项目架构 根据业务需求进行拆分成N ...

  4. Feign 失败降级未生效和超时配置优先级问题

    一.问题: 生产环境服务A 通过feign调用 服务B,服务A报警信息如下: 详细分析发现问题 (1)服务A调用服务B失败,未触发声明的失败降级操作 (2)同时配置ribbon和feign超时时间,优 ...

  5. Feign负载均衡

    Feign是一个声明式的Web Service客户端,比Ribbon好用,默认也是轮巡.我们只需要使用Feign创建一个接口,并用注解就好了.如果你基于spring cloud发布一个接口,实际上就是 ...

  6. Spring Cloud 各组件调优参数

    Spring Cloud整合了各种组件,每个组件往往还有各种参数.本文来详细探讨Spring Cloud各组件的调优参数. Tomcat配置参数 1 server: 2 tomcat: 3 max-c ...

  7. zuul超时及重试配置

    配置实例 ##timeout config hystrix: command: default: execution: timeout: enabled: true isolation: thread ...

  8. spring cloud 2.x版本 Gateway熔断、限流教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  9. 【springcloud】常见面试题总结

    1.springcloud与dubbo的区别? https://jingyan.baidu.com/article/b0b63dbf3784294a483070fa.html 1.1 springcl ...

随机推荐

  1. 【Java】获取当前操作系统桌面路径

    //当前用户桌面 File desktopDir = FileSystemView.getFileSystemView() .getHomeDirectory(); String desktopPat ...

  2. Python开发专属壁纸下载与轮换程序

    本人对于壁纸一直偏佛系,不爱特意去找一堆壁纸.因此用Python简单地搞了一个自动下载壁纸,定时随机轮换的功能来自娱自乐,顺便分享给大家. 原文链接:https://www.codernice.top ...

  3. vim 的初始配置

    我是一个vim爱好者,项目开发过程中,大量地使用vim作为编辑工具. 今天刚开箱了一台新的 CentOS7, 现将Vim安装后的一些基本配置作一下记录. 使用配置模板 vim安装完之后,大多数配置都是 ...

  4. python 安装pyqt

    ---恢复内容开始--- 一.安装 1.官网:www.riverbankcomputing.com 2.使用命令安装,可以自动去官网查找与Python版本号相同的程序进行下载,比较方便,如果不是这样也 ...

  5. Redis 哨兵机制以及灾难演练

    #### 哨兵都采用这个配置即可 ##### 1.修改sentinel.conf配置文件 ![image](https://img2018.cnblogs.com/blog/1334966/20191 ...

  6. PHP next

    1.函数的作用:返回数组当前元素位置的下一个元素 2.函数的参数: @param array &$array 3. 例子一:数组拷贝时,内部指针的位置也一起拷贝 <?php $arr1 ...

  7. prototype与 _proto__的关系

    prototype与 __ proto__ 都是在这个过程中催生的产物,我们一会儿马上讨论,在这之...做对象即可,那javascript种究竟是通过什么来明确继承关系的呢. 一.构造函数: 构造函数 ...

  8. CSS中重要的BFC

    CSS中有个重要的概念BFC,搞懂BFC可以让我们理解CSS中某些原本看似诡异的地方. 简介 在解释BFC之前,先说一下文档流.我们常说的文档流其实分为定位流.浮动流.普通流三种.而普通流其实就是指B ...

  9. Cocos2d-x 学习笔记(15.2) EventDispatcher 事件分发机制 dispatchEvent(event)

    1. 事件分发方法 EventDispatcher::dispatchEvent(Event* event) 首先通过_isEnabled标志判断事件分发是否启用. 执行 updateDirtyFla ...

  10. .NET Core ORM 类库Petapoco中对分页Page添加Order By对查询的影响

    最近一直在使用Petapoco+Entity Framework Core结合开发一套系统. 使用EFCore进行Code First编码,使用PMC命令生成数据库表的信息. 使用Petapoco进行 ...