woody 2 жил өмнө
parent
commit
ad0fd15b83

+ 2 - 2
framework-base/src/main/java/com/chelvc/framework/base/context/ExecutorContextHolder.java

@@ -70,7 +70,7 @@ public class ExecutorContextHolder implements ApplicationContextAware {
      */
     public static CompletableFuture<Void> run(Executor executor, @NonNull Runnable runnable) {
         long id = Thread.currentThread().getId();
-        Session session = SessionContextHolder.getSession(true);
+        Session session = SessionContextHolder.getSession(false);
         Runnable function = () -> {
             if (id == Thread.currentThread().getId()) {
                 runnable.run();
@@ -113,7 +113,7 @@ public class ExecutorContextHolder implements ApplicationContextAware {
      */
     public static <T> CompletableFuture<T> supply(Executor executor, @NonNull Supplier<T> supplier) {
         long id = Thread.currentThread().getId();
-        Session session = SessionContextHolder.getSession(true);
+        Session session = SessionContextHolder.getSession(false);
         Supplier<T> function = () -> {
             if (id == Thread.currentThread().getId()) {
                 return supplier.get();

+ 140 - 55
framework-base/src/main/java/com/chelvc/framework/base/context/SessionContextHolder.java

@@ -110,7 +110,7 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 会话信息
      */
     public static Session getSession() {
-        return getSession(false);
+        return getSession(true);
     }
 
     /**
@@ -125,58 +125,53 @@ public class SessionContextHolder implements ServletRequestListener {
     /**
      * 获取会话信息
      *
-     * @param nullable 会话是否可以为空
+     * @param required 会话是否是必须
      * @return 会话信息
      */
-    public static Session getSession(boolean nullable) {
+    public static Session getSession(boolean required) {
         Session session = SESSION_CONTEXT.get().peek();
         if (session == EMPTY_SESSION) {
             session = null;
         }
-        return nullable ? session : Objects.requireNonNull(session, "Session has not been initialized");
+        return required ? Objects.requireNonNull(session, "Session has not been initialized") : session;
     }
 
     /**
-     * 获取会话属性
+     * 获取当前用户ID
      *
-     * @param function 会话属性方法
-     * @param <T>      会话对象属性类型
-     * @return 会话属性值
+     * @return 用户ID
      */
-    public static <T> T getSessionProperty(@NonNull Function<Session, T> function) {
-        return getSessionProperty(function, null);
+    public static Long getId() {
+        return getId(true);
     }
 
     /**
-     * 获取会话属性
+     * 获取当前用户ID
      *
-     * @param function     会话属性方法
-     * @param defaultValue 默认值
-     * @param <T>          会话对象属性类型
-     * @return 会话属性值
+     * @param requireSession 会话是否是必须
+     * @return 用户ID
      */
-    public static <T> T getSessionProperty(@NonNull Function<Session, T> function, T defaultValue) {
-        return ObjectUtils.ifNull(ObjectUtils.ifNull(getSession(true), function), defaultValue);
+    public static Long getId(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getId);
     }
 
     /**
-     * 获取当前用户ID
+     * 获取当前会话类型
      *
-     * @return 用户ID
+     * @return 会话类型
      */
-    public static Long getId() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getId);
+    public static Session.Type getType() {
+        return getType(true);
     }
 
     /**
      * 获取当前会话类型
      *
+     * @param requireSession 会话是否是必须
      * @return 会话类型
      */
-    public static Session.Type getType() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getType);
+    public static Session.Type getType(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getType);
     }
 
     /**
@@ -185,8 +180,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 请求地址
      */
     public static String getHost() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getHost);
+        return getHost(true);
+    }
+
+    /**
+     * 获取当前会话请求地址
+     *
+     * @param requireSession 会话是否是必须
+     * @return 请求地址
+     */
+    public static String getHost(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getHost);
     }
 
     /**
@@ -195,8 +199,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 设备标识
      */
     public static String getDevice() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getDevice);
+        return getDevice(true);
+    }
+
+    /**
+     * 获取当前会话设备标识
+     *
+     * @param requireSession 会话是否是必须
+     * @return 设备标识
+     */
+    public static String getDevice(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getDevice);
     }
 
     /**
@@ -205,8 +218,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 租户编号
      */
     public static Integer getTenant() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getTenant);
+        return getTenant(true);
+    }
+
+    /**
+     * 获取当前会话租户编号
+     *
+     * @param requireSession 会话是否是必须
+     * @return 租户编号
+     */
+    public static Integer getTenant(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getTenant);
     }
 
     /**
@@ -215,8 +237,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 渠道来源
      */
     public static String getChannel() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getChannel);
+        return getChannel(true);
+    }
+
+    /**
+     * 获取当前会话渠道来源
+     *
+     * @param requireSession 会话是否是必须
+     * @return 渠道来源
+     */
+    public static String getChannel(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getChannel);
     }
 
     /**
@@ -225,8 +256,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return true/false
      */
     public static boolean isInitial() {
-        Session session = getSession(true);
-        return Boolean.TRUE.equals(ObjectUtils.ifNull(session, Session::getInitial));
+        return isInitial(true);
+    }
+
+    /**
+     * 判断当前会话是否是首次请求
+     *
+     * @param requireSession 会话是否是必须
+     * @return true/false
+     */
+    public static boolean isInitial(boolean requireSession) {
+        return Boolean.TRUE.equals(ObjectUtils.ifNull(getSession(requireSession), Session::getInitial));
     }
 
     /**
@@ -235,8 +275,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 平台信息
      */
     public static Platform getPlatform() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getPlatform);
+        return getPlatform(true);
+    }
+
+    /**
+     * 获取当前会话平台信息
+     *
+     * @param requireSession 会话是否是必须
+     * @return 平台信息
+     */
+    public static Platform getPlatform(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getPlatform);
     }
 
     /**
@@ -245,8 +294,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 终端信息
      */
     public static Terminal getTerminal() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getTerminal);
+        return getTerminal(true);
+    }
+
+    /**
+     * 获取当前会话终端信息
+     *
+     * @param requireSession 会话是否是必须
+     * @return 终端信息
+     */
+    public static Terminal getTerminal(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getTerminal);
     }
 
     /**
@@ -255,8 +313,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 版本信息
      */
     public static String getVersion() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getVersion);
+        return getVersion(true);
+    }
+
+    /**
+     * 获取当前会话版本信息
+     *
+     * @param requireSession 会话是否是必须
+     * @return 版本信息
+     */
+    public static String getVersion(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getVersion);
     }
 
     /**
@@ -265,8 +332,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 签名信息
      */
     public static String getSignature() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getSignature);
+        return getSignature(true);
+    }
+
+    /**
+     * 获取当前会话签名信息
+     *
+     * @param requireSession 会话是否是必须
+     * @return 签名信息
+     */
+    public static String getSignature(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getSignature);
     }
 
     /**
@@ -275,8 +351,17 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return 序列号
      */
     public static Integer getSequence() {
-        Session session = getSession(true);
-        return ObjectUtils.ifNull(session, Session::getSequence);
+        return getSequence(true);
+    }
+
+    /**
+     * 获取当前终端序列号
+     *
+     * @param requireSession 会话是否是必须
+     * @return 序列号
+     */
+    public static Integer getSequence(boolean requireSession) {
+        return ObjectUtils.ifNull(getSession(requireSession), Session::getSequence);
     }
 
     /**
@@ -359,7 +444,7 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return true/false
      */
     public static boolean isPlatform(Platform platform) {
-        return platform != null && platform == getSessionProperty(Session::getPlatform);
+        return platform != null && platform == getPlatform(false);
     }
 
     /**
@@ -369,7 +454,7 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return true/false
      */
     public static boolean isTerminal(Terminal terminal) {
-        return terminal != null && terminal == getSessionProperty(Session::getTerminal);
+        return terminal != null && terminal == getTerminal(false);
     }
 
     /**
@@ -383,7 +468,7 @@ public class SessionContextHolder implements ServletRequestListener {
         if (terminal == null || StringUtils.isEmpty(version)) {
             return false;
         }
-        Session session = getSession(true);
+        Session session = getSession(false);
         return terminal == ObjectUtils.ifNull(session, Session::getTerminal)
                 && version.equals(ObjectUtils.ifNull(session, Session::getVersion));
     }
@@ -398,7 +483,7 @@ public class SessionContextHolder implements ServletRequestListener {
         if (version == null) {
             return false;
         }
-        Session session = getSession(true);
+        Session session = getSession(false);
         String channel = ObjectUtils.ifNull(session, Session::getChannel);
         Platform platform = ObjectUtils.ifNull(session, Session::getPlatform);
         Terminal terminal = ObjectUtils.ifNull(session, Session::getTerminal);
@@ -440,7 +525,7 @@ public class SessionContextHolder implements ServletRequestListener {
         if (terminal == null || StringUtils.isEmpty(version)) {
             return false;
         }
-        Session session = getSession(true);
+        Session session = getSession(false);
         return terminal == ObjectUtils.ifNull(session, Session::getTerminal)
                 && Version.isBefore(ObjectUtils.ifNull(session, Session::getVersion), version, contains);
     }
@@ -468,7 +553,7 @@ public class SessionContextHolder implements ServletRequestListener {
         if (terminal == null || StringUtils.isEmpty(version)) {
             return false;
         }
-        Session session = getSession(true);
+        Session session = getSession(false);
         return terminal == ObjectUtils.ifNull(session, Session::getTerminal)
                 && Version.isAfter(ObjectUtils.ifNull(session, Session::getVersion), version, contains);
     }
@@ -479,7 +564,7 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return true/false
      */
     public static boolean isAdmin() {
-        return ObjectUtils.ifNull(getSession(true), Session::getType) == Session.Type.ADMIN;
+        return ObjectUtils.ifNull(getSession(false), Session::getType) == Session.Type.ADMIN;
     }
 
     /**
@@ -488,7 +573,7 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return true/false
      */
     public static boolean isNormal() {
-        return ObjectUtils.ifNull(getSession(true), Session::getType) == Session.Type.NORMAL;
+        return ObjectUtils.ifNull(getSession(false), Session::getType) == Session.Type.NORMAL;
     }
 
     /**
@@ -497,7 +582,7 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return true/false
      */
     public static boolean isClient() {
-        return ObjectUtils.ifNull(getSession(true), Session::getType) == Session.Type.CLIENT;
+        return ObjectUtils.ifNull(getSession(false), Session::getType) == Session.Type.CLIENT;
     }
 
     /**
@@ -537,7 +622,7 @@ public class SessionContextHolder implements ServletRequestListener {
      * @return true/false
      */
     public static boolean isTesting(Platform platform, Terminal terminal) {
-        Session session = getSession(true);
+        Session session = getSession(false);
         if (session == null) {
             return false;
         }

+ 1 - 1
framework-base/src/main/java/com/chelvc/framework/base/util/HttpUtils.java

@@ -226,7 +226,7 @@ public final class HttpUtils {
      * @return 消息
      */
     public static String message(HttpServletRequest request, Object type, String message) {
-        Session session = SessionContextHolder.getSession(true);
+        Session session = SessionContextHolder.getSession(false);
         Long id = ObjectUtils.ifNull(session, Session::getId);
         Platform platform = ObjectUtils.ifNull(session, Session::getPlatform);
         Terminal terminal = ObjectUtils.ifNull(session, Session::getTerminal);

+ 74 - 6
framework-base/src/main/java/com/chelvc/framework/base/util/StringUtils.java

@@ -137,12 +137,32 @@ public final class StringUtils {
                     "\\|\\、\\|\\;\\:\\;\\:\\'\"\\,\\.\\,\\。\\<\\>\\《\\》\\/\\?\\?A-Za-z0-9\u4e00-\u9fa5\n ]";
 
     /**
-     * 基础的随机串种子
+     * 默认随机字符串长度
+     */
+    public static final int DEFAULT_RANDOM_LENGTH = 4;
+
+    /**
+     * 默认密钥字符串长度
+     */
+    public static final int DEFAULT_SECRET_LENGTH = 16;
+
+    /**
+     * 基础的随机串字符
      */
     private static final char[] BASIC_RANDOM_CHARS = new char[]{
-            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
-            'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
-            'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'
+            'V', 'u', 's', 'r', 'l', 'g', 'T', 'z', 'H', 'v', '5', '0', 'P', 'm', '9', 'j', 'x', 'X', 'K', 'w', '8',
+            'M', 'd', 'S', 'F', 'e', 'W', '7', 'G', '4', 'y', 'B', 'R', 'Z', 'h', 'U', 'D', 'L', 'A', 'a', 'f', 'N',
+            'k', '6', '2', 'C', 'c', '3', 'p', 'Y', 'Q', 'E', 'q', 't', 'b', '1', 'n', 'J'
+    };
+
+    /**
+     * 标准的随机串字符
+     */
+    private static final char[] STANDARD_RANDOM_CHARS = {
+            'd', 'j', '9', '0', 'e', 'n', 'x', 'A', 'h', 'Y', 'B', '3', '2', 's', 'Q', 'R', 'w', 'c', 'm', '4', '^',
+            'V', 'F', 'v', 'U', 'l', 'H', 'W', 'L', '&', 'G', '@', '1', 'J', '#', 'a', 'C', '7', 'N', '|', 'g', 'K',
+            'T', 'X', 'u', 'b', 'k', 'P', 'q', 'Z', 'f', '%', 't', 'M', 'y', '*', 'z', 'E', 'S', 'r', '8', 'p', '~',
+            'D', '6', '!', '$', '5'
     };
 
     /**
@@ -899,7 +919,7 @@ public final class StringUtils {
      * @return 随机字符串
      */
     public static String random() {
-        return random(4);
+        return random(DEFAULT_RANDOM_LENGTH);
     }
 
     /**
@@ -919,7 +939,7 @@ public final class StringUtils {
      * @return 随机字符串
      */
     public static String random(@NonNull char[] chars) {
-        return random(chars, 4);
+        return random(chars, DEFAULT_RANDOM_LENGTH);
     }
 
     /**
@@ -940,6 +960,35 @@ public final class StringUtils {
         return new String(array);
     }
 
+    /**
+     * 生成密钥字符串
+     *
+     * @return 密钥字符串
+     */
+    public static String secret() {
+        return secret(DEFAULT_SECRET_LENGTH);
+    }
+
+    /**
+     * 生成密钥字符串
+     *
+     * @param length 密钥长度
+     * @return 密钥字符串
+     */
+    public static String secret(int length) {
+        return random(STANDARD_RANDOM_CHARS, length);
+    }
+
+    /**
+     * 生成密钥字符串
+     *
+     * @param chars 密钥字符数组
+     * @return 密钥字符串
+     */
+    public static String secret(@NonNull char[] chars) {
+        return random(chars, DEFAULT_SECRET_LENGTH);
+    }
+
     /**
      * 获取UUID字符串
      *
@@ -959,4 +1008,23 @@ public final class StringUtils {
         String uuid = UUID.randomUUID().toString();
         return horizontal ? uuid : uuid.replace(HORIZONTAL, EMPTY);
     }
+
+    /**
+     * 生成随机序列号
+     *
+     * @param length 序列号长度
+     * @return 随机序列号
+     */
+    public static String sequence(int length) {
+        Assert.isTrue(length > 0, "length must be greater than 0");
+        char[] sequence = new char[length];
+        for (int i = 0, count = ObjectUtils.group(length, 8); i < count; i++) {
+            String uuid = uuid();
+            for (int j = 0, n = i * 8 + j; j < 8 && n < length; j++, n++) {
+                int number = Integer.parseInt(uuid.substring(j * 4, j * 4 + 4), 16);
+                sequence[n] = BASIC_RANDOM_CHARS[number % BASIC_RANDOM_CHARS.length];
+            }
+        }
+        return new String(sequence);
+    }
 }

+ 3 - 4
framework-database/src/main/java/com/chelvc/framework/database/context/DatabaseContextHolder.java

@@ -26,7 +26,6 @@ import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.function.Executor;
 import com.chelvc.framework.base.function.Handler;
 import com.chelvc.framework.base.function.Provider;
-import com.chelvc.framework.base.model.Session;
 import com.chelvc.framework.base.util.AESUtils;
 import com.chelvc.framework.base.util.ErrorUtils;
 import com.chelvc.framework.base.util.ObjectUtils;
@@ -280,7 +279,7 @@ public class DatabaseContextHolder implements ApplicationContextAware {
         }
         Creatable<?> creatable = (Creatable<?>) entity;
         if (creatable.getCreator() == null) {
-            creatable.setCreator(SessionContextHolder.getSessionProperty(Session::getId));
+            creatable.setCreator(SessionContextHolder.getId(false));
         }
         if (creatable.getCreateTime() == null) {
             creatable.setCreateTime(new Date());
@@ -299,7 +298,7 @@ public class DatabaseContextHolder implements ApplicationContextAware {
             return;
         }
         Updatable<?> updatable = (Updatable<?>) entity;
-        Long operator = SessionContextHolder.getSessionProperty(Session::getId);
+        Long operator = SessionContextHolder.getId(false);
         if (operator != null) {
             updatable.setUpdater(operator);
         }
@@ -318,7 +317,7 @@ public class DatabaseContextHolder implements ApplicationContextAware {
             return;
         }
         Isolatable<?> isolatable = (Isolatable<?>) entity;
-        isolatable.setTenant(SessionContextHolder.getSessionProperty(Session::getTenant));
+        isolatable.setTenant(SessionContextHolder.getTenant(false));
     }
 
     /**

+ 1 - 2
framework-database/src/main/java/com/chelvc/framework/database/interceptor/TenantIsolateHandler.java

@@ -3,7 +3,6 @@ package com.chelvc.framework.database.interceptor;
 import com.baomidou.mybatisplus.core.metadata.TableInfo;
 import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
 import com.chelvc.framework.base.context.SessionContextHolder;
-import com.chelvc.framework.base.model.Session;
 import com.chelvc.framework.database.config.MybatisPlusConfigurer;
 import com.chelvc.framework.database.context.DatabaseContextHolder;
 import com.chelvc.framework.database.entity.Isolatable;
@@ -35,7 +34,7 @@ public class TenantIsolateHandler implements TenantLineHandler {
 
     @Override
     public boolean ignoreTable(String tableName) {
-        if (SessionContextHolder.getSessionProperty(Session::getTenant) != null) {
+        if (SessionContextHolder.getTenant(false) != null) {
             TableInfo table = DatabaseContextHolder.getTable(tableName);
             return table == null || !Isolatable.class.isAssignableFrom(table.getEntityType());
         }

+ 1 - 1
framework-dubbo/src/main/java/com/chelvc/framework/dubbo/interceptor/DubboConsumerInterceptor.java

@@ -51,7 +51,7 @@ public class DubboConsumerInterceptor implements Filter {
     public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
         // 初始化会话信息
         RpcContext.getClientAttachment().setObjectAttachment(
-                Session.class.getName(), SessionContextHolder.getSession(true)
+                Session.class.getName(), SessionContextHolder.getSession(false)
         );
 
         // 查找对象适配器

+ 1 - 1
framework-rocketmq/src/main/java/com/chelvc/framework/rocketmq/config/MessageConverterWrapper.java

@@ -84,7 +84,7 @@ public class MessageConverterWrapper implements MessageConverter {
     @Override
     public Message<?> toMessage(Object payload, MessageHeaders headers) {
         Object target = JacksonContextHolder.serialize(payload);
-        Session session = SessionContextHolder.getSession(true);
+        Session session = SessionContextHolder.getSession(false);
         MessageWrapper wrapper = MessageWrapper.builder().target(target).session(session).build();
         return this.target.toMessage(wrapper, headers);
     }

+ 3 - 3
framework-security/src/main/java/com/chelvc/framework/security/config/MethodSecurityExpressionWrapper.java

@@ -144,7 +144,7 @@ public class MethodSecurityExpressionWrapper implements MethodSecurityExpression
         if (platforms == null || platforms.length == 0) {
             return false;
         }
-        Platform current = SessionContextHolder.getSessionProperty(Session::getPlatform);
+        Platform current = SessionContextHolder.getPlatform(false);
         return Stream.of(platforms).filter(Objects::nonNull).anyMatch(platform -> Objects.equals(platform, current));
     }
 
@@ -158,7 +158,7 @@ public class MethodSecurityExpressionWrapper implements MethodSecurityExpression
         if (terminals == null || terminals.length == 0) {
             return false;
         }
-        Terminal current = SessionContextHolder.getSessionProperty(Session::getTerminal);
+        Terminal current = SessionContextHolder.getTerminal(false);
         return Stream.of(terminals).filter(Objects::nonNull).anyMatch(terminal -> Objects.equals(terminal, current));
     }
 
@@ -172,7 +172,7 @@ public class MethodSecurityExpressionWrapper implements MethodSecurityExpression
         if (versions == null || versions.length == 0) {
             return false;
         }
-        String current = SessionContextHolder.getSessionProperty(Session::getVersion);
+        String current = SessionContextHolder.getVersion(false);
         return Stream.of(versions).filter(Objects::nonNull).anyMatch(version -> Objects.equals(version, current));
     }
 

+ 1 - 2
framework-security/src/main/java/com/chelvc/framework/security/token/RedisTokenActiveValidator.java

@@ -3,7 +3,6 @@ package com.chelvc.framework.security.token;
 import java.util.Objects;
 
 import com.chelvc.framework.base.context.SessionContextHolder;
-import com.chelvc.framework.base.model.Session;
 import com.chelvc.framework.base.model.Terminal;
 import com.chelvc.framework.base.util.StringUtils;
 import com.chelvc.framework.security.config.WebSecurityConfigurer;
@@ -38,7 +37,7 @@ public class RedisTokenActiveValidator implements TokenActiveValidator {
     public OAuth2TokenValidatorResult validate(@NonNull Jwt jwt) {
         // 验证Redis中是否存在该令牌,同时忽略Redis请求/操作异常(为了提高系统可用性)
         String token = null;
-        Terminal terminal = SessionContextHolder.getSessionProperty(Session::getTerminal);
+        Terminal terminal = SessionContextHolder.getTerminal(false);
         String key = TokenUtils.key(terminal, TokenUtils.getId(jwt));
         if (log.isDebugEnabled()) {
             log.debug("Validating redis token key: {}", key);

+ 2 - 3
framework-security/src/main/java/com/chelvc/framework/security/token/RedisTokenStore.java

@@ -4,7 +4,6 @@ import java.util.Date;
 import java.util.concurrent.TimeUnit;
 
 import com.chelvc.framework.base.context.SessionContextHolder;
-import com.chelvc.framework.base.model.Session;
 import com.chelvc.framework.base.model.Terminal;
 import com.chelvc.framework.security.util.TokenUtils;
 import lombok.NonNull;
@@ -64,13 +63,13 @@ public class RedisTokenStore extends JwtTokenStore {
 
     @Override
     public void storeAccessToken(OAuth2AccessToken token, OAuth2Authentication authentication) {
-        Terminal terminal = SessionContextHolder.getSessionProperty(Session::getTerminal);
+        Terminal terminal = SessionContextHolder.getTerminal(false);
         this.save(terminal, TokenUtils.getId(token), token.getValue(), token.getExpiration());
     }
 
     @Override
     public void removeAccessToken(OAuth2AccessToken token) {
-        Terminal terminal = SessionContextHolder.getSessionProperty(Session::getTerminal);
+        Terminal terminal = SessionContextHolder.getTerminal(false);
         this.remove(terminal, TokenUtils.getId(token));
     }
 }