-
Notifications
You must be signed in to change notification settings - Fork 266
Expand file tree
/
Copy pathLocalUser.java
More file actions
46 lines (38 loc) · 942 Bytes
/
LocalUser.java
File metadata and controls
46 lines (38 loc) · 942 Bytes
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
package io.github.talelin.latticy.common;
import io.github.talelin.latticy.model.UserDO;
/**
* 线程安全的当前登录用户,如果用户未登录,则得到 null
*
* @author pedro@TaleLin
*/
public class LocalUser {
private LocalUser() {
throw new IllegalStateException("Utility class");
}
private static final ThreadLocal<UserDO> LOCAL = new ThreadLocal<>();
/**
* 得到当前登录用户
*
* @return user | null
*/
public static UserDO getLocalUser() {
return LocalUser.LOCAL.get();
}
/**
* 设置登录用户
*
* @param user user
*/
public static void setLocalUser(UserDO user) {
LocalUser.LOCAL.set(user);
}
public static <T> T getLocalUser(Class<T> clazz) {
return (T) LOCAL.get();
}
/**
* 清理当前用户
*/
public static void clearLocalUser() {
LOCAL.remove();
}
}