mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-04-05 17:37:53 +08:00
新增二级Context模式
This commit is contained in:
parent
3929b16dfb
commit
068f838165
@ -9,6 +9,7 @@ import cn.dev33.satoken.config.SaTokenConfig;
|
|||||||
import cn.dev33.satoken.config.SaTokenConfigFactory;
|
import cn.dev33.satoken.config.SaTokenConfigFactory;
|
||||||
import cn.dev33.satoken.context.SaTokenContext;
|
import cn.dev33.satoken.context.SaTokenContext;
|
||||||
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||||
|
import cn.dev33.satoken.context.second.SaTokenSecondContext;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
|
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
|
||||||
import cn.dev33.satoken.exception.SaTokenException;
|
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.StpInterfaceDefaultImpl;
|
||||||
import cn.dev33.satoken.stp.StpLogic;
|
import cn.dev33.satoken.stp.StpLogic;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import cn.dev33.satoken.temp.SaTempInterface;
|
|
||||||
import cn.dev33.satoken.temp.SaTempDefaultImpl;
|
import cn.dev33.satoken.temp.SaTempDefaultImpl;
|
||||||
|
import cn.dev33.satoken.temp.SaTempInterface;
|
||||||
import cn.dev33.satoken.util.SaFoxUtil;
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 管理 Sa-Token 所有接口对象
|
* 管理 Sa-Token 所有全局组件
|
||||||
* @author kong
|
* @author kong
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
@ -111,21 +112,49 @@ public class SaManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 上下文 Bean
|
* 上下文Context Bean
|
||||||
*/
|
*/
|
||||||
private volatile static SaTokenContext saTokenContext;
|
private volatile static SaTokenContext saTokenContext;
|
||||||
public static void setSaTokenContext(SaTokenContext saTokenContext) {
|
public static void setSaTokenContext(SaTokenContext saTokenContext) {
|
||||||
SaManager.saTokenContext = saTokenContext;
|
SaManager.saTokenContext = saTokenContext;
|
||||||
}
|
}
|
||||||
public static SaTokenContext getSaTokenContext() {
|
public static SaTokenContext getSaTokenContext() {
|
||||||
if (saTokenContext == null) {
|
return saTokenContext;
|
||||||
synchronized (SaManager.class) {
|
}
|
||||||
if (saTokenContext == null) {
|
|
||||||
setSaTokenContext(new SaTokenContextDefaultImpl());
|
/**
|
||||||
}
|
* 二级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 {
|
public class SaHolder {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取当前请求的 SaTokenContext
|
||||||
|
*
|
||||||
|
* @return see note
|
||||||
|
*/
|
||||||
|
public static SaTokenContext getContext() {
|
||||||
|
return SaManager.getSaTokenContextOrSecond();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 获取当前请求的 [Request] 对象
|
* 获取当前请求的 [Request] 对象
|
||||||
@ -18,7 +27,7 @@ public class SaHolder {
|
|||||||
* @return see note
|
* @return see note
|
||||||
*/
|
*/
|
||||||
public static SaRequest getRequest() {
|
public static SaRequest getRequest() {
|
||||||
return SaManager.getSaTokenContext().getRequest();
|
return SaManager.getSaTokenContextOrSecond().getRequest();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -27,7 +36,7 @@ public class SaHolder {
|
|||||||
* @return see note
|
* @return see note
|
||||||
*/
|
*/
|
||||||
public static SaResponse getResponse() {
|
public static SaResponse getResponse() {
|
||||||
return SaManager.getSaTokenContext().getResponse();
|
return SaManager.getSaTokenContextOrSecond().getResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,7 +45,7 @@ public class SaHolder {
|
|||||||
* @return see note
|
* @return see note
|
||||||
*/
|
*/
|
||||||
public static SaStorage getStorage() {
|
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);
|
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 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;
|
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 是否匹配成功
|
* @return 是否匹配成功
|
||||||
*/
|
*/
|
||||||
public static boolean isMatch(String pattern, String path) {
|
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 是否匹配成功
|
* @return 是否匹配成功
|
||||||
*/
|
*/
|
||||||
public static boolean isMatch(String pattern, String path) {
|
public static boolean isMatch(String pattern, String path) {
|
||||||
return SaManager.getSaTokenContext().matchPath(pattern, path);
|
return SaManager.getSaTokenContextOrSecond().matchPath(pattern, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -14,8 +14,8 @@ import cn.dev33.satoken.SaManager;
|
|||||||
public class SaTokenDemoApplication {
|
public class SaTokenDemoApplication {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
SpringApplication.run(SaTokenDemoApplication.class, args);
|
SpringApplication.run(SaTokenDemoApplication.class, args);
|
||||||
System.out.println("\n启动成功:Sa-Token配置如下:" + SaManager.getConfig());
|
System.out.println("\n启动成功:Sa-Token配置如下:" + SaManager.getConfig());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -17,25 +17,25 @@ Sa-Token 采用 Apache-2.0 开源协议,**承诺框架本身与官网文档永
|
|||||||
|
|
||||||
<p class="zanzhu-pre"></p>
|
<p class="zanzhu-pre"></p>
|
||||||
|
|
||||||
| 赞助人 | 赞助金额 | 留言 |
|
| 赞助人 | 赞助金额 | 留言 | 时间 |
|
||||||
| :-------- | :-------- | :-------- |
|
| :-------- | :-------- | :-------- | :-------- |
|
||||||
| apifox001 | ¥ 200 | 开源不易,Apifox(apifox.cn)和你们一起加油! |
|
| apifox001 | ¥ 200 | 开源不易,Apifox(apifox.cn)和你们一起加油! | 2021-10-15 |
|
||||||
| 永夜 | ¥ 20 | 感谢您的开源项目! |
|
| 永夜 | ¥ 20 | 感谢您的开源项目! | 2021-09-18 |
|
||||||
| 苏永晓 | ¥ 10 | 感谢您的开源项目! |
|
| 苏永晓 | ¥ 10 | 感谢您的开源项目! | 2021-09-01 |
|
||||||
| xiaoyan | ¥ 200 | 好的作者理应被认可 |
|
| xiaoyan | ¥ 200 | 好的作者理应被认可 | 2021-08-24 |
|
||||||
| xiaoyan | ¥ 50 | be better |
|
| xiaoyan | ¥ 50 | be better | 2021-07-31 |
|
||||||
| 孔孔的空空 | ¥ 500 | 感谢您的开源项目! |
|
| 孔孔的空空 | ¥ 500 | 感谢您的开源项目! | 2021-07-30 |
|
||||||
| Wizzer | ¥ 20 | 感谢您的开源项目! |
|
| Wizzer | ¥ 20 | 感谢您的开源项目! | 2021-05-22 |
|
||||||
| 二范先生 | ¥ 20 | 省长加油啊 喝杯茶 |
|
| 二范先生 | ¥ 20 | 省长加油啊 喝杯茶 | 2021-03-16 |
|
||||||
| EchoSlammaJamma | ¥ 20 | 感谢您的开源项目! |
|
| EchoSlammaJamma | ¥ 20 | 感谢您的开源项目! | 2021-03-16 |
|
||||||
| xue1992wz | ¥ 20 | 感谢您的开源项目! |
|
| xue1992wz | ¥ 20 | 感谢您的开源项目! | 2021-03-16 |
|
||||||
| whcrow | ¥ 20 | 军师加油! |
|
| whcrow | ¥ 20 | 军师加油! | 2021-03-16 |
|
||||||
| njx33 | ¥ 10 | 感谢您的开源项目! |
|
| njx33 | ¥ 10 | 感谢您的开源项目! | 2020-12-17 |
|
||||||
| zhangjiaxiaozhuo | ¥ 10 | 感谢您的开源项目! |
|
| zhangjiaxiaozhuo | ¥ 10 | 感谢您的开源项目! | 2020-12-15 |
|
||||||
| 知知 | ¥ 10 | 感谢您的开源项目! |
|
| 知知 | ¥ 10 | 感谢您的开源项目! | 2020-12-15 |
|
||||||
| 省长 | ¥ 10 | java中最好用的权限认证框架! |
|
| 省长 | ¥ 10 | java中最好用的权限认证框架! | 2020-12-15 |
|
||||||
|
|
||||||
感谢每一位小伙伴的支持!
|
感谢每一位小伙伴的热心支持!
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -8,7 +8,7 @@
|
|||||||
首先在项目已经引入 Sa-Token 的基础上,继续添加:
|
首先在项目已经引入 Sa-Token 的基础上,继续添加:
|
||||||
|
|
||||||
``` xml
|
``` xml
|
||||||
<!-- 在 thymeleaf 标签中使用 Sa-Token -->
|
<!-- Sa-Token 整合 jwt -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>cn.dev33</groupId>
|
<groupId>cn.dev33</groupId>
|
||||||
<artifactId>sa-token-jwt</artifactId>
|
<artifactId>sa-token-jwt</artifactId>
|
||||||
|
@ -319,8 +319,9 @@
|
|||||||
<a href="https://su.usthe.com/" target="_blank">
|
<a href="https://su.usthe.com/" target="_blank">
|
||||||
<img src="https://oss.dev33.cn/sa-token/link/sureness.png" msg="面向 REST API 的高性能认证鉴权框架">
|
<img src="https://oss.dev33.cn/sa-token/link/sureness.png" msg="面向 REST API 的高性能认证鉴权框架">
|
||||||
</a>
|
</a>
|
||||||
|
<a href="https://searcher.ejlchina.com/" target="_blank">
|
||||||
|
<img src="https://oss.dev33.cn/sa-token/link/bean-searcher.png" msg="轻量级关系数据库条件检索引擎,使一行代码实现复杂列表检索成为可能!">
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div style="height: 10px; clear: both;"></div>
|
<div style="height: 10px; clear: both;"></div>
|
||||||
</div>
|
</div>
|
||||||
|
@ -9,6 +9,7 @@ import cn.dev33.satoken.basic.SaBasicTemplate;
|
|||||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||||
import cn.dev33.satoken.config.SaTokenConfig;
|
import cn.dev33.satoken.config.SaTokenConfig;
|
||||||
import cn.dev33.satoken.context.SaTokenContext;
|
import cn.dev33.satoken.context.SaTokenContext;
|
||||||
|
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
import cn.dev33.satoken.id.SaIdTemplate;
|
import cn.dev33.satoken.id.SaIdTemplate;
|
||||||
import cn.dev33.satoken.id.SaIdUtil;
|
import cn.dev33.satoken.id.SaIdUtil;
|
||||||
@ -69,7 +70,7 @@ public class SaBeanInject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入容器交互Bean
|
* 注入上下文Bean
|
||||||
*
|
*
|
||||||
* @param saTokenContext SaTokenContext对象
|
* @param saTokenContext SaTokenContext对象
|
||||||
*/
|
*/
|
||||||
@ -78,6 +79,16 @@ public class SaBeanInject {
|
|||||||
SaManager.setSaTokenContext(saTokenContext);
|
SaManager.setSaTokenContext(saTokenContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注入二级上下文Bean
|
||||||
|
*
|
||||||
|
* @param saTokenSecondContextCreator 二级上下文创建器
|
||||||
|
*/
|
||||||
|
@Autowired(required = false)
|
||||||
|
public void setSaTokenContext(SaTokenSecondContextCreator saTokenSecondContextCreator) {
|
||||||
|
SaManager.setSaTokenSecondContext(saTokenSecondContextCreator.create());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入侦听器Bean
|
* 注入侦听器Bean
|
||||||
*
|
*
|
||||||
|
@ -106,7 +106,7 @@ public class SaRequestForServlet implements SaRequest {
|
|||||||
@Override
|
@Override
|
||||||
public Object forward(String path) {
|
public Object forward(String path) {
|
||||||
try {
|
try {
|
||||||
HttpServletResponse response = (HttpServletResponse)SaManager.getSaTokenContext().getResponse().getSource();
|
HttpServletResponse response = (HttpServletResponse)SaManager.getSaTokenContextOrSecond().getResponse().getSource();
|
||||||
request.getRequestDispatcher(path).forward(request, response);
|
request.getRequestDispatcher(path).forward(request, response);
|
||||||
return null;
|
return null;
|
||||||
} catch (ServletException | IOException e) {
|
} catch (ServletException | IOException e) {
|
||||||
|
@ -15,6 +15,7 @@ import cn.dev33.satoken.annotation.SaCheckSafe;
|
|||||||
import cn.dev33.satoken.basic.SaBasicTemplate;
|
import cn.dev33.satoken.basic.SaBasicTemplate;
|
||||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||||
import cn.dev33.satoken.config.SaTokenConfig;
|
import cn.dev33.satoken.config.SaTokenConfig;
|
||||||
|
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
import cn.dev33.satoken.id.SaIdTemplate;
|
import cn.dev33.satoken.id.SaIdTemplate;
|
||||||
import cn.dev33.satoken.id.SaIdUtil;
|
import cn.dev33.satoken.id.SaIdUtil;
|
||||||
@ -49,9 +50,15 @@ public class XPluginImp implements Plugin {
|
|||||||
SaTokenConfig saTokenConfig = Solon.cfg().getBean("sa-token", SaTokenConfig.class);
|
SaTokenConfig saTokenConfig = Solon.cfg().getBean("sa-token", SaTokenConfig.class);
|
||||||
SaManager.setConfig(saTokenConfig);
|
SaManager.setConfig(saTokenConfig);
|
||||||
|
|
||||||
//注入容器交互Bean
|
// 注入上下文Bean
|
||||||
SaManager.setSaTokenContext(new SaContextForSolon());
|
SaManager.setSaTokenContext(new SaContextForSolon());
|
||||||
|
|
||||||
|
// 注入二级上下文 Bean
|
||||||
|
Aop.getAsyn(SaTokenSecondContextCreator.class, bw->{
|
||||||
|
SaTokenSecondContextCreator raw = bw.raw();
|
||||||
|
SaManager.setSaTokenSecondContext(raw.create());
|
||||||
|
});
|
||||||
|
|
||||||
// 注入侦听器 Bean
|
// 注入侦听器 Bean
|
||||||
Aop.getAsyn(SaTokenListener.class, bw->{
|
Aop.getAsyn(SaTokenListener.class, bw->{
|
||||||
SaManager.setSaTokenListener(bw.raw());
|
SaManager.setSaTokenListener(bw.raw());
|
||||||
|
@ -7,6 +7,8 @@ import cn.dev33.satoken.context.model.SaStorage;
|
|||||||
import cn.dev33.satoken.solon.model.SaRequestForSolon;
|
import cn.dev33.satoken.solon.model.SaRequestForSolon;
|
||||||
import cn.dev33.satoken.solon.model.SaResponseForSolon;
|
import cn.dev33.satoken.solon.model.SaResponseForSolon;
|
||||||
import cn.dev33.satoken.solon.model.SaStorageForSolon;
|
import cn.dev33.satoken.solon.model.SaStorageForSolon;
|
||||||
|
|
||||||
|
import org.noear.solon.core.handle.Context;
|
||||||
import org.noear.solon.core.util.PathAnalyzer;
|
import org.noear.solon.core.util.PathAnalyzer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -45,4 +47,13 @@ public class SaContextForSolon implements SaTokenContext {
|
|||||||
public boolean matchPath(String pattern, String path) {
|
public boolean matchPath(String pattern, String path) {
|
||||||
return PathAnalyzer.get(pattern).matches(path);
|
return PathAnalyzer.get(pattern).matches(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 此上下文是否有效
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public boolean isValid() {
|
||||||
|
return Context.current() != null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -9,6 +9,7 @@ import cn.dev33.satoken.basic.SaBasicTemplate;
|
|||||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||||
import cn.dev33.satoken.config.SaTokenConfig;
|
import cn.dev33.satoken.config.SaTokenConfig;
|
||||||
import cn.dev33.satoken.context.SaTokenContext;
|
import cn.dev33.satoken.context.SaTokenContext;
|
||||||
|
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
import cn.dev33.satoken.id.SaIdTemplate;
|
import cn.dev33.satoken.id.SaIdTemplate;
|
||||||
import cn.dev33.satoken.id.SaIdUtil;
|
import cn.dev33.satoken.id.SaIdUtil;
|
||||||
@ -69,7 +70,7 @@ public class SaBeanInject {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入容器交互Bean
|
* 注入上下文Bean
|
||||||
*
|
*
|
||||||
* @param saTokenContext SaTokenContext对象
|
* @param saTokenContext SaTokenContext对象
|
||||||
*/
|
*/
|
||||||
@ -78,6 +79,16 @@ public class SaBeanInject {
|
|||||||
SaManager.setSaTokenContext(saTokenContext);
|
SaManager.setSaTokenContext(saTokenContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 注入二级上下文Bean
|
||||||
|
*
|
||||||
|
* @param saTokenSecondContextCreator 二级上下文创建器
|
||||||
|
*/
|
||||||
|
@Autowired(required = false)
|
||||||
|
public void setSaTokenContext(SaTokenSecondContextCreator saTokenSecondContextCreator) {
|
||||||
|
SaManager.setSaTokenSecondContext(saTokenSecondContextCreator.create());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 注入侦听器Bean
|
* 注入侦听器Bean
|
||||||
*
|
*
|
||||||
|
@ -48,6 +48,12 @@ public class SaTokenContextForSpring implements SaTokenContext {
|
|||||||
return SaPathMatcherHolder.getPathMatcher().match(pattern, path);
|
return SaPathMatcherHolder.getPathMatcher().match(pattern, path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 此上下文是否有效
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean isValid() {
|
||||||
|
return SpringMVCUtil.isWeb();
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -39,4 +39,12 @@ public class SpringMVCUtil {
|
|||||||
return servletRequestAttributes.getResponse();
|
return servletRequestAttributes.getResponse();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 判断当前是否处于 Web 上下文中
|
||||||
|
* @return request
|
||||||
|
*/
|
||||||
|
public static boolean isWeb() {
|
||||||
|
return RequestContextHolder.getRequestAttributes() != null;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user