提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.websocket.core.util; |
H |
2 |
|
|
3 |
import com.iailab.framework.security.core.LoginUser; |
|
4 |
import org.springframework.web.socket.WebSocketSession; |
|
5 |
|
|
6 |
import java.util.Map; |
|
7 |
|
|
8 |
/** |
|
9 |
* 专属于 web 包的工具类 |
|
10 |
* |
|
11 |
* @author iailab |
|
12 |
*/ |
|
13 |
public class WebSocketFrameworkUtils { |
|
14 |
|
|
15 |
public static final String ATTRIBUTE_LOGIN_USER = "LOGIN_USER"; |
|
16 |
|
|
17 |
/** |
|
18 |
* 设置当前用户 |
|
19 |
* |
|
20 |
* @param loginUser 登录用户 |
|
21 |
* @param attributes Session |
|
22 |
*/ |
|
23 |
public static void setLoginUser(LoginUser loginUser, Map<String, Object> attributes) { |
|
24 |
attributes.put(ATTRIBUTE_LOGIN_USER, loginUser); |
|
25 |
} |
|
26 |
|
|
27 |
/** |
|
28 |
* 获取当前用户 |
|
29 |
* |
|
30 |
* @return 当前用户 |
|
31 |
*/ |
|
32 |
public static LoginUser getLoginUser(WebSocketSession session) { |
|
33 |
return (LoginUser) session.getAttributes().get(ATTRIBUTE_LOGIN_USER); |
|
34 |
} |
|
35 |
|
|
36 |
/** |
|
37 |
* 获得当前用户的编号 |
|
38 |
* |
|
39 |
* @return 用户编号 |
|
40 |
*/ |
|
41 |
public static Long getLoginUserId(WebSocketSession session) { |
|
42 |
LoginUser loginUser = getLoginUser(session); |
|
43 |
return loginUser != null ? loginUser.getId() : null; |
|
44 |
} |
|
45 |
|
|
46 |
/** |
|
47 |
* 获得当前用户的类型 |
|
48 |
* |
|
49 |
* @return 用户编号 |
|
50 |
*/ |
|
51 |
public static Integer getLoginUserType(WebSocketSession session) { |
|
52 |
LoginUser loginUser = getLoginUser(session); |
|
53 |
return loginUser != null ? loginUser.getUserType() : null; |
|
54 |
} |
|
55 |
|
|
56 |
/** |
|
57 |
* 获得当前用户的租户编号 |
|
58 |
* |
|
59 |
* @param session Session |
|
60 |
* @return 租户编号 |
|
61 |
*/ |
|
62 |
public static Long getTenantId(WebSocketSession session) { |
|
63 |
LoginUser loginUser = getLoginUser(session); |
|
64 |
return loginUser != null ? loginUser.getTenantId() : null; |
|
65 |
} |
|
66 |
|
|
67 |
} |