From 247f19f6c3f17c2ac2f37b55c7d0550731f31ffe Mon Sep 17 00:00:00 2001
From: kaiyu <404897439@qq.com>
Date: Thu, 25 Mar 2021 17:24:16 +0800
Subject: [PATCH] Merge remote-tracking branch 'origin/dev' into dev

---
 screen-common/src/main/java/com/moral/util/TokenUtils.java |  115 +++++++++++++++++++++++++++++++++++++--------------------
 1 files changed, 75 insertions(+), 40 deletions(-)

diff --git a/screen-common/src/main/java/com/moral/util/TokenUtils.java b/screen-common/src/main/java/com/moral/util/TokenUtils.java
index 2fb6132..d1aa0aa 100644
--- a/screen-common/src/main/java/com/moral/util/TokenUtils.java
+++ b/screen-common/src/main/java/com/moral/util/TokenUtils.java
@@ -1,14 +1,16 @@
 package com.moral.util;
 
+import com.moral.constant.ResponseCodeEnum;
+import com.moral.exception.TokenException;
+
 import lombok.extern.slf4j.Slf4j;
+
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.beans.factory.annotation.Qualifier;
 import org.springframework.data.redis.core.RedisTemplate;
 import org.springframework.stereotype.Component;
 
 
-import java.util.HashMap;
-import java.util.Map;
 import java.util.concurrent.TimeUnit;
 
 /**
@@ -21,7 +23,6 @@
 @Component
 @Slf4j
 public class TokenUtils {
-
     private static RedisTemplate redisTemplate;
 
     @Autowired
@@ -31,75 +32,109 @@
     }
 
     //token��������� ������������
-    private static final int validity_time = 60*60*24*7;
-    //token������,������������
-    public static final int error = -1;
-    //token������
-    public static final int timeout = -2;
-    //token���������������������
-    public static final int valid = 1;
+    private static final int validity_time = 60*30;
 
+    //������user_token���������������������token
+    public static boolean hHasKey(String uid){
+        return redisTemplate.opsForHash().hasKey("user_token",uid);
+    }
+
+    //������������id������token
+    public static Object hget(String uid){
+        return redisTemplate.opsForHash().get("user_token",uid);
+    }
     /**
-     * @Description: ������token
-     * @Param: [type, uid] type��� ���������������manage  ���������������api
+     * @Description: ������token,���������������������������������
+     * @Param: [uid] type��� ���������������manage  ���������������api
      * @return: java.lang.String
      * @Author: ���������
      * @Date: 2021/3/10
      */
-    public static Map<String, Object> getToken(String uid, Map<String, Object> userInfo) {
-        Map<String, Object> result = new HashMap<>();
+    public static String getToken(String uid, Object userInfo) {
         //������������token
         try {
+            //������token
             String token = TokenEncryptUtils.encoded(uid + "/" + System.currentTimeMillis() / 1000);
+            //������������token
+            String oldToken = (String) redisTemplate.opsForHash().get("user_token", uid);
+            if (oldToken != null)
+                redisTemplate.delete(oldToken);
+            //���token���������value���
             redisTemplate.opsForValue().set(token, userInfo);
             redisTemplate.expire(token, validity_time, TimeUnit.SECONDS);
-            result.put("code", valid);
-            result.put("token",token);
-        }catch (Exception e){
-            e.printStackTrace();
-            log.error(e.getMessage());
-            result.put("code",error);
+            //���token���������Hash���
+            redisTemplate.opsForHash().put("user_token", uid, token);
+            return token;
+        } catch (Exception e) {
+            log.error("token���������������"+e.getMessage());
+            throw new TokenException(ResponseCodeEnum.TOKEN_CREATE_ERROR.getCode(),
+                    ResponseCodeEnum.TOKEN_CREATE_ERROR.getMsg());
         }
-        return result;
     }
+
 
     /**
      * @Description: ������token
      * @Param: [type, token] type��� ���������������manage  ���������������api
-     * @return: java.util.Map<java.lang.String                               ,                               java.lang.Object>
+     * @return: java.util.Map<java.lang.String   ,   java.lang.Object>
      * @Author: ���������
      * @Date: 2021/3/10
      */
-    public static Map<String, Object> checkToken( String token) {
-        Map<String, Object> result = new HashMap<>();
+    public static void checkToken(String token) {
         try {
             String[] tokenArray = TokenEncryptUtils.decoded(token).split("/");
             //������token������������
             if (tokenArray.length != 2) {
-                result.put("code", error);
-                result.put("msg", "���������token");
-                return result;
+                throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
+                        ResponseCodeEnum.TOKEN_INVALID.getMsg());
             }
             //������token������������
-            int tokenTime = Integer.parseInt(tokenArray[1]);
-            if ((System.currentTimeMillis() / 1000) - tokenTime > validity_time) {
-                result.put("code", timeout);
-                result.put("msg", "���������������������������������������");
-                return result;
+            if (!redisTemplate.hasKey(token)) {
+                throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
+                        ResponseCodeEnum.TOKEN_INVALID.getMsg());
             }
-            result.put("code", valid);
-            return result;
         } catch (Exception e) {
             log.error("token���������������token������" + e.getMessage());
-            result.put("code", error);
-            result.put("msg", "���������token");
-            return result;
+            throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
+                    ResponseCodeEnum.TOKEN_INVALID.getMsg());
         }
     }
 
-    //������token������������������
-    public static Map<String, Object> getUserInfoByToken(String token) {
-        Map<String, Object> userInfo = (Map<String, Object>) redisTemplate.opsForValue().get(token);
+    /**
+     * @Description: ������token������������������  ������������
+     * @Param: [token]
+     * @return: java.util.Map<java.lang.String   ,   java.lang.Object>
+     * @Author: ���������
+     * @Date: 2021/3/11
+     */
+    public static Object getUserInfoByToken(String token) {
+        Object userInfo = redisTemplate.opsForValue().get(token);
+        if(userInfo==null)
+            throw new TokenException(ResponseCodeEnum.TOKEN_INVALID.getCode(),
+                    ResponseCodeEnum.TOKEN_INVALID.getMsg());
         return userInfo;
     }
+
+    /**
+     * @Description: ������token
+     * @Param: [uid, token]
+     * @return: void
+     * @Author: ���������
+     * @Date: 2021/3/11
+     */
+    public static void destoryToken(String uid, String token) {
+        redisTemplate.delete(token);
+        redisTemplate.opsForHash().delete("user_token", uid);
+    }
+
+    /**
+     * @Description: token������
+     * @Param: [token]
+     * @return: void
+     * @Author: ���������
+     * @Date: 2021/3/11
+     */
+    public static void extendTokenTime(String token)  {
+        redisTemplate.expire(token, validity_time, TimeUnit.SECONDS);
+    }
 }

--
Gitblit v1.8.0