From 951c3647278a51933e3dbdc30a889fefb7b3574a Mon Sep 17 00:00:00 2001 From: caozehui <2427765068@qq.com> Date: Thu, 27 Feb 2025 11:10:12 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BE=AE=E8=B0=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/njcn/web/utils/RequestUtil.java | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/RequestUtil.java b/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/RequestUtil.java index c93f9cf..3f30e2a 100644 --- a/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/RequestUtil.java +++ b/njcn-springboot/spingboot2.3.12/src/main/java/com/njcn/web/utils/RequestUtil.java @@ -2,7 +2,6 @@ package com.njcn.web.utils; import cn.hutool.core.util.StrUtil; import cn.hutool.extra.spring.SpringUtil; -import cn.hutool.json.JSON; import cn.hutool.json.JSONObject; import cn.hutool.json.JSONUtil; import com.njcn.common.bean.CustomCacheUtil; @@ -27,10 +26,10 @@ public class RequestUtil { HttpServletRequest request = HttpServletUtil.getRequest(); String accessTokenStr = request.getHeader(SecurityConstants.AUTHORIZATION_KEY); String accessToken = null; - if (StrUtil.isNotBlank(accessTokenStr)) { + if (StrUtil.isNotBlank(accessTokenStr) && accessTokenStr.startsWith(SecurityConstants.AUTHORIZATION_PREFIX)) { accessToken = accessTokenStr.replace(SecurityConstants.AUTHORIZATION_PREFIX, Strings.EMPTY); } - return accessToken; + return "".equals(accessToken)? null : accessToken; } /** @@ -60,9 +59,10 @@ public class RequestUtil { /** * 获取当前登录用户的用户名 + * * @return 若成功,返回当前登录用户的用户名;若失败,返回null */ - public static String getUserName(){ + public static String getUserName() { String userJson = getUserJson(); String username = null; if (StrUtil.isNotBlank(userJson)) { @@ -74,15 +74,16 @@ public class RequestUtil { /** * 从缓冲中获取当前登录用户的JSON格式的用户信息 + * * @return 若成功,返回当前登录用户的JSON格式的用户信息;若失败,返回null */ - public static String getUserJson(){ + public static String getUserJson() { String accessToken = getAccessToken(); String userJson = null; if (StrUtil.isNotBlank(accessToken)) { CustomCacheUtil customCacheUtil = SpringUtil.getBean(CustomCacheUtil.CACHE_NAME); - userJson = customCacheUtil.get(accessToken,false); + userJson = customCacheUtil.get(accessToken, false); } - return userJson; + return userJson; } }