提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.framework.web.core.util; |
H |
2 |
|
|
3 |
import cn.hutool.core.util.NumberUtil; |
|
4 |
import cn.hutool.extra.servlet.ServletUtil; |
|
5 |
import com.iailab.framework.common.enums.RpcConstants; |
|
6 |
import com.iailab.framework.common.enums.TerminalEnum; |
|
7 |
import com.iailab.framework.common.enums.UserTypeEnum; |
|
8 |
import com.iailab.framework.common.pojo.CommonResult; |
|
9 |
import com.iailab.framework.common.util.servlet.ServletUtils; |
|
10 |
import com.iailab.framework.web.config.WebProperties; |
|
11 |
import org.springframework.web.context.request.RequestAttributes; |
|
12 |
import org.springframework.web.context.request.RequestContextHolder; |
|
13 |
import org.springframework.web.context.request.ServletRequestAttributes; |
|
14 |
|
|
15 |
import javax.servlet.ServletRequest; |
|
16 |
import javax.servlet.http.HttpServletRequest; |
|
17 |
|
|
18 |
/** |
|
19 |
* 专属于 web 包的工具类 |
|
20 |
* |
|
21 |
* @author iailab |
|
22 |
*/ |
|
23 |
public class WebFrameworkUtils { |
|
24 |
|
|
25 |
private static final String REQUEST_ATTRIBUTE_LOGIN_USER_ID = "login_user_id"; |
|
26 |
private static final String REQUEST_ATTRIBUTE_LOGIN_USER_TYPE = "login_user_type"; |
|
27 |
|
|
28 |
private static final String REQUEST_ATTRIBUTE_COMMON_RESULT = "common_result"; |
|
29 |
|
|
30 |
public static final String HEADER_TENANT_ID = "tenant-id"; |
|
31 |
|
|
32 |
/** |
|
33 |
* 终端的 Header |
|
34 |
* |
|
35 |
* @see com.iailab.framework.common.enums.TerminalEnum |
|
36 |
*/ |
|
37 |
public static final String HEADER_TERMINAL = "terminal"; |
|
38 |
|
|
39 |
private static WebProperties properties; |
|
40 |
|
|
41 |
public WebFrameworkUtils(WebProperties webProperties) { |
|
42 |
WebFrameworkUtils.properties = webProperties; |
|
43 |
} |
|
44 |
|
|
45 |
/** |
|
46 |
* 获得租户编号,从 header 中 |
|
47 |
* 考虑到其它 framework 组件也会使用到租户编号,所以不得不放在 WebFrameworkUtils 统一提供 |
|
48 |
* |
|
49 |
* @param request 请求 |
|
50 |
* @return 租户编号 |
|
51 |
*/ |
|
52 |
public static Long getTenantId(HttpServletRequest request) { |
|
53 |
String tenantId = request.getHeader(HEADER_TENANT_ID); |
|
54 |
return NumberUtil.isNumber(tenantId) ? Long.valueOf(tenantId) : null; |
|
55 |
} |
|
56 |
|
|
57 |
public static void setLoginUserId(ServletRequest request, Long userId) { |
|
58 |
request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_ID, userId); |
|
59 |
} |
|
60 |
|
|
61 |
/** |
|
62 |
* 设置用户类型 |
|
63 |
* |
|
64 |
* @param request 请求 |
|
65 |
* @param userType 用户类型 |
|
66 |
*/ |
|
67 |
public static void setLoginUserType(ServletRequest request, Integer userType) { |
|
68 |
request.setAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_TYPE, userType); |
|
69 |
} |
|
70 |
|
|
71 |
/** |
|
72 |
* 获得当前用户的编号,从请求中 |
|
73 |
* 注意:该方法仅限于 framework 框架使用!!! |
|
74 |
* |
|
75 |
* @param request 请求 |
|
76 |
* @return 用户编号 |
|
77 |
*/ |
|
78 |
public static Long getLoginUserId(HttpServletRequest request) { |
|
79 |
if (request == null) { |
|
80 |
return null; |
|
81 |
} |
|
82 |
return (Long) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_ID); |
|
83 |
} |
|
84 |
|
|
85 |
/** |
|
86 |
* 获得当前用户的类型 |
|
87 |
* 注意:该方法仅限于 web 相关的 framework 组件使用!!! |
|
88 |
* |
|
89 |
* @param request 请求 |
|
90 |
* @return 用户编号 |
|
91 |
*/ |
|
92 |
public static Integer getLoginUserType(HttpServletRequest request) { |
|
93 |
if (request == null) { |
|
94 |
return null; |
|
95 |
} |
|
96 |
// 1. 优先,从 Attribute 中获取 |
|
97 |
Integer userType = (Integer) request.getAttribute(REQUEST_ATTRIBUTE_LOGIN_USER_TYPE); |
|
98 |
if (userType != null) { |
|
99 |
return userType; |
|
100 |
} |
|
101 |
// 2. 其次,基于 URL 前缀的约定 |
|
102 |
if (request.getServletPath().startsWith(properties.getAdminApi().getPrefix())) { |
|
103 |
return UserTypeEnum.ADMIN.getValue(); |
|
104 |
} |
|
105 |
if (request.getServletPath().startsWith(properties.getAppApi().getPrefix())) { |
|
106 |
return UserTypeEnum.MEMBER.getValue(); |
|
107 |
} |
|
108 |
return null; |
|
109 |
} |
|
110 |
|
|
111 |
public static Integer getLoginUserType() { |
|
112 |
HttpServletRequest request = getRequest(); |
|
113 |
return getLoginUserType(request); |
|
114 |
} |
|
115 |
|
|
116 |
public static Long getLoginUserId() { |
|
117 |
HttpServletRequest request = getRequest(); |
|
118 |
return getLoginUserId(request); |
|
119 |
} |
|
120 |
|
|
121 |
public static Integer getTerminal() { |
|
122 |
HttpServletRequest request = getRequest(); |
|
123 |
if (request == null) { |
|
124 |
return TerminalEnum.UNKNOWN.getTerminal(); |
|
125 |
} |
|
126 |
String terminalValue = request.getHeader(HEADER_TERMINAL); |
|
127 |
return NumberUtil.parseInt(terminalValue, TerminalEnum.UNKNOWN.getTerminal()); |
|
128 |
} |
|
129 |
|
|
130 |
public static void setCommonResult(ServletRequest request, CommonResult<?> result) { |
|
131 |
request.setAttribute(REQUEST_ATTRIBUTE_COMMON_RESULT, result); |
|
132 |
} |
|
133 |
|
|
134 |
public static CommonResult<?> getCommonResult(ServletRequest request) { |
|
135 |
return (CommonResult<?>) request.getAttribute(REQUEST_ATTRIBUTE_COMMON_RESULT); |
|
136 |
} |
|
137 |
|
|
138 |
public static HttpServletRequest getRequest() { |
|
139 |
RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); |
|
140 |
if (!(requestAttributes instanceof ServletRequestAttributes)) { |
|
141 |
return null; |
|
142 |
} |
|
143 |
ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes; |
|
144 |
return servletRequestAttributes.getRequest(); |
|
145 |
} |
|
146 |
|
|
147 |
/** |
|
148 |
* 判断是否为 RPC 请求 |
|
149 |
* |
|
150 |
* @param request 请求 |
|
151 |
* @return 是否为 RPC 请求 |
|
152 |
*/ |
|
153 |
public static boolean isRpcRequest(HttpServletRequest request) { |
|
154 |
return request.getRequestURI().startsWith(RpcConstants.RPC_API_PREFIX); |
|
155 |
} |
|
156 |
|
|
157 |
/** |
|
158 |
* 判断是否为 RPC 请求 |
|
159 |
* |
|
160 |
* 约定大于配置,只要以 Api 结尾,都认为是 RPC 接口 |
|
161 |
* |
|
162 |
* @param className 类名 |
|
163 |
* @return 是否为 RPC 请求 |
|
164 |
*/ |
|
165 |
public static boolean isRpcRequest(String className) { |
|
166 |
return className.endsWith("Api"); |
|
167 |
} |
|
168 |
|
|
169 |
} |