From 7ade964249bf6bd91a63503d740b563469cadf05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E6=88=AC?= Date: Sat, 9 Mar 2019 16:09:52 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0feign=E6=8B=A6=E6=88=AA?= =?UTF-8?q?=E5=99=A8=EF=BC=8C=E9=80=82=E9=85=8D=E6=97=A0=E7=BD=91=E7=BB=9C?= =?UTF-8?q?=E9=9A=94=E7=A6=BB=E6=97=B6token=E7=9A=84=E4=BC=A0=E9=80=92?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../zlt-ribbon-spring-boot-starter/pom.xml | 5 +++ .../annotation/EnableFeignInterceptor.java | 22 +++++++++++ .../ribbon/config/FeignInterceptorConfig.java | 37 +++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java create mode 100644 zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/pom.xml b/zlt-commons/zlt-ribbon-spring-boot-starter/pom.xml index e2c7dd9e..9bcc201a 100644 --- a/zlt-commons/zlt-ribbon-spring-boot-starter/pom.xml +++ b/zlt-commons/zlt-ribbon-spring-boot-starter/pom.xml @@ -28,5 +28,10 @@ org.apache.httpcomponents httpclient + + org.springframework.cloud + spring-cloud-starter-oauth2 + true + diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java new file mode 100644 index 00000000..bbbe6f54 --- /dev/null +++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/annotation/EnableFeignInterceptor.java @@ -0,0 +1,22 @@ +package com.central.common.ribbon.annotation; + +import com.central.common.ribbon.config.FeignInterceptorConfig; +import org.springframework.context.annotation.Import; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 在启动类上添加该注解来----开启自动登录用户对象注入 + * Token转化SysUser + * + * @author zlt + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Import(FeignInterceptorConfig.class) +public @interface EnableFeignInterceptor { + +} diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java new file mode 100644 index 00000000..9b0911f6 --- /dev/null +++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java @@ -0,0 +1,37 @@ +package com.central.common.ribbon.config; + +import feign.RequestInterceptor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +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.OAuth2Authentication; +import org.springframework.security.oauth2.provider.authentication.OAuth2AuthenticationDetails; + +/** + * feign拦截器 + * + * @author zlt + */ +public class FeignInterceptorConfig { + + /** + * 使用feign client访问别的微服务时,将access_token放入参数或者header ,Authorization:Bearer xxx + * 或者url?access_token=xxx + */ + @Bean + public RequestInterceptor requestInterceptor() { + RequestInterceptor requestInterceptor = template -> { + Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); + if (authentication != null) { + if (authentication instanceof OAuth2Authentication) { + OAuth2AuthenticationDetails details = (OAuth2AuthenticationDetails) authentication.getDetails(); + String access_token = details.getTokenValue(); + template.header("Authorization", OAuth2AccessToken.BEARER_TYPE + " " + access_token); + } + } + }; + return requestInterceptor; + } +} -- Gitee