From 726b056fc2d3b51acdeb0b5fbaf74c8886acc2ac Mon Sep 17 00:00:00 2001 From: kaiyu <404897439@qq.com> Date: Fri, 23 Apr 2021 14:49:40 +0800 Subject: [PATCH] screen-manage 更新时间格式 --- screen-common/src/main/java/com/moral/util/TokenUtils.java | 143 ++++++++++++++++++++++++++++++++++------------- 1 files changed, 102 insertions(+), 41 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..9bc1d5e 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,17 @@ 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 org.springframework.util.ObjectUtils; -import java.util.HashMap; -import java.util.Map; import java.util.concurrent.TimeUnit; /** @@ -21,7 +24,6 @@ @Component @Slf4j public class TokenUtils { - private static RedisTemplate redisTemplate; @Autowired @@ -31,37 +33,47 @@ } //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); + //redisTemplate.expire(token, validity_time, TimeUnit.SECONDS); + //���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 @@ -70,36 +82,85 @@ * @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/4/1 + */ + public static void destoryToken(String token) { + destoryToken(getUidByToken(token), token); + } + + /** + * @Description: ������TOKEN������Id + * @Param: [token] + * @return: void + * @Author: ��������� + * @Date: 2021/4/1 + */ + public static String getUidByToken(String token) { + String[] string = TokenEncryptUtils.decoded(token).split("/"); + return string[0]; + } + + + /** + * @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