mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 01:58:05 +08:00
新增二级Context模式
This commit is contained in:
@@ -9,6 +9,7 @@ import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.config.SaTokenConfigFactory;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContext;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
|
||||
import cn.dev33.satoken.exception.SaTokenException;
|
||||
@@ -18,12 +19,12 @@ import cn.dev33.satoken.stp.StpInterface;
|
||||
import cn.dev33.satoken.stp.StpInterfaceDefaultImpl;
|
||||
import cn.dev33.satoken.stp.StpLogic;
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.temp.SaTempInterface;
|
||||
import cn.dev33.satoken.temp.SaTempDefaultImpl;
|
||||
import cn.dev33.satoken.temp.SaTempInterface;
|
||||
import cn.dev33.satoken.util.SaFoxUtil;
|
||||
|
||||
/**
|
||||
* 管理 Sa-Token 所有接口对象
|
||||
* 管理 Sa-Token 所有全局组件
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@@ -111,21 +112,49 @@ public class SaManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 上下文 Bean
|
||||
* 上下文Context Bean
|
||||
*/
|
||||
private volatile static SaTokenContext saTokenContext;
|
||||
public static void setSaTokenContext(SaTokenContext saTokenContext) {
|
||||
SaManager.saTokenContext = saTokenContext;
|
||||
}
|
||||
public static SaTokenContext getSaTokenContext() {
|
||||
if (saTokenContext == null) {
|
||||
synchronized (SaManager.class) {
|
||||
if (saTokenContext == null) {
|
||||
setSaTokenContext(new SaTokenContextDefaultImpl());
|
||||
}
|
||||
return saTokenContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 二级Context
|
||||
*/
|
||||
private volatile static SaTokenSecondContext saTokenSecondContext;
|
||||
public static SaTokenSecondContext getSaTokenSecondContext() {
|
||||
return saTokenSecondContext;
|
||||
}
|
||||
public static void setSaTokenSecondContext(SaTokenSecondContext saTokenSecondContext) {
|
||||
SaManager.saTokenSecondContext = saTokenSecondContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个可用的SaTokenContext
|
||||
* @return /
|
||||
*/
|
||||
public static SaTokenContext getSaTokenContextOrSecond() {
|
||||
|
||||
// s1. 一级Context可用时返回一级Context
|
||||
if(saTokenContext != null) {
|
||||
if(saTokenSecondContext == null || saTokenContext.isValid()) {
|
||||
// 因为 isValid 是一个耗时操作,所以此处假定:二级Context为null的情况下无需验证一级Context有效性
|
||||
// 这样可以提升6倍左右的上下文获取速度
|
||||
return saTokenContext;
|
||||
}
|
||||
}
|
||||
return saTokenContext;
|
||||
|
||||
// s2. 一级Context不可用时判断二级Context是否可用
|
||||
if(saTokenSecondContext != null && saTokenSecondContext.isValid()) {
|
||||
return saTokenSecondContext;
|
||||
}
|
||||
|
||||
// s3. 都不行,就返回默认的 Context
|
||||
return SaTokenContextDefaultImpl.defaultContext;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -11,6 +11,15 @@ import cn.dev33.satoken.context.model.SaStorage;
|
||||
*
|
||||
*/
|
||||
public class SaHolder {
|
||||
|
||||
/**
|
||||
* 获取当前请求的 SaTokenContext
|
||||
*
|
||||
* @return see note
|
||||
*/
|
||||
public static SaTokenContext getContext() {
|
||||
return SaManager.getSaTokenContextOrSecond();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前请求的 [Request] 对象
|
||||
@@ -18,7 +27,7 @@ public class SaHolder {
|
||||
* @return see note
|
||||
*/
|
||||
public static SaRequest getRequest() {
|
||||
return SaManager.getSaTokenContext().getRequest();
|
||||
return SaManager.getSaTokenContextOrSecond().getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,7 +36,7 @@ public class SaHolder {
|
||||
* @return see note
|
||||
*/
|
||||
public static SaResponse getResponse() {
|
||||
return SaManager.getSaTokenContext().getResponse();
|
||||
return SaManager.getSaTokenContextOrSecond().getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -36,7 +45,7 @@ public class SaHolder {
|
||||
* @return see note
|
||||
*/
|
||||
public static SaStorage getStorage() {
|
||||
return SaManager.getSaTokenContext().getStorage();
|
||||
return SaManager.getSaTokenContextOrSecond().getStorage();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -41,4 +41,12 @@ public interface SaTokenContext {
|
||||
*/
|
||||
public boolean matchPath(String pattern, String path);
|
||||
|
||||
/**
|
||||
* 此上下文是否有效
|
||||
* @return /
|
||||
*/
|
||||
public default boolean isValid() {
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -17,6 +17,11 @@ import cn.dev33.satoken.exception.SaTokenException;
|
||||
*
|
||||
*/
|
||||
public class SaTokenContextDefaultImpl implements SaTokenContext {
|
||||
|
||||
/**
|
||||
* 默认的上下文处理器对象
|
||||
*/
|
||||
public static SaTokenContextDefaultImpl defaultContext = new SaTokenContextDefaultImpl();
|
||||
|
||||
/**
|
||||
* 默认的错误提示语
|
||||
@@ -56,5 +61,4 @@ public class SaTokenContextDefaultImpl implements SaTokenContext {
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@@ -37,4 +37,9 @@ public class SaTokenContextForThreadLocal implements SaTokenContext {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return SaTokenContextForThreadLocalStorage.getBox() != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,15 @@
|
||||
package cn.dev33.satoken.context.second;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
|
||||
/**
|
||||
* Sa-Token 二级Context - 基础接口
|
||||
*
|
||||
* <p> (利用继承机制实现区别 [一级Context] 与 [二级Context] 的目的)
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public interface SaTokenSecondContext extends SaTokenContext {
|
||||
|
||||
}
|
@@ -0,0 +1,18 @@
|
||||
package cn.dev33.satoken.context.second;
|
||||
|
||||
/**
|
||||
* Sa-Token 二级Context - 创建器
|
||||
*
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface SaTokenSecondContextCreator {
|
||||
|
||||
/**
|
||||
* 创建一个二级 Context
|
||||
* @return /
|
||||
*/
|
||||
public SaTokenSecondContext create();
|
||||
|
||||
}
|
@@ -26,7 +26,7 @@ public class SaRouter {
|
||||
* @return 是否匹配成功
|
||||
*/
|
||||
public static boolean isMatch(String pattern, String path) {
|
||||
return SaManager.getSaTokenContext().matchPath(pattern, path);
|
||||
return SaManager.getSaTokenContextOrSecond().matchPath(pattern, path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -27,7 +27,7 @@ public class SaRouterUtil {
|
||||
* @return 是否匹配成功
|
||||
*/
|
||||
public static boolean isMatch(String pattern, String path) {
|
||||
return SaManager.getSaTokenContext().matchPath(pattern, path);
|
||||
return SaManager.getSaTokenContextOrSecond().matchPath(pattern, path);
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user