|
@@ -40,14 +40,9 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
public static final String HEADER_ID = "Id";
|
|
public static final String HEADER_ID = "Id";
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 会话类型请求头
|
|
|
|
|
|
+ * 电话号码请求头
|
|
*/
|
|
*/
|
|
- public static final String HEADER_TYPE = "Type";
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 租户标识请求头
|
|
|
|
- */
|
|
|
|
- public static final String HEADER_TENANT = "Tenant";
|
|
|
|
|
|
+ public static final String HEADER_MOBILE = "Mobile";
|
|
|
|
|
|
/**
|
|
/**
|
|
* 设备标识请求头
|
|
* 设备标识请求头
|
|
@@ -55,15 +50,20 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
public static final String HEADER_DEVICE = "Device";
|
|
public static final String HEADER_DEVICE = "Device";
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 渠道来源请求头
|
|
|
|
|
|
+ * 租户标识请求头
|
|
*/
|
|
*/
|
|
- public static final String HEADER_CHANNEL = "Channel";
|
|
|
|
|
|
+ public static final String HEADER_TENANT = "Tenant";
|
|
|
|
|
|
/**
|
|
/**
|
|
* 是否首次请求头
|
|
* 是否首次请求头
|
|
*/
|
|
*/
|
|
public static final String HEADER_INITIAL = "Initial";
|
|
public static final String HEADER_INITIAL = "Initial";
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 渠道来源请求头
|
|
|
|
+ */
|
|
|
|
+ public static final String HEADER_CHANNEL = "Channel";
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 应用平台请求头
|
|
* 应用平台请求头
|
|
*/
|
|
*/
|
|
@@ -79,6 +79,11 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
*/
|
|
*/
|
|
public static final String HEADER_VERSION = "Version";
|
|
public static final String HEADER_VERSION = "Version";
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 业务类型请求头
|
|
|
|
+ */
|
|
|
|
+ public static final String HEADER_BUSINESS = "Business";
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 终端序号请求头
|
|
* 终端序号请求头
|
|
*/
|
|
*/
|
|
@@ -156,41 +161,41 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取当前会话类型
|
|
|
|
|
|
+ * 获取当前会话请求地址
|
|
*
|
|
*
|
|
- * @return 会话类型
|
|
|
|
|
|
+ * @return 请求地址
|
|
*/
|
|
*/
|
|
- public static Session.Type getType() {
|
|
|
|
- return getType(true);
|
|
|
|
|
|
+ public static String getHost() {
|
|
|
|
+ return getHost(true);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取当前会话类型
|
|
|
|
|
|
+ * 获取当前会话请求地址
|
|
*
|
|
*
|
|
* @param requireSession 会话是否是必须
|
|
* @param requireSession 会话是否是必须
|
|
- * @return 会话类型
|
|
|
|
|
|
+ * @return 请求地址
|
|
*/
|
|
*/
|
|
- public static Session.Type getType(boolean requireSession) {
|
|
|
|
- return ObjectUtils.ifNull(getSession(requireSession), Session::getType);
|
|
|
|
|
|
+ public static String getHost(boolean requireSession) {
|
|
|
|
+ return ObjectUtils.ifNull(getSession(requireSession), Session::getHost);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取当前会话请求地址
|
|
|
|
|
|
+ * 获取当前会话电话号码
|
|
*
|
|
*
|
|
- * @return 请求地址
|
|
|
|
|
|
+ * @return 电话号码
|
|
*/
|
|
*/
|
|
- public static String getHost() {
|
|
|
|
- return getHost(true);
|
|
|
|
|
|
+ public static String getMobile() {
|
|
|
|
+ return getMobile(true);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取当前会话请求地址
|
|
|
|
|
|
+ * 获取当前会话电话号码
|
|
*
|
|
*
|
|
* @param requireSession 会话是否是必须
|
|
* @param requireSession 会话是否是必须
|
|
- * @return 请求地址
|
|
|
|
|
|
+ * @return 电话号码
|
|
*/
|
|
*/
|
|
- public static String getHost(boolean requireSession) {
|
|
|
|
- return ObjectUtils.ifNull(getSession(requireSession), Session::getHost);
|
|
|
|
|
|
+ public static String getMobile(boolean requireSession) {
|
|
|
|
+ return ObjectUtils.ifNull(getSession(requireSession), Session::getMobile);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -232,41 +237,41 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取当前会话渠道来源
|
|
|
|
|
|
+ * 判断当前会话是否是首次请求
|
|
*
|
|
*
|
|
- * @return 渠道来源
|
|
|
|
|
|
+ * @return true/false
|
|
*/
|
|
*/
|
|
- public static String getChannel() {
|
|
|
|
- return getChannel(true);
|
|
|
|
|
|
+ public static boolean isInitial() {
|
|
|
|
+ return isInitial(true);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取当前会话渠道来源
|
|
|
|
|
|
+ * 判断当前会话是否是首次请求
|
|
*
|
|
*
|
|
* @param requireSession 会话是否是必须
|
|
* @param requireSession 会话是否是必须
|
|
- * @return 渠道来源
|
|
|
|
|
|
+ * @return true/false
|
|
*/
|
|
*/
|
|
- public static String getChannel(boolean requireSession) {
|
|
|
|
- return ObjectUtils.ifNull(getSession(requireSession), Session::getChannel);
|
|
|
|
|
|
+ public static boolean isInitial(boolean requireSession) {
|
|
|
|
+ return Boolean.TRUE.equals(ObjectUtils.ifNull(getSession(requireSession), Session::getInitial));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 判断当前会话是否是首次请求
|
|
|
|
|
|
+ * 获取当前会话渠道来源
|
|
*
|
|
*
|
|
- * @return true/false
|
|
|
|
|
|
+ * @return 渠道来源
|
|
*/
|
|
*/
|
|
- public static boolean isInitial() {
|
|
|
|
- return isInitial(true);
|
|
|
|
|
|
+ public static String getChannel() {
|
|
|
|
+ return getChannel(true);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 判断当前会话是否是首次请求
|
|
|
|
|
|
+ * 获取当前会话渠道来源
|
|
*
|
|
*
|
|
* @param requireSession 会话是否是必须
|
|
* @param requireSession 会话是否是必须
|
|
- * @return true/false
|
|
|
|
|
|
+ * @return 渠道来源
|
|
*/
|
|
*/
|
|
- public static boolean isInitial(boolean requireSession) {
|
|
|
|
- return Boolean.TRUE.equals(ObjectUtils.ifNull(getSession(requireSession), Session::getInitial));
|
|
|
|
|
|
+ public static String getChannel(boolean requireSession) {
|
|
|
|
+ return ObjectUtils.ifNull(getSession(requireSession), Session::getChannel);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -326,6 +331,25 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getVersion);
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getVersion);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取当前业务类型
|
|
|
|
+ *
|
|
|
|
+ * @return 业务类型
|
|
|
|
+ */
|
|
|
|
+ public static String getBusiness() {
|
|
|
|
+ return getBusiness(true);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取当前业务类型
|
|
|
|
+ *
|
|
|
|
+ * @param requireSession 会话是否是必须
|
|
|
|
+ * @return 业务类型
|
|
|
|
+ */
|
|
|
|
+ public static String getBusiness(boolean requireSession) {
|
|
|
|
+ return ObjectUtils.ifNull(getSession(requireSession), Session::getBusiness);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 获取当前会话签名信息
|
|
* 获取当前会话签名信息
|
|
*
|
|
*
|
|
@@ -559,30 +583,13 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 判断主体角色是否是管理员
|
|
|
|
|
|
+ * 判断是否是指定业务类型
|
|
*
|
|
*
|
|
|
|
+ * @param business 业务类型
|
|
* @return true/false
|
|
* @return true/false
|
|
*/
|
|
*/
|
|
- public static boolean isAdmin() {
|
|
|
|
- return ObjectUtils.ifNull(getSession(false), Session::getType) == Session.Type.ADMIN;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 判断主体是否是普通用户
|
|
|
|
- *
|
|
|
|
- * @return true/false
|
|
|
|
- */
|
|
|
|
- public static boolean isNormal() {
|
|
|
|
- return ObjectUtils.ifNull(getSession(false), Session::getType) == Session.Type.NORMAL;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 判断主体是否是客户端
|
|
|
|
- *
|
|
|
|
- * @return true/false
|
|
|
|
- */
|
|
|
|
- public static boolean isClient() {
|
|
|
|
- return ObjectUtils.ifNull(getSession(false), Session::getType) == Session.Type.CLIENT;
|
|
|
|
|
|
+ public static boolean isBusiness(String business) {
|
|
|
|
+ return StringUtils.nonEmpty(business) && Objects.equals(business, getBusiness(false));
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -652,21 +659,41 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
return ObjectUtils.ifNull(attributes, ServletRequestAttributes::getResponse);
|
|
return ObjectUtils.ifNull(attributes, ServletRequestAttributes::getResponse);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * 获取请求参数
|
|
|
|
+ *
|
|
|
|
+ * @param name 参数名称
|
|
|
|
+ * @return 参数值
|
|
|
|
+ */
|
|
|
|
+ public static String getParameter(@NonNull String name) {
|
|
|
|
+ return getParameter(name, value -> value);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取请求参数
|
|
|
|
+ *
|
|
|
|
+ * @param name 参数名称
|
|
|
|
+ * @param adapter 参数值适配器
|
|
|
|
+ * @return 参数值
|
|
|
|
+ */
|
|
|
|
+ public static <T> T getParameter(@NonNull String name, @NonNull Function<String, T> adapter) {
|
|
|
|
+ return StringUtils.ifEmpty(ObjectUtils.ifNull(getRequest(), request -> request.getParameter(name)), adapter);
|
|
|
|
+ }
|
|
|
|
+
|
|
/**
|
|
/**
|
|
* 初始化会话主体
|
|
* 初始化会话主体
|
|
*
|
|
*
|
|
- * @param id 主体标识
|
|
|
|
- * @param type 会话类型
|
|
|
|
- * @return 会话信息
|
|
|
|
|
|
+ * @param id 主体标识
|
|
|
|
+ * @param business 业务类型
|
|
|
|
+ * @param mobile 电话号码
|
|
*/
|
|
*/
|
|
- public static Session initializeSessionPrincipal(@NonNull Long id, @NonNull Session.Type type) {
|
|
|
|
|
|
+ public static void initializeSessionPrincipal(@NonNull Long id, @NonNull String business, String mobile) {
|
|
Session current = Objects.requireNonNull(SESSION_CONTEXT.get().poll(), "Session has not been initialized");
|
|
Session current = Objects.requireNonNull(SESSION_CONTEXT.get().poll(), "Session has not been initialized");
|
|
- Session session = Session.builder().id(id).type(type).host(current.getHost()).tenant(current.getTenant())
|
|
|
|
- .device(current.getDevice()).channel(current.getChannel()).version(current.getVersion())
|
|
|
|
- .initial(current.getInitial()).platform(current.getPlatform()).terminal(current.getTerminal())
|
|
|
|
- .signature(current.getSignature()).sequence(current.getSequence()).build();
|
|
|
|
|
|
+ Session session = Session.builder().id(id).host(current.getHost()).mobile(mobile).device(current.getDevice())
|
|
|
|
+ .tenant(current.getTenant()).initial(current.getInitial()).channel(current.getChannel())
|
|
|
|
+ .platform(current.getPlatform()).terminal(current.getTerminal()).version(current.getVersion())
|
|
|
|
+ .business(business).sequence(current.getSequence()).signature(current.getSignature()).build();
|
|
SESSION_CONTEXT.get().push(session);
|
|
SESSION_CONTEXT.get().push(session);
|
|
- return session;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -694,16 +721,6 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
return StringUtils.ifEmpty(request.getHeader(HEADER_ID), Long::parseLong);
|
|
return StringUtils.ifEmpty(request.getHeader(HEADER_ID), Long::parseLong);
|
|
}
|
|
}
|
|
|
|
|
|
- /**
|
|
|
|
- * 获取会话类型
|
|
|
|
- *
|
|
|
|
- * @param request Http请求对象
|
|
|
|
- * @return 会话类型
|
|
|
|
- */
|
|
|
|
- protected Session.Type getType(HttpServletRequest request) {
|
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_TYPE), Session.Type::valueOf);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* 获取客户端主机地址
|
|
* 获取客户端主机地址
|
|
*
|
|
*
|
|
@@ -715,13 +732,13 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取客户端租户标识
|
|
|
|
|
|
+ * 获取会话电话号码
|
|
*
|
|
*
|
|
* @param request Http请求对象
|
|
* @param request Http请求对象
|
|
- * @return 租户标识
|
|
|
|
|
|
+ * @return 电话号码
|
|
*/
|
|
*/
|
|
- protected Integer getTenant(HttpServletRequest request) {
|
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_TENANT), Integer::parseInt);
|
|
|
|
|
|
+ protected String getMobile(HttpServletRequest request) {
|
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_MOBILE), (String) null);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -735,23 +752,33 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取客户端渠道来源
|
|
|
|
|
|
+ * 获取客户端租户标识
|
|
*
|
|
*
|
|
* @param request Http请求对象
|
|
* @param request Http请求对象
|
|
- * @return 渠道来源
|
|
|
|
|
|
+ * @return 租户标识
|
|
*/
|
|
*/
|
|
- protected String getChannel(HttpServletRequest request) {
|
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_CHANNEL), (String) null);
|
|
|
|
|
|
+ protected Integer getTenant(HttpServletRequest request) {
|
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_TENANT), Integer::parseInt);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取客户端版本号
|
|
|
|
|
|
+ * 获取是否首次请求
|
|
*
|
|
*
|
|
* @param request Http请求对象
|
|
* @param request Http请求对象
|
|
- * @return 版本号
|
|
|
|
|
|
+ * @return true/false
|
|
*/
|
|
*/
|
|
- protected String getVersion(HttpServletRequest request) {
|
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_VERSION), (String) null);
|
|
|
|
|
|
+ protected Boolean getInitial(HttpServletRequest request) {
|
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_INITIAL), Boolean::parseBoolean);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取客户端渠道来源
|
|
|
|
+ *
|
|
|
|
+ * @param request Http请求对象
|
|
|
|
+ * @return 渠道来源
|
|
|
|
+ */
|
|
|
|
+ protected String getChannel(HttpServletRequest request) {
|
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_CHANNEL), (String) null);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -775,23 +802,33 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取签名信息
|
|
|
|
|
|
+ * 获取客户端版本号
|
|
*
|
|
*
|
|
* @param request Http请求对象
|
|
* @param request Http请求对象
|
|
- * @return 签名信息
|
|
|
|
|
|
+ * @return 版本号
|
|
*/
|
|
*/
|
|
- protected String getSignature(HttpServletRequest request) {
|
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_SIGNATURE), (String) null);
|
|
|
|
|
|
+ protected String getVersion(HttpServletRequest request) {
|
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_VERSION), (String) null);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
- * 获取是否首次请求
|
|
|
|
|
|
+ * 获取业务类型
|
|
*
|
|
*
|
|
* @param request Http请求对象
|
|
* @param request Http请求对象
|
|
- * @return true/false
|
|
|
|
|
|
+ * @return 业务类型
|
|
*/
|
|
*/
|
|
- protected Boolean getInitial(HttpServletRequest request) {
|
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_INITIAL), Boolean::parseBoolean);
|
|
|
|
|
|
+ protected String getBusiness(HttpServletRequest request) {
|
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_BUSINESS), (String) null);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取签名信息
|
|
|
|
+ *
|
|
|
|
+ * @param request Http请求对象
|
|
|
|
+ * @return 签名信息
|
|
|
|
+ */
|
|
|
|
+ protected String getSignature(HttpServletRequest request) {
|
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_SIGNATURE), (String) null);
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
/**
|
|
@@ -807,11 +844,12 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
@Override
|
|
@Override
|
|
public void requestInitialized(ServletRequestEvent event) {
|
|
public void requestInitialized(ServletRequestEvent event) {
|
|
HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
|
|
HttpServletRequest request = (HttpServletRequest) event.getServletRequest();
|
|
- Session session = Session.builder().id(this.getId(request)).type(this.getType(request))
|
|
|
|
- .host(this.getHost(request)).tenant(this.getTenant(request)).device(this.getDevice(request))
|
|
|
|
- .channel(this.getChannel(request)).version(this.getVersion(request)).initial(this.getInitial(request))
|
|
|
|
- .platform(this.getPlatform(request)).terminal(this.getTerminal(request))
|
|
|
|
- .signature(this.getSignature(request)).sequence(this.getSequence(request)).build();
|
|
|
|
|
|
+ Session session = Session.builder().id(this.getId(request)).host(this.getHost(request))
|
|
|
|
+ .mobile(this.getMobile(request)).device(this.getDevice(request)).tenant(this.getTenant(request))
|
|
|
|
+ .initial(this.getInitial(request)).channel(this.getChannel(request)).platform(this.getPlatform(request))
|
|
|
|
+ .terminal(this.getTerminal(request)).version(this.getVersion(request))
|
|
|
|
+ .business(this.getBusiness(request)).sequence(this.getSequence(request))
|
|
|
|
+ .signature(this.getSignature(request)).build();
|
|
setSession(session);
|
|
setSession(session);
|
|
}
|
|
}
|
|
|
|
|