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
package com.yanzuoguang.token;
import com.yanzuoguang.util.YzgError;
import com.yanzuoguang.util.base.ObjectHelper;
import com.yanzuoguang.util.cache.MemoryCache;
import com.yanzuoguang.util.helper.JsonHelper;
import com.yanzuoguang.util.helper.StringHelper;
import com.yanzuoguang.util.vo.MapRow;
/**
* 登录请求
*
* @author 颜佐光
*/
public class TokenHelper {
/**
* 动态加载token支持对象
*/
private static TokenLoad tokenLoad = null;
/**
* 获取动态加载支持对象
*
* @return 动态加载支持对象
*/
public static TokenLoad getTokenLoad() {
return tokenLoad;
}
/**
* 设置动态加载支持对象
*
* @param tokenLoad 设置值
*/
public static void setTokenLoad(TokenLoad tokenLoad) {
TokenHelper.tokenLoad = tokenLoad;
}
/**
* 内存缓存
*/
protected final static MemoryCache<TokenData> cache = new MemoryCache<>();
/**
* 获取当前线程编号
*
* @return 线程编号
*/
private static String getCurrentId() {
return String.valueOf(Thread.currentThread().getId());
}
/**
* 获取当前的登录信息
*
* @param cls 需要获取的数据的类型
* @param <T> 数据类型
* @return 缓存的数据
*/
public static <T extends Object> T get(Class<T> cls) {
return get(true, cls);
}
/**
* 获取当前的登录信息
*
* @param checkFlag 是否抛出异常
* @param cls 需要获取的数据的类型
* @param <T> 数据类型
* @return 缓存的数据
*/
public static <T extends Object> T get(boolean checkFlag, Class<T> cls) {
return get(checkFlag, cls, true);
}
/**
* 获取当前的登录信息
*
* @param checkFlag 是否抛出异常
* @param cls 需要获取的数据的类型
* @param <T> 数据类型
* @return 缓存的数据
*/
public static <T extends Object> T get(boolean checkFlag, Class<T> cls, boolean readNewToken) {
String id = getCurrentId();
TokenData tokenData = cache.get(id);
if (tokenData == null) {
if (checkFlag) {
throw YzgError.getRuntimeException("061");
}
return null;
}
// 获取最新的token
if (readNewToken) {
boolean isEmpty = tokenData.getData() == null || tokenData.getData().isEmpty();
boolean isExpire = tokenData.getExpire() < System.currentTimeMillis();
boolean isToken = !StringHelper.isEmpty(tokenData.getToken()) && (isEmpty || isExpire);
if (isToken) {
tokenData = tokenLoad.load(tokenData.getToken());
if (tokenData != null) {
cache.put(id, tokenData);
}
}
if (tokenData == null || tokenData.getData() == null) {
if (checkFlag) {
throw YzgError.getRuntimeException("061");
}
return null;
}
}
if (ObjectHelper.isSub(cls, tokenData.getData().getClass())) {
return (T) tokenData.getData();
} else {
String json = JsonHelper.serialize(tokenData.getData());
return JsonHelper.deserialize(json, cls);
}
}
/**
* 获取字段值
*
* @param cls 字段值类型
* @param field 字段值
* @param <T> 类型
* @return 对象
*/
public static <T extends Object> T getField(Class<T> cls, String field) {
return getField(true, cls, field);
}
/**
* 获取字段值
*
* @param checkFlag 是否抛出异常
* @param cls 字段值类型
* @param field 字段值
* @param <T> 类型
* @return 对象
*/
public static <T extends Object> T getField(boolean checkFlag, Class<T> cls, String field) {
MapRow row = get(checkFlag, MapRow.class);
Object value = null;
if (row.containsKey(field)) {
value = row.get(field);
}
return StringHelper.to(cls, value);
}
/**
* 缓存数据
*
* @param token 标记
* @param data 数据时间
*/
public static void write(String token, Object data) {
write(token, StringHelper.EMPTY, 0, data);
}
/**
* 缓存数据
*
* @param token 标记
* @param expire 有效期
* @param data 数据时间
*/
public static void write(String token, long expire, Object data) {
write(token, StringHelper.EMPTY, expire, data);
}
/**
* 缓存数据
*
* @param token 标记
* @param expire 有效期
* @param dataPwd 数据密码
* @param data 数据时间
*/
public static void write(String token, String dataPwd, long expire, Object data) {
String id = getCurrentId();
TokenData tokenData = cache.get(id);
if (tokenData == null) {
tokenData = new TokenData();
cache.put(id, tokenData);
}
tokenData.setToken(token);
tokenData.setDataPwd(dataPwd);
if (expire > 0) {
tokenData.setExpire(expire);
}
if (tokenData.getData() == null) {
tokenData.setData(new MapRow());
}
ObjectHelper.writeWithFrom(tokenData.getData(), data);
tokenData.setWrite(true);
}
/**
* 设置登录标记字符串(用于初始化设置,本次修改不会写入到数据库)
*
* @param tokenString 登录标记字符串
*/
public static TokenData setTokenString(String tokenString) {
String id = getCurrentId();
TokenData tokenData = JsonHelper.deserialize(tokenString, TokenData.class);
cache.put(id, tokenData);
tokenData.setWrite(false);
return tokenData;
}
/**
* 获取登录标记字符串,自动要删除数据
*
* @return 登录标记字符粗
*/
public static String getTokenString() {
String id = getCurrentId();
TokenData tokenData = cache.get(id);
if (tokenData == null) {
return StringHelper.EMPTY;
}
return JsonHelper.serialize(tokenData);
}
/**
* 删除缓存信息
*/
public static void remove() {
String id = getCurrentId();
cache.remove(id);
}
}