935090232@qq.com
2021-03-11 dce21b3131e81663ccd0291b7e96542d3f1cec7e
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
129
130
131
132
package com.matrix.component.redis;
 
import com.google.gson.Gson;
import com.matrix.core.constance.SystemErrorCode;
import com.matrix.core.exception.GlobleException;
import com.matrix.core.tools.LogUtil;
import com.matrix.core.tools.StringUtils;
import com.matrix.core.tools.UUIDUtil;
import com.matrix.core.tools.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
 
 
/**
 * 用户登录信息保存在redis上
 *
 * @author JIANGYOUYAO
 * @email 935090232@qq.com
 * @date 2018年1月19日
 */
@Component
public class RedisUserLoginUtils {
 
    @Autowired
    RedisClient redisClient;
    private static final String TOKEN = "token";
 
    @Value("${debug}")
    private String isDebug;
 
    /**
     * 判断用户是否已经登录过
     *
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     *            会从http请求投中获取token进行验证
     * @return
     *
     */
    public boolean isUserLogin() {
        String token = getUserToken();
 
        if (StringUtils.isNotBlank(token)) {
            String user = redisClient.getCachedValue(token);
            LogUtil.debug("redis 获取到token={},用户={}", token, user);
            return user != null;
        } else {
            LogUtil.debug("redis token未获取到token");
            return false;
        }
    }
 
 
 
 
    public String getUserToken() {
        String token = WebUtil.getRequest().getHeader("token");
        // debug模式可以从url参数中获取token
        if (StringUtils.isBlank(token) && "true".equals(isDebug)) {
            if (token == null) {
                token = WebUtil.getRequest().getParameter("token");
            }
        }
        return token;
    }
 
    /**
     * 获取登录用户信息,如果 获取失败将会抛出异常
     *
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     * @return
     */
    public <T> T getLoginUser(Class<T> clazz) {
        String userToken = getUserToken();
        LogUtil.debug("redisClient获取到用户token={}", userToken);
        if (userToken != null) {
            String json = redisClient.getCachedValue(userToken);
            LogUtil.info("从redis中获取的值为:"+json);
            if(StringUtils.isNotBlank(json)){
                //重新设置key过期时间
                redisClient.resetExpire(userToken);
                Gson g = new Gson();
                return g.fromJson(json, clazz);
            }else{
                throw new GlobleException(SystemErrorCode.REQUEST_INVALID);
            }
 
        } else {
            throw new GlobleException(SystemErrorCode.REQUEST_INVALID);
        }
    }
 
 
    /**
     * 在redis中保存用户信息,并且返回保存的token
     *
     * @author JIANGYOUYAO
     * @email 935090232@qq.com
     * @date 2018年1月19日
     * @return 返回保存用户信息的token
     */
    public String saveUserInfo(Object obj) {
        String token = UUIDUtil.getRandomID();
 
        redisClient.saveValue(token, obj);
        LogUtil.debug("redis 保存用户返回token={}", token);
        return token;
    }
 
 
    /**
     * 用户退出
     *
     * @author:吕敬瑛
     * @date:2018年1月19日下午3:17:14
     */
    public void loginOut() {
        String token = getUserToken();
        redisClient.removeObject(token);
    }
 
 
    public void updateUserInfo(Object obj) {
        String token = getUserToken();
        redisClient.saveValue(token, obj);
        LogUtil.debug("更新redis用户");
    }
}