86183
2022-09-09 0d999e33085c0a25c5525242748f6aa62a401159
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package com.dsh.utils.login;
 
import cn.mb.cloud.common.core.constant.enums.ErrorCodeConstants;
import cn.mb.cloud.common.core.exception.BusinessException;
import com.dsh.constant.AuthConstants;
import com.dsh.constant.MsgConstants;
 
 
import com.dsh.upms.model.bo.UserDetailBo;
import com.dsh.upms.model.vo.LoginUserVo;
import com.dsh.utils.CacheUtils;
import com.dsh.utils.DateUtils;
import com.dsh.utils.ObjectUtils;
import com.dsh.utils.ServletUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.Assert;
 
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.UUID;
 
/**
 * * @author: lihong .
 * * @email: 765907456@qq.com .
 * * @createTime:  19-7-15 上午11:44 .
 * * description: 登录帮助工具.
 **/
@Slf4j
public final class LoginHelper {
 
 
    /**
     * admin后台操作记录userId Key
     */
    public static final String ADMINUSERID = "adminUserId";
 
    /**
     * 获取用户.
     *
     * @return
     */
    public static LoginUserVo getUser() throws BusinessException {
        String authorization = getCurrentUserToken();
        if (StringUtils.isEmpty(authorization)) {
            throw new BusinessException("您还未登录",
                    ErrorCodeConstants.FAIL.getValue());
        }
        return getUserByCache(authorization.replace(AuthConstants.BASIC_BEARER_HEADER, ""));
    }
 
    /**
     * 转换
     *
     * @param convert
     * @param types
     * @param <T>
     * @return
     */
    public static <T> T convert(Convert<T> convert, Type... types) {
        return convert.convert(ObjectUtils.newInstance((Class) ObjectUtils.buildType(types)));
    }
 
    /**
     * 获取当前推送设备号.
     *
     * @return
     */
    public static String getCurrentPushDevice() {
        HttpServletRequest httpServletRequest = ServletUtils.getHttpServletRequest();
        String jPushDevice = httpServletRequest.getHeader(AuthConstants.J_PUSH_DEVICE);
        return jPushDevice;
    }
 
    /**
     * token生成.
     *
     * @return
     */
    public static String tokenGenerate() {
        return UUID.randomUUID().toString().replace("-", "");
    }
 
    /**
     * 更新用户.
     */
    public static void update(LoginUserVo loginUserVo, long expire) throws BusinessException {
        Assert.notNull(loginUserVo, "用户信息不能为空.");
        //更新缓存中用户信息
        long diff = DateUtils.diff(loginUserVo.getStartTime(), LocalDateTime.now());
        if (diff < loginUserVo.getExpire()) {
            log.debug("{}更新了token缓存时间", loginUserVo.getName());
            CacheUtils.put(loginUserVo.getToken(), loginUserVo, expire, CacheUtils.CacheTimeUnit.MILLISECONDS);
        } else {
            CacheUtils.delete(loginUserVo.getToken());
            throw new BusinessException(MsgConstants.TOKEN_EXPIRE);
        }
 
    }
 
    /**
     * 添加用户
     *
     * @param userBo
     * @param token
     * @param expire
     */
    public static LoginUserVo addUser(UserDetailBo userBo, String token, Long expire) {
        Assert.notNull(userBo, "用户信息不能为空.");
        Assert.hasText(token, "令牌不能为空.");
        Assert.notNull(expire, "过期时间不能为空");
        LoginUserVo loginUserVo = covertUser(userBo, null);
        loginUserVo.setToken(token);
        loginUserVo.setExpire(expire);
        loginUserVo.setStartTime(LocalDateTime.now());
        CacheUtils.put(token, loginUserVo, expire, CacheUtils.CacheTimeUnit.MILLISECONDS);
        return loginUserVo;
    }
 
 
    /**
     * 移除用户信息
     */
    public static void remove() {
        String authorization = getCurrentUserToken();
        CacheUtils.delete(authorization);
    }
 
    public static void remove(String tomke) {
        CacheUtils.delete(tomke);
    }
 
    /**
     * 用户是否存在.
     *
     * @param token
     * @return
     */
    public static boolean userExist(String token) {
        if (StringUtils.isNotEmpty(token)) {
            return CacheUtils.exist(token);
        } else {
            return false;
        }
    }
 
    /**
     * 查询当前用户是否存在.
     *
     * @return
     */
    public static boolean userExist() throws BusinessException {
        String authorization = getCurrentUserToken();
        if (StringUtils.isEmpty(authorization)) {
            throw new BusinessException(MsgConstants.USER_NOT_HAS_AUTH);
        }
 
        return userExist(authorization);
    }
 
    /**
     * 获取用户当前的设备号
     *
     * @return
     */
    public static String getCurrentDevice() {
        HttpServletRequest request = ServletUtils.getHttpServletRequest();
        return request.getHeader(AuthConstants.DEVICE_HEADER);
    }
 
    /**
     * 设备号是否存在.
     *
     * @return
     */
    public static boolean deviceExist() {
        return true;
    }
 
    /**
     * 转换用户信息.
     *
     * @param userBo
     * @param loginUserVo
     * @return
     */
    public static LoginUserVo covertUser(UserDetailBo userBo, LoginUserVo loginUserVo) {
        if (loginUserVo == null) {
            loginUserVo = new LoginUserVo();
        }
        loginUserVo.setUsername(userBo.getUsername())
                .setName(userBo.getName())
                .setLockFlag(userBo.getLockFlag())
                .setId(userBo.getId());
        return loginUserVo;
    }
 
 
    /**
     * 从缓存中获取用户
     *
     * @param token
     * @return
     */
    public static LoginUserVo getUserByCache(String token) {
        if (StringUtils.isNotEmpty(token)) {
            Boolean hasUser = CacheUtils.exist(token);
            if (hasUser != null && hasUser) {
                return CacheUtils.get(token);
            }
        }
        return null;
    }
 
    /**
     * 获取当前用户token
     *
     * @return
     */
    private static String getCurrentUserToken() {
        try {
            HttpServletRequest request = ServletUtils.getHttpServletRequest();
            //获取是否存在
            String authorization = request.getHeader(AuthConstants.AUTH_HEADER);
            return authorization;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
 
    public static boolean Logout() {
        String authorization = getCurrentUserToken();
        String token = authorization.replace("Bearer ", "");
        if (CacheUtils.getRedisTemplate().hasKey(token)) {
            CacheUtils.getRedisTemplate().delete(token);
            return true;
        }
        return false;
    }
}