Selaa lähdekoodia

优化认证逻辑

woody 8 kuukautta sitten
vanhempi
commit
5a61f1f298

+ 27 - 0
framework-redis/src/main/java/com/chelvc/framework/redis/context/RedisHashHolder.java

@@ -72,6 +72,33 @@ public final class RedisHashHolder {
         });
     }
 
+    /**
+     * 获取Redis Hash结构数据
+     *
+     * @param name Hash结构名称
+     * @param <HK> Hash结构键类型
+     * @param <HV> Hash结构值类型
+     * @return Hash键/值映射表
+     */
+    public static <HK, HV> Map<HK, HV> get(@NonNull String name) {
+        return get(RedisContextHolder.getRedisTemplate(), name);
+    }
+
+    /**
+     * 获取Redis Hash结构数据
+     *
+     * @param template Redis操作模版实例
+     * @param name     Hash结构名称
+     * @param <K>      Hash结构名称类型
+     * @param <HK>     Hash结构键类型
+     * @param <HV>     Hash结构值类型
+     * @return Hash键/值映射表
+     */
+    public static <K, HK, HV> Map<HK, HV> get(@NonNull RedisTemplate<K, ?> template, @NonNull K name) {
+        HashOperations<K, HK, HV> operations = template.opsForHash();
+        return operations.entries(name);
+    }
+
     /**
      * 获取Redis Hash结构数据
      *

+ 8 - 15
framework-security/src/main/java/com/chelvc/framework/security/token/RedisSessionValidator.java

@@ -1,13 +1,12 @@
 package com.chelvc.framework.security.token;
 
-import java.util.List;
+import java.util.Map;
 import java.util.Objects;
 
 import com.chelvc.framework.base.context.ApplicationContextHolder;
 import com.chelvc.framework.base.context.SessionContextHolder;
 import com.chelvc.framework.base.context.Using;
 import com.chelvc.framework.common.util.DateUtils;
-import com.chelvc.framework.common.util.ObjectUtils;
 import com.chelvc.framework.common.util.StringUtils;
 import com.chelvc.framework.redis.context.RedisContextHolder;
 import com.chelvc.framework.redis.context.RedisHashHolder;
@@ -42,18 +41,12 @@ public class RedisSessionValidator extends DefaultSessionValidator {
             return OAuth2TokenValidatorResult.success();
         }
 
-        // 从Redis获取令牌信息
+        // 校验令牌有效性
         Long id = this.getId(jwt);
-        String terminal = String.valueOf(SessionContextHolder.getTerminal());
         RedisTemplate<String, Object> template = RedisContextHolder.getDefaultTemplate();
-        List<?> values = RedisHashHolder.get(
-                template, AuthorizeContextHolder.key(id),
-                terminal, AuthorizeContextHolder.SCOPE, AuthorizeContextHolder.MOBILE,
-                AuthorizeContextHolder.REGISTERING
-        );
-
-        // 校验令牌有效性及应用范围
-        String token = ObjectUtils.get(values, 0), scope = this.getScope(jwt);
+        Map<String, ?> context = RedisHashHolder.get(template, AuthorizeContextHolder.key(id));
+        String terminal = String.valueOf(SessionContextHolder.getTerminal());
+        String token = (String) context.get(terminal), scope = this.getScope(jwt);
         if (StringUtils.isEmpty(token)) {
             throw new OAuth2AuthenticationException(new OAuth2Error(
                     "TOKEN_EXPIRED", ApplicationContextHolder.getMessage("Token.Expired"), null
@@ -65,7 +58,7 @@ public class RedisSessionValidator extends DefaultSessionValidator {
             ));
         } else if (this.properties.getAuthorize().isScoped()) {
             // 判断应用范围是否相同,如果不同则表示应用范围已被重置,需要刷新令牌
-            String real = ObjectUtils.get(values, 1);
+            String real = (String) context.get(AuthorizeContextHolder.SCOPE);
             if (!Objects.equals(scope, real)) {
                 String arg = StringUtils.ifEmpty(real, ApplicationContextHolder::getMessage);
                 String message = ApplicationContextHolder.getMessage("Scope.Changed", new Object[]{arg});
@@ -74,8 +67,8 @@ public class RedisSessionValidator extends DefaultSessionValidator {
         }
 
         // 更新会话主体信息
-        String mobile = ObjectUtils.get(values, 2);
-        Long registering = ObjectUtils.get(values, 3);
+        String mobile = (String) context.get(AuthorizeContextHolder.MOBILE);
+        Long registering = (Long) context.get(AuthorizeContextHolder.REGISTERING);
         long usage = RedisUserDailyHashHolder.increment(template, id, "usage");
         Using using = usage > 1 || registering == null ? Using.NORMAL :
                 registering >= DateUtils.today().getTime() ? Using.NEWLY : Using.DAILY;