kaiyu
2021-03-15 a3af0cf745d7a1632e2b803ca23387c226b13e29
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
package com.moral.util;
 
import com.moral.constant.Constants;
import com.moral.exception.TokenException;
import com.sun.org.apache.bcel.internal.classfile.ConstantString;
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;
 
/**
 * @ClassName tokenUtils
 * @Description token生成、解析工具类
 * @Author 陈凯裕
 * @Date 2021/3/10 11:35
 * @Version TODO
 **/
@Component
@Slf4j
public class TokenUtils {
 
    private static RedisTemplate redisTemplate;
 
    @Autowired
    @Qualifier("tokenRedisTemplate")
    public void setRedisTemplate(RedisTemplate redisTemplate) {
        TokenUtils.redisTemplate = redisTemplate;
    }
 
    //token有效期 单位:秒
    private static final int validity_time = 60*30;
 
    /**
     * @Description: 生成token,并且将用户信息存入缓存
     * @Param: [uid] type: 后台取值:manage  前台取值:api
     * @return: java.lang.String
     * @Author: 陈凯裕
     * @Date: 2021/3/10
     */
    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);
            //新token写入到Hash中
            redisTemplate.opsForHash().put("user_token", uid, token);
            return token;
        } catch (Exception e) {
            log.error("token生成异常:"+e.getMessage());
            throw new TokenException(Constants.CODE_TOKEN_CREATE_ERROR,Constants.MSG_TOKEN_CREATE_ERROR);
        }
    }
 
 
    /**
     * @Description: 校验token
     * @Param: [type, token] type: 后台取值:manage  前台取值:api
     * @return: java.util.Map<java.lang.String   ,   java.lang.Object>
     * @Author: 陈凯裕
     * @Date: 2021/3/10
     */
    public static void checkToken(String token) {
        try {
            String[] tokenArray = TokenEncryptUtils.decoded(token).split("/");
            //校验token是否合法
            if (tokenArray.length != 2) {
                throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
            }
            //校验token是否过期
            if (!redisTemplate.hasKey(token)) {
                throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
            }
        } catch (Exception e) {
            log.error("token工具类校验token异常" + e.getMessage());
            throw new TokenException(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
        }
    }
 
    /**
     * @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(Constants.CODE_TOKEN_ERROR,Constants.MSG_TOKEN_ERROR);
        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);
    }
}