deason 7 년 전
부모
커밋
d8d01c660b

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/app/controller/version_1_0/UserAuthRestController.java

@@ -51,7 +51,7 @@ public class UserAuthRestController {
             UserInfo userInfo = result.getData();
             UserToken userToken = new UserToken(account, password, accountType, rootOrgId, domain, userInfo.getKey(), userInfo.getToken());
             //将登录信息存放到Redis中
-            String sessionKey = StrUtils.uuid();
+            String sessionKey = StrUtils.md5Key(userInfo.getKey());
             redisService.cacheUserToken(sessionKey, userToken, REDIS_EXPIRE_TIME);
             log.debug("token:" + userToken.getToken() + " sessionKey:" + sessionKey);
             //替换Token为本地SessionKey

+ 13 - 0
src/main/java/cn/com/qmth/examcloud/app/core/utils/StrUtils.java

@@ -7,6 +7,8 @@
 
 package cn.com.qmth.examcloud.app.core.utils;
 
+import org.apache.commons.codec.digest.DigestUtils;
+
 import java.util.Random;
 import java.util.UUID;
 
@@ -39,4 +41,15 @@ public class StrUtils {
         return false;
     }
 
+    /**
+     * 将字符串MD5
+     */
+    public static String md5Key(String str) {
+        if (str == null) {
+            return null;
+        }
+        //MD5后字符串长度默认32位
+        return DigestUtils.md5Hex(str);
+    }
+
 }

+ 1 - 1
src/main/java/cn/com/qmth/examcloud/app/model/UserToken.java

@@ -37,7 +37,7 @@ public class UserToken implements Serializable {
         }
         Calendar c = Calendar.getInstance();
         c.setTime(createTime);
-        c.set(Calendar.HOUR, hours);
+        c.add(Calendar.HOUR, hours);
         //判断是否在n小时内
         if (c.getTime().after(new Date())) {
             return false;

+ 7 - 0
src/main/java/cn/com/qmth/examcloud/app/service/RedisService.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.app.service;
 
+import cn.com.qmth.examcloud.app.core.exception.ApiException;
 import cn.com.qmth.examcloud.app.core.utils.JsonMapper;
 import cn.com.qmth.examcloud.app.model.UserToken;
 import org.slf4j.Logger;
@@ -39,10 +40,16 @@ public class RedisService {
     }
 
     public void cacheUserToken(String key, UserToken value, long seconds) {
+        if (key == null) {
+            throw new ApiException("Key must be not null.");
+        }
         this.set(REDIS_KEY_PREFIX + key, new JsonMapper().toJson(value), seconds);
     }
 
     public UserToken getUserToken(String key) {
+        if (key == null) {
+            throw new ApiException("Key must be not null.");
+        }
         String value = this.get(REDIS_KEY_PREFIX + key);
         if (value != null) {
             return new JsonMapper().fromJson(value, UserToken.class);

+ 2 - 0
src/test/java/cn/com/qmth/examcloud/app/SimpleTest.java

@@ -1,5 +1,6 @@
 package cn.com.qmth.examcloud.app;
 
+import cn.com.qmth.examcloud.app.core.utils.StrUtils;
 import org.junit.Test;
 
 import java.time.Duration;
@@ -12,6 +13,7 @@ public class SimpleTest {
         System.out.println(Duration.ofMinutes(1));
         System.out.println(Duration.ofHours(1));
         System.out.println(Duration.ofDays(1));
+        System.out.println(StrUtils.md5Key("U_C_109_18809"));
     }
 
 }