From 91a2f7b16b178b62c1f282ad80f0d8c3774e9f2f Mon Sep 17 00:00:00 2001 From: RuoYi Date: Mon, 30 Nov 2020 15:09:59 +0800 Subject: [PATCH] =?UTF-8?q?=E8=8E=B7=E5=8F=96=E8=AF=B7=E6=B1=82token?= =?UTF-8?q?=E6=96=B9=E6=B3=95=E7=A7=BB=E8=87=B3=E6=9D=83=E9=99=90=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/common/core/constant/CacheConstants.java | 5 +++++ .../com/ruoyi/common/core/utils/SecurityUtils.java | 22 ++++++++++++++++++++++ .../security/feign/FeignRequestInterceptor.java | 5 +++++ .../common/security/service/TokenService.java | 16 ++-------------- 4 files changed, 34 insertions(+), 14 deletions(-) diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheConstants.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheConstants.java index c6db347..8f666dd 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheConstants.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/constant/CacheConstants.java @@ -31,4 +31,9 @@ public class CacheConstants * 用户名字段 */ public static final String DETAILS_USERNAME = "username"; + + /** + * 授权信息字段 + */ + public static final String AUTHORIZATION_HEADER = "authorization"; } diff --git a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java index dbba358..3c5bf20 100644 --- a/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java +++ b/ruoyi-common/ruoyi-common-core/src/main/java/com/ruoyi/common/core/utils/SecurityUtils.java @@ -1,5 +1,6 @@ package com.ruoyi.common.core.utils; +import javax.servlet.http.HttpServletRequest; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.text.Convert; @@ -28,6 +29,27 @@ public class SecurityUtils } /** + * 获取请求token + */ + public static String getToken() + { + return getToken(ServletUtils.getRequest()); + } + + /** + * 根据request获取请求token + */ + public static String getToken(HttpServletRequest request) + { + String token = ServletUtils.getRequest().getHeader(CacheConstants.HEADER); + if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX)) + { + token = token.replace(CacheConstants.TOKEN_PREFIX, ""); + } + return token; + } + + /** * 是否为管理员 * * @param userId 用户ID diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/feign/FeignRequestInterceptor.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/feign/FeignRequestInterceptor.java index 1e1dbf7..2642dd1 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/feign/FeignRequestInterceptor.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/feign/FeignRequestInterceptor.java @@ -35,6 +35,11 @@ public class FeignRequestInterceptor implements RequestInterceptor { requestTemplate.header(CacheConstants.DETAILS_USERNAME, userName); } + String authentication = headers.get(CacheConstants.AUTHORIZATION_HEADER); + if (StringUtils.isNotEmpty(authentication)) + { + requestTemplate.header(CacheConstants.AUTHORIZATION_HEADER, authentication); + } } } } \ No newline at end of file diff --git a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java index 0772abf..a4e8428 100644 --- a/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java +++ b/ruoyi-common/ruoyi-common-security/src/main/java/com/ruoyi/common/security/service/TokenService.java @@ -9,6 +9,7 @@ import org.springframework.stereotype.Component; import com.ruoyi.common.core.constant.CacheConstants; import com.ruoyi.common.core.constant.Constants; import com.ruoyi.common.core.utils.IdUtils; +import com.ruoyi.common.core.utils.SecurityUtils; import com.ruoyi.common.core.utils.ServletUtils; import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.ip.IpUtils; @@ -71,7 +72,7 @@ public class TokenService public LoginUser getLoginUser(HttpServletRequest request) { // 获取请求携带的令牌 - String token = getToken(request); + String token = SecurityUtils.getToken(request); if (StringUtils.isNotEmpty(token)) { String userKey = getTokenKey(token); @@ -119,17 +120,4 @@ public class TokenService { return ACCESS_TOKEN + token; } - - /** - * 获取请求token - */ - private String getToken(HttpServletRequest request) - { - String token = request.getHeader(CacheConstants.HEADER); - if (StringUtils.isNotEmpty(token) && token.startsWith(CacheConstants.TOKEN_PREFIX)) - { - token = token.replace(CacheConstants.TOKEN_PREFIX, ""); - } - return token; - } } \ No newline at end of file