|
@@ -3,10 +3,7 @@ package com.chelvc.framework.base.context;
|
|
|
import java.io.IOException;
|
|
|
import java.io.OutputStream;
|
|
|
import java.util.ArrayDeque;
|
|
|
-import java.util.Collection;
|
|
|
import java.util.Deque;
|
|
|
-import java.util.HashMap;
|
|
|
-import java.util.Map;
|
|
|
import java.util.Objects;
|
|
|
import java.util.Set;
|
|
|
import java.util.function.Function;
|
|
@@ -18,6 +15,7 @@ import javax.servlet.http.HttpServletResponse;
|
|
|
import com.chelvc.framework.base.annotation.Versioning;
|
|
|
import com.chelvc.framework.base.model.Result;
|
|
|
import com.chelvc.framework.base.model.Session;
|
|
|
+import com.chelvc.framework.base.model.Using;
|
|
|
import com.chelvc.framework.base.util.HttpUtils;
|
|
|
import com.chelvc.framework.common.model.Platform;
|
|
|
import com.chelvc.framework.common.model.Terminal;
|
|
@@ -30,7 +28,6 @@ import org.apache.commons.lang3.ArrayUtils;
|
|
|
import org.springframework.http.HttpStatus;
|
|
|
import org.springframework.http.MediaType;
|
|
|
import org.springframework.stereotype.Component;
|
|
|
-import org.springframework.util.CollectionUtils;
|
|
|
import org.springframework.web.context.request.NativeWebRequest;
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
@@ -49,9 +46,9 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
public static final String HEADER_ID = "Id";
|
|
|
|
|
|
/**
|
|
|
- * 应用范围请求头
|
|
|
+ * 使用信息请求头
|
|
|
*/
|
|
|
- public static final String HEADER_SCOPE = "Scope";
|
|
|
+ public static final String HEADER_USING = "Using";
|
|
|
|
|
|
/**
|
|
|
* 电话号码请求头
|
|
@@ -64,9 +61,9 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
public static final String HEADER_DEVICE = "Device";
|
|
|
|
|
|
/**
|
|
|
- * 是否首次请求头
|
|
|
+ * 业务类型请求头
|
|
|
*/
|
|
|
- public static final String HEADER_INITIAL = "Initial";
|
|
|
+ public static final String HEADER_BUSINESS = "Business";
|
|
|
|
|
|
/**
|
|
|
* 渠道来源请求头
|
|
@@ -88,11 +85,6 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
*/
|
|
|
public static final String HEADER_VERSION = "Version";
|
|
|
|
|
|
- /**
|
|
|
- * 授权信息请求头
|
|
|
- */
|
|
|
- public static final String HEADER_AUTHORITY = "Authority";
|
|
|
-
|
|
|
/**
|
|
|
* 时间戳请求头
|
|
|
*/
|
|
@@ -119,14 +111,50 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
private static final Session EMPTY_SESSION = new Session();
|
|
|
|
|
|
/**
|
|
|
- * 对象缓存映射上下文
|
|
|
+ * 用户会话上下文
|
|
|
*/
|
|
|
- private static final ThreadLocal<Map<Object, Object>> CACHE_CONTEXT = ThreadLocal.withInitial(HashMap::new);
|
|
|
+ private static final ThreadLocal<Deque<Session>> SESSION_CONTEXT = ThreadLocal.withInitial(ArrayDeque::new);
|
|
|
|
|
|
/**
|
|
|
- * 用户会话上下文
|
|
|
+ * 获取当前请求对象
|
|
|
+ *
|
|
|
+ * @return Http请求对象
|
|
|
*/
|
|
|
- private static final ThreadLocal<Deque<Session>> SESSION_CONTEXT = ThreadLocal.withInitial(ArrayDeque::new);
|
|
|
+ public static HttpServletRequest getRequest() {
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ return ObjectUtils.ifNull(attributes, ServletRequestAttributes::getRequest);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取当前响应对象
|
|
|
+ *
|
|
|
+ * @return Http响应对象
|
|
|
+ */
|
|
|
+ public static HttpServletResponse getResponse() {
|
|
|
+ ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
+ 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);
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 获取会话信息
|
|
@@ -179,6 +207,45 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getId);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取客户端身份标识
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 身份标识
|
|
|
+ */
|
|
|
+ public static Long getId(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_ID), Long::parseLong);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取使用信息
|
|
|
+ *
|
|
|
+ * @return 使用信息
|
|
|
+ */
|
|
|
+ public static Using getUsing() {
|
|
|
+ return getUsing(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取使用信息
|
|
|
+ *
|
|
|
+ * @param requireSession 会话是否是必须
|
|
|
+ * @return 使用信息
|
|
|
+ */
|
|
|
+ public static Using getUsing(boolean requireSession) {
|
|
|
+ return ObjectUtils.ifNull(getSession(requireSession), Session::getUsing);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取使用信息
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 使用信息
|
|
|
+ */
|
|
|
+ public static Using getUsing(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_USING), Using::valueOf);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前会话请求地址
|
|
|
*
|
|
@@ -199,22 +266,13 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前应用范围
|
|
|
- *
|
|
|
- * @return 应用范围
|
|
|
- */
|
|
|
- public static String getScope() {
|
|
|
- return getScope(true);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取当前应用范围
|
|
|
+ * 获取客户端主机地址
|
|
|
*
|
|
|
- * @param requireSession 会话是否是必须
|
|
|
- * @return 应用范围
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 主机地址
|
|
|
*/
|
|
|
- public static String getScope(boolean requireSession) {
|
|
|
- return ObjectUtils.ifNull(getSession(requireSession), Session::getScope);
|
|
|
+ public static String getHost(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(HttpUtils.getRequestAddress(request), (String) null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -236,6 +294,16 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getMobile);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取会话电话号码
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 电话号码
|
|
|
+ */
|
|
|
+ public static String getMobile(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_MOBILE), (String) null);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前会话设备标识
|
|
|
*
|
|
@@ -255,6 +323,45 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getDevice);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取客户端设备标识
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 设备标识
|
|
|
+ */
|
|
|
+ public static String getDevice(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_DEVICE), (String) null);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取业务类型
|
|
|
+ *
|
|
|
+ * @return 业务类型
|
|
|
+ */
|
|
|
+ public static String getBusiness() {
|
|
|
+ return getBusiness(true);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取业务类型
|
|
|
+ *
|
|
|
+ * @param requireSession 会话是否是必须
|
|
|
+ * @return 业务类型
|
|
|
+ */
|
|
|
+ public static String getBusiness(boolean requireSession) {
|
|
|
+ return ObjectUtils.ifNull(getSession(requireSession), Session::getBusiness);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取业务类型
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 业务类型
|
|
|
+ */
|
|
|
+ public static String getBusiness(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_BUSINESS), (String) null);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前会话渠道来源
|
|
|
*
|
|
@@ -274,6 +381,16 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getChannel);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取客户端渠道来源
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 渠道来源
|
|
|
+ */
|
|
|
+ public static String getChannel(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_CHANNEL), (String) null);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前会话平台信息
|
|
|
*
|
|
@@ -293,6 +410,16 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getPlatform);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取客户端平台标识
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 平台标识
|
|
|
+ */
|
|
|
+ public static Platform getPlatform(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_PLATFORM), Platform::valueOf);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前会话终端信息
|
|
|
*
|
|
@@ -312,6 +439,16 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return ObjectUtils.ifNull(getSession(requireSession), Session::getTerminal);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 获取客户端终端
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 终端标识
|
|
|
+ */
|
|
|
+ public static Terminal getTerminal(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_TERMINAL), Terminal::valueOf);
|
|
|
+ }
|
|
|
+
|
|
|
/**
|
|
|
* 获取当前会话版本信息
|
|
|
*
|
|
@@ -332,41 +469,13 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取当前会话授权信息
|
|
|
- *
|
|
|
- * @return 授权信息
|
|
|
- */
|
|
|
- public static String getAuthority() {
|
|
|
- return getAuthority(true);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取当前会话授权信息
|
|
|
- *
|
|
|
- * @param requireSession 会话是否是必须
|
|
|
- * @return 授权信息
|
|
|
- */
|
|
|
- public static String getAuthority(boolean requireSession) {
|
|
|
- return ObjectUtils.ifNull(getSession(requireSession), Session::getAuthority);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断当前会话是否是首次请求
|
|
|
- *
|
|
|
- * @return true/false
|
|
|
- */
|
|
|
- public static boolean isInitial() {
|
|
|
- return isInitial(true);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 判断当前会话是否是首次请求
|
|
|
+ * 获取客户端版本号
|
|
|
*
|
|
|
- * @param requireSession 会话是否是必须
|
|
|
- * @return true/false
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 版本号
|
|
|
*/
|
|
|
- public static boolean isInitial(boolean requireSession) {
|
|
|
- return Boolean.TRUE.equals(ObjectUtils.ifNull(getSession(requireSession), Session::getInitial));
|
|
|
+ public static String getVersion(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_VERSION), (String) null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -389,104 +498,88 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断当前会话是否存在手机号
|
|
|
+ * 获取请求时间戳
|
|
|
*
|
|
|
- * @return true/false
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 时间戳
|
|
|
*/
|
|
|
- public static boolean hasMobile() {
|
|
|
- return StringUtils.nonEmpty(getMobile(false));
|
|
|
+ public static Long getTimestamp(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_TIMESTAMP), Long::parseLong);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断当前会话是否已认证
|
|
|
+ * 获取签名信息
|
|
|
*
|
|
|
- * @return true/false
|
|
|
+ * @return 签名信息
|
|
|
*/
|
|
|
- public static boolean isAuthenticated() {
|
|
|
- return Objects.nonNull(getId(false));
|
|
|
+ public static String getSignature() {
|
|
|
+ return ObjectUtils.ifNull(getRequest(), SessionContextHolder::getSignature, () -> null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取缓存对象
|
|
|
+ * 获取签名信息
|
|
|
*
|
|
|
- * @param key 缓存标识
|
|
|
- * @param <K> 缓存键类型泛型
|
|
|
- * @param <V> 缓存值得类型泛型
|
|
|
- * @return 对象实例
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 签名信息
|
|
|
*/
|
|
|
- public static <K, V> V getCache(@NonNull K key) {
|
|
|
- return getCache(key, k -> null);
|
|
|
+ public static String getSignature(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_SIGNATURE), (String) null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 获取缓存对象
|
|
|
+ * 获取设备指纹
|
|
|
*
|
|
|
- * @param key 缓存标识
|
|
|
- * @param provider 对象提供方法
|
|
|
- * @param <K> 缓存键类型泛型
|
|
|
- * @param <V> 缓存值得类型泛型
|
|
|
- * @return 对象实例
|
|
|
+ * @return 设备指纹
|
|
|
*/
|
|
|
- @SuppressWarnings("unchecked")
|
|
|
- public static <K, V> V getCache(@NonNull K key, @NonNull Function<K, V> provider) {
|
|
|
- return (V) CACHE_CONTEXT.get().computeIfAbsent(key, k -> provider.apply((K) k));
|
|
|
+ public static String getFingerprint() {
|
|
|
+ return getFingerprint(true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 设置缓存对象
|
|
|
+ * 获取设备指纹
|
|
|
*
|
|
|
- * @param key 缓存标识
|
|
|
- * @param value 缓存对象
|
|
|
- * @param <K> 缓存键类型泛型
|
|
|
- * @param <V> 缓存值得类型泛型
|
|
|
+ * @param requireSession 会话是否是必须
|
|
|
+ * @return 设备指纹
|
|
|
*/
|
|
|
- public static <K, V> void setCache(@NonNull K key, V value) {
|
|
|
- CACHE_CONTEXT.get().put(key, value);
|
|
|
+ public static String getFingerprint(boolean requireSession) {
|
|
|
+ return ObjectUtils.ifNull(getSession(requireSession), Session::getFingerprint);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 移除缓存对象
|
|
|
+ * 获取设备指纹
|
|
|
*
|
|
|
- * @param key 缓存标识
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 设备指纹
|
|
|
*/
|
|
|
- public static void removeCache(@NonNull String key) {
|
|
|
- CACHE_CONTEXT.get().remove(key);
|
|
|
+ public static String getFingerprint(@NonNull HttpServletRequest request) {
|
|
|
+ return StringUtils.ifEmpty(request.getHeader(HEADER_FINGERPRINT), (String) null);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 移除缓存对象
|
|
|
+ * 判断是否是历史首次使用
|
|
|
*
|
|
|
- * @param keys 缓存标识数组
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
- public static void removeCaches(@NonNull String... keys) {
|
|
|
- if (keys.length > 0) {
|
|
|
- Map<Object, Object> caches = CACHE_CONTEXT.get();
|
|
|
- for (String key : keys) {
|
|
|
- caches.remove(key);
|
|
|
- }
|
|
|
- }
|
|
|
+ public static boolean isInitialUsing() {
|
|
|
+ return getUsing(false) == Using.INITIAL;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 移除缓存对象
|
|
|
+ * 判断当前用户是否已注册
|
|
|
*
|
|
|
- * @param keys 缓存标识集合
|
|
|
+ * @return true/false
|
|
|
*/
|
|
|
- public static void removeCaches(@NonNull Collection<String> keys) {
|
|
|
- if (!CollectionUtils.isEmpty(keys)) {
|
|
|
- Map<Object, Object> caches = CACHE_CONTEXT.get();
|
|
|
- keys.forEach(caches::remove);
|
|
|
- }
|
|
|
+ public static boolean isRegistered() {
|
|
|
+ return StringUtils.nonEmpty(getMobile(false));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
- * 判断是否是指定应用范围
|
|
|
+ * 判断当前会话是否已认证
|
|
|
*
|
|
|
- * @param scope 应用范围
|
|
|
* @return true/false
|
|
|
*/
|
|
|
- public static boolean isScope(String scope) {
|
|
|
- return StringUtils.nonEmpty(scope) && Objects.equals(scope, getScope(false));
|
|
|
+ public static boolean isAuthenticated() {
|
|
|
+ return Objects.nonNull(getId(false));
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -634,196 +727,6 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return ObjectUtils.nonEmpty(accounts) && accounts.contains(account);
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * 获取当前请求对象
|
|
|
- *
|
|
|
- * @return Http请求对象
|
|
|
- */
|
|
|
- public static HttpServletRequest getRequest() {
|
|
|
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
- return ObjectUtils.ifNull(attributes, ServletRequestAttributes::getRequest);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取当前响应对象
|
|
|
- *
|
|
|
- * @return Http响应对象
|
|
|
- */
|
|
|
- public static HttpServletResponse getResponse() {
|
|
|
- ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
|
- 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);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 清空上下文信息
|
|
|
- */
|
|
|
- public static void clearSessionContext() {
|
|
|
- // 清空缓存
|
|
|
- CACHE_CONTEXT.remove();
|
|
|
-
|
|
|
- // 清空会话
|
|
|
- Deque<Session> sessions = SESSION_CONTEXT.get();
|
|
|
- sessions.poll();
|
|
|
- if (sessions.isEmpty()) {
|
|
|
- SESSION_CONTEXT.remove();
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端身份标识
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 身份标识
|
|
|
- */
|
|
|
- protected Long getId(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_ID), Long::parseLong);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端主机地址
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 主机地址
|
|
|
- */
|
|
|
- protected String getHost(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(HttpUtils.getRequestAddress(request), (String) null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取应用范围
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 应用范围
|
|
|
- */
|
|
|
- protected String getScope(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_SCOPE), (String) null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取会话电话号码
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 电话号码
|
|
|
- */
|
|
|
- protected String getMobile(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_MOBILE), (String) null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端设备标识
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 设备标识
|
|
|
- */
|
|
|
- protected String getDevice(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_DEVICE), (String) null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端渠道来源
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 渠道来源
|
|
|
- */
|
|
|
- protected String getChannel(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_CHANNEL), (String) null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端平台标识
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 平台标识
|
|
|
- */
|
|
|
- protected Platform getPlatform(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_PLATFORM), Platform::valueOf);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端终端
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 终端标识
|
|
|
- */
|
|
|
- protected Terminal getTerminal(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_TERMINAL), Terminal::valueOf);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端版本号
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 版本号
|
|
|
- */
|
|
|
- protected String getVersion(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_VERSION), (String) null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取客户端授权信息
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 授权信息
|
|
|
- */
|
|
|
- protected String getAuthority(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_AUTHORITY), (String) null);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取是否首次请求
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return true/false
|
|
|
- */
|
|
|
- protected Boolean getInitial(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_INITIAL), Boolean::parseBoolean);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 获取请求时间戳
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 时间戳
|
|
|
- */
|
|
|
- protected Long getTimestamp(@NonNull HttpServletRequest request) {
|
|
|
- return StringUtils.ifEmpty(request.getHeader(HEADER_TIMESTAMP), Long::parseLong);
|
|
|
- }
|
|
|
-
|
|
|
- /**
|
|
|
- * 初始化会话信息
|
|
|
- *
|
|
|
- * @param request Http请求对象
|
|
|
- * @return 会话信息
|
|
|
- */
|
|
|
- protected Session initializeSession(@NonNull HttpServletRequest request) {
|
|
|
- return Session.builder().id(this.getId(request)).host(this.getHost(request)).scope(this.getScope(request))
|
|
|
- .mobile(this.getMobile(request)).device(this.getDevice(request)).channel(this.getChannel(request))
|
|
|
- .platform(this.getPlatform(request)).terminal(this.getTerminal(request))
|
|
|
- .version(this.getVersion(request)).authority(this.getAuthority(request))
|
|
|
- .initial(this.getInitial(request)).timestamp(this.getTimestamp(request)).build();
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* 初始化会话信息
|
|
|
*
|
|
@@ -838,64 +741,59 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
/**
|
|
|
* 初始化会话主体
|
|
|
*
|
|
|
- * @param id 主体标识
|
|
|
- * @param scope 应用范围
|
|
|
+ * @param id 主体标识
|
|
|
* @return 会话信息
|
|
|
*/
|
|
|
- public static Session initializeSessionPrincipal(@NonNull Long id, @NonNull String scope) {
|
|
|
- return initializeSessionPrincipal(id, scope, null, null, true);
|
|
|
+ public static Session initializeSessionPrincipal(@NonNull Long id) {
|
|
|
+ return initializeSessionPrincipal(id, true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化会话主体
|
|
|
*
|
|
|
* @param id 主体标识
|
|
|
- * @param scope 应用范围
|
|
|
* @param cover 是否覆盖已认证主体信息
|
|
|
* @return 会话信息
|
|
|
*/
|
|
|
- public static Session initializeSessionPrincipal(@NonNull Long id, @NonNull String scope, boolean cover) {
|
|
|
- return initializeSessionPrincipal(id, scope, null, null, cover);
|
|
|
+ public static Session initializeSessionPrincipal(@NonNull Long id, boolean cover) {
|
|
|
+ return initializeSessionPrincipal(id, null, null, cover);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化会话主体
|
|
|
*
|
|
|
- * @param id 主体标识
|
|
|
- * @param scope 应用范围
|
|
|
- * @param mobile 电话号码
|
|
|
- * @param authority 授权信息
|
|
|
+ * @param id 主体标识
|
|
|
+ * @param mobile 电话号码
|
|
|
+ * @param business 业务类型
|
|
|
* @return 会话信息
|
|
|
*/
|
|
|
- public static Session initializeSessionPrincipal(@NonNull Long id, @NonNull String scope, String mobile,
|
|
|
- String authority) {
|
|
|
- return initializeSessionPrincipal(id, scope, mobile, authority, true);
|
|
|
+ public static Session initializeSessionPrincipal(@NonNull Long id, String mobile, String business) {
|
|
|
+ return initializeSessionPrincipal(id, mobile, business, true);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 初始化会话主体
|
|
|
*
|
|
|
- * @param id 主体标识
|
|
|
- * @param scope 应用范围
|
|
|
- * @param mobile 电话号码
|
|
|
- * @param authority 授权信息
|
|
|
- * @param cover 是否覆盖已认证主体信息
|
|
|
+ * @param id 主体标识
|
|
|
+ * @param mobile 电话号码
|
|
|
+ * @param business 业务类型
|
|
|
+ * @param cover 是否覆盖已认证主体信息
|
|
|
* @return 会话信息
|
|
|
*/
|
|
|
- public static Session initializeSessionPrincipal(@NonNull Long id, @NonNull String scope, String mobile,
|
|
|
- String authority, boolean cover) {
|
|
|
+ public static Session initializeSessionPrincipal(@NonNull Long id, String mobile, String business, boolean cover) {
|
|
|
Deque<Session> deque = SESSION_CONTEXT.get();
|
|
|
Session session = deque.peek();
|
|
|
if (cover || ObjectUtils.ifNull(session, Session::getId) == null) {
|
|
|
- session = Session.builder().id(id).scope(scope).mobile(mobile).authority(authority)
|
|
|
+ session = Session.builder().id(id).mobile(mobile).business(business)
|
|
|
+ .using(ObjectUtils.ifNull(session, Session::getUsing))
|
|
|
.host(ObjectUtils.ifNull(session, Session::getHost))
|
|
|
.device(ObjectUtils.ifNull(session, Session::getDevice))
|
|
|
.channel(ObjectUtils.ifNull(session, Session::getChannel))
|
|
|
.platform(ObjectUtils.ifNull(session, Session::getPlatform))
|
|
|
.terminal(ObjectUtils.ifNull(session, Session::getTerminal))
|
|
|
.version(ObjectUtils.ifNull(session, Session::getVersion))
|
|
|
- .timestamp(ObjectUtils.ifNull(session, Session::getTimestamp))
|
|
|
- .initial(ObjectUtils.ifNull(session, Session::getInitial)).build();
|
|
|
+ .fingerprint(ObjectUtils.ifNull(session, Session::getFingerprint))
|
|
|
+ .timestamp(ObjectUtils.ifNull(session, Session::getTimestamp)).build();
|
|
|
deque.poll();
|
|
|
deque.push(session);
|
|
|
}
|
|
@@ -1008,6 +906,31 @@ public class SessionContextHolder implements ServletRequestListener {
|
|
|
return getSessionMessage(request, status.value(), exception);
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * 清空上下文信息
|
|
|
+ */
|
|
|
+ public static void clearSessionContext() {
|
|
|
+ Deque<Session> sessions = SESSION_CONTEXT.get();
|
|
|
+ sessions.poll();
|
|
|
+ if (sessions.isEmpty()) {
|
|
|
+ SESSION_CONTEXT.remove();
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 初始化会话信息
|
|
|
+ *
|
|
|
+ * @param request Http请求对象
|
|
|
+ * @return 会话信息
|
|
|
+ */
|
|
|
+ protected Session initializeSession(@NonNull HttpServletRequest request) {
|
|
|
+ return Session.builder().id(getId(request)).using(getUsing(request)).host(getHost(request))
|
|
|
+ .mobile(getMobile(request)).device(getDevice(request)).business(getBusiness(request))
|
|
|
+ .channel(getChannel(request)).platform(getPlatform(request)).terminal(getTerminal(request))
|
|
|
+ .version(getVersion(request)).fingerprint(getFingerprint(request))
|
|
|
+ .timestamp(getTimestamp(request)).build();
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public void requestInitialized(ServletRequestEvent event) {
|
|
|
setSession(this.initializeSession((HttpServletRequest) event.getServletRequest()));
|