mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 10:08:07 +08:00
新增二级Context模式
This commit is contained in:
@@ -9,6 +9,7 @@ import cn.dev33.satoken.basic.SaBasicTemplate;
|
||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.id.SaIdTemplate;
|
||||
import cn.dev33.satoken.id.SaIdUtil;
|
||||
@@ -69,7 +70,7 @@ public class SaBeanInject {
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入容器交互Bean
|
||||
* 注入上下文Bean
|
||||
*
|
||||
* @param saTokenContext SaTokenContext对象
|
||||
*/
|
||||
@@ -78,6 +79,16 @@ public class SaBeanInject {
|
||||
SaManager.setSaTokenContext(saTokenContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入二级上下文Bean
|
||||
*
|
||||
* @param saTokenSecondContextCreator 二级上下文创建器
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenContext(SaTokenSecondContextCreator saTokenSecondContextCreator) {
|
||||
SaManager.setSaTokenSecondContext(saTokenSecondContextCreator.create());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入侦听器Bean
|
||||
*
|
||||
|
@@ -106,7 +106,7 @@ public class SaRequestForServlet implements SaRequest {
|
||||
@Override
|
||||
public Object forward(String path) {
|
||||
try {
|
||||
HttpServletResponse response = (HttpServletResponse)SaManager.getSaTokenContext().getResponse().getSource();
|
||||
HttpServletResponse response = (HttpServletResponse)SaManager.getSaTokenContextOrSecond().getResponse().getSource();
|
||||
request.getRequestDispatcher(path).forward(request, response);
|
||||
return null;
|
||||
} 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.SaBasicUtil;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.id.SaIdTemplate;
|
||||
import cn.dev33.satoken.id.SaIdUtil;
|
||||
@@ -49,9 +50,15 @@ public class XPluginImp implements Plugin {
|
||||
SaTokenConfig saTokenConfig = Solon.cfg().getBean("sa-token", SaTokenConfig.class);
|
||||
SaManager.setConfig(saTokenConfig);
|
||||
|
||||
//注入容器交互Bean
|
||||
// 注入上下文Bean
|
||||
SaManager.setSaTokenContext(new SaContextForSolon());
|
||||
|
||||
// 注入二级上下文 Bean
|
||||
Aop.getAsyn(SaTokenSecondContextCreator.class, bw->{
|
||||
SaTokenSecondContextCreator raw = bw.raw();
|
||||
SaManager.setSaTokenSecondContext(raw.create());
|
||||
});
|
||||
|
||||
// 注入侦听器 Bean
|
||||
Aop.getAsyn(SaTokenListener.class, bw->{
|
||||
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.SaResponseForSolon;
|
||||
import cn.dev33.satoken.solon.model.SaStorageForSolon;
|
||||
|
||||
import org.noear.solon.core.handle.Context;
|
||||
import org.noear.solon.core.util.PathAnalyzer;
|
||||
|
||||
/**
|
||||
@@ -45,4 +47,13 @@ public class SaContextForSolon implements SaTokenContext {
|
||||
public boolean matchPath(String pattern, String 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.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.id.SaIdTemplate;
|
||||
import cn.dev33.satoken.id.SaIdUtil;
|
||||
@@ -69,7 +70,7 @@ public class SaBeanInject {
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入容器交互Bean
|
||||
* 注入上下文Bean
|
||||
*
|
||||
* @param saTokenContext SaTokenContext对象
|
||||
*/
|
||||
@@ -78,6 +79,16 @@ public class SaBeanInject {
|
||||
SaManager.setSaTokenContext(saTokenContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入二级上下文Bean
|
||||
*
|
||||
* @param saTokenSecondContextCreator 二级上下文创建器
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenContext(SaTokenSecondContextCreator saTokenSecondContextCreator) {
|
||||
SaManager.setSaTokenSecondContext(saTokenSecondContextCreator.create());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入侦听器Bean
|
||||
*
|
||||
|
@@ -48,6 +48,12 @@ public class SaTokenContextForSpring implements SaTokenContext {
|
||||
return SaPathMatcherHolder.getPathMatcher().match(pattern, path);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 此上下文是否有效
|
||||
*/
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return SpringMVCUtil.isWeb();
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -39,4 +39,12 @@ public class SpringMVCUtil {
|
||||
return servletRequestAttributes.getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 判断当前是否处于 Web 上下文中
|
||||
* @return request
|
||||
*/
|
||||
public static boolean isWeb() {
|
||||
return RequestContextHolder.getRequestAttributes() != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user