mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-20 18:47:25 +08:00
重构:全局过滤器执行函数放到成员变量里.
This commit is contained in:
@@ -11,10 +11,6 @@ import cn.dev33.satoken.context.SaTokenContext;
|
|||||||
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||||
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.filter.SaFilterErrorStrategy;
|
|
||||||
import cn.dev33.satoken.filter.SaFilterErrorStrategyDefaultImpl;
|
|
||||||
import cn.dev33.satoken.filter.SaFilterStrategy;
|
|
||||||
import cn.dev33.satoken.filter.SaFilterStrategyDefaultImpl;
|
|
||||||
import cn.dev33.satoken.stp.StpInterface;
|
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;
|
||||||
@@ -128,42 +124,6 @@ public class SaTokenManager {
|
|||||||
return saTokenContext;
|
return saTokenContext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 全局过滤器-认证策略 Bean
|
|
||||||
*/
|
|
||||||
private static SaFilterStrategy strategy;
|
|
||||||
public static void setSaFilterStrategy(SaFilterStrategy strategy) {
|
|
||||||
SaTokenManager.strategy = strategy;
|
|
||||||
}
|
|
||||||
public static SaFilterStrategy getSaFilterStrategy() {
|
|
||||||
if (strategy == null) {
|
|
||||||
synchronized (SaTokenManager.class) {
|
|
||||||
if (strategy == null) {
|
|
||||||
setSaFilterStrategy(new SaFilterStrategyDefaultImpl());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return strategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 全局过滤器-异常处理策略 Bean
|
|
||||||
*/
|
|
||||||
private static SaFilterErrorStrategy errorStrategy;
|
|
||||||
public static void setSaFilterErrorStrategy(SaFilterErrorStrategy errorStrategy) {
|
|
||||||
SaTokenManager.errorStrategy = errorStrategy;
|
|
||||||
}
|
|
||||||
public static SaFilterErrorStrategy getSaFilterErrorStrategy() {
|
|
||||||
if (errorStrategy == null) {
|
|
||||||
synchronized (SaTokenManager.class) {
|
|
||||||
if (errorStrategy == null) {
|
|
||||||
setSaFilterErrorStrategy(new SaFilterErrorStrategyDefaultImpl());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return errorStrategy;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* StpLogic集合, 记录框架所有成功初始化的StpLogic
|
* StpLogic集合, 记录框架所有成功初始化的StpLogic
|
||||||
*/
|
*/
|
||||||
|
@@ -1,21 +0,0 @@
|
|||||||
package cn.dev33.satoken.filter;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.exception.SaTokenException;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sa-token全局过滤器-异常处理策略 [默认实现]
|
|
||||||
*
|
|
||||||
* @author kong
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class SaFilterErrorStrategyDefaultImpl implements SaFilterErrorStrategy {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行方法
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public Object run(Throwable e) {
|
|
||||||
throw new SaTokenException(e);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -1,19 +0,0 @@
|
|||||||
package cn.dev33.satoken.filter;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* sa-token全局过滤器-认证策略 [默认实现]
|
|
||||||
*
|
|
||||||
* @author kong
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class SaFilterStrategyDefaultImpl implements SaFilterStrategy {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 执行验证的方法
|
|
||||||
*/
|
|
||||||
@Override
|
|
||||||
public void run(Object r) {
|
|
||||||
// default no action
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
@@ -30,8 +30,21 @@ public class SaTokenConfigure implements WebMvcConfigurer {
|
|||||||
// @Bean
|
// @Bean
|
||||||
// public SaServletFilter getSaReactorFilter() {
|
// public SaServletFilter getSaReactorFilter() {
|
||||||
// return new SaServletFilter()
|
// return new SaServletFilter()
|
||||||
|
// // 指定 [拦截路由]
|
||||||
// .addInclude("/**")
|
// .addInclude("/**")
|
||||||
// .addExclude("/favicon.ico");
|
// // 指定 [放行路由]
|
||||||
|
// .addExclude("/favicon.ico")
|
||||||
|
// // 指定[认证函数]: 每次请求执行
|
||||||
|
// .setAuth(r -> {
|
||||||
|
// System.out.println("---------- sa全局认证");
|
||||||
|
// SaRouterUtil.match("/test/test", () -> StpUtil.checkLogin());
|
||||||
|
// })
|
||||||
|
// // 指定[异常处理函数]:每次[认证函数]发生异常时执行此函数
|
||||||
|
// .setError(e -> {
|
||||||
|
// System.out.println("---------- sa全局异常 ");
|
||||||
|
// return AjaxJson.getError(e.getMessage());
|
||||||
|
// })
|
||||||
|
// ;
|
||||||
// }
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -6,7 +6,7 @@ import org.springframework.context.annotation.Configuration;
|
|||||||
import com.pj.util.AjaxJson;
|
import com.pj.util.AjaxJson;
|
||||||
|
|
||||||
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
|
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
|
||||||
import cn.dev33.satoken.filter.SaFilterStrategy;
|
import cn.dev33.satoken.filter.SaFilterAuthStrategy;
|
||||||
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
|
import cn.dev33.satoken.reactor.filter.SaReactorFilter;
|
||||||
import cn.dev33.satoken.router.SaRouterUtil;
|
import cn.dev33.satoken.router.SaRouterUtil;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
@@ -33,7 +33,7 @@ public class SaTokenConfigure {
|
|||||||
* 注册 [sa-token全局过滤器-认证策略]
|
* 注册 [sa-token全局过滤器-认证策略]
|
||||||
*/
|
*/
|
||||||
@Bean
|
@Bean
|
||||||
public SaFilterStrategy getSaFilterStrategy() {
|
public SaFilterAuthStrategy getSaFilterStrategy() {
|
||||||
return r -> {
|
return r -> {
|
||||||
System.out.println("---------- 进入sa-token全局过滤器 -----------");
|
System.out.println("---------- 进入sa-token全局过滤器 -----------");
|
||||||
SaRouterUtil.match("/test/test333", () -> StpUtil.checkLogin());
|
SaRouterUtil.match("/test/test333", () -> StpUtil.checkLogin());
|
||||||
|
@@ -59,12 +59,12 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
|
|||||||
100%{box-shadow: 0 0 20px #FFF;}
|
100%{box-shadow: 0 0 20px #FFF;}
|
||||||
}
|
}
|
||||||
/* 微信二维码 */
|
/* 微信二维码 */
|
||||||
.wx-qr-box{margin-top: 50px; margin-bottom: 20px;}
|
.wx-qr-box{margin-top: 50px;}
|
||||||
.qr-item{display: inline-block;}
|
.qr-item{display: inline-block;}
|
||||||
.qr-item p{font-size: 12px; padding: 0 0.5em;}
|
.qr-item p{font-size: 12px; padding: 0 0.5em;}
|
||||||
/* .qr-item a{color: #42B983;} */
|
/* .qr-item a{color: #42B983;} */
|
||||||
.wx-qr{width: 150px;}
|
.wx-qr{width: 150px;}
|
||||||
.wx-qr-box p{margin-top: 10px; color: #666;}
|
.wx-qr-box p{margin-top: 10px; color: #666; margin-bottom: 20px;}
|
||||||
.wx-qr,.dro-qr{cursor: pointer;}
|
.wx-qr,.dro-qr{cursor: pointer;}
|
||||||
|
|
||||||
/* -------- 支持特性 --------- */
|
/* -------- 支持特性 --------- */
|
||||||
|
@@ -204,6 +204,9 @@
|
|||||||
<a href="http://www.jmwl51.com/" target="_blank">
|
<a href="http://www.jmwl51.com/" target="_blank">
|
||||||
<img src="https://oss.dev33.cn/sa-token/com/jimeng.png">
|
<img src="https://oss.dev33.cn/sa-token/com/jimeng.png">
|
||||||
</a>
|
</a>
|
||||||
|
<a href="https://uniadmin.jiangruyi.com/" target="_blank">
|
||||||
|
<img src="https://oss.dev33.cn/sa-token/com/uniadmin.png">
|
||||||
|
</a>
|
||||||
</div>
|
</div>
|
||||||
<div style="height: 10px; clear: both;"></div>
|
<div style="height: 10px; clear: both;"></div>
|
||||||
<p style="color: #666;">
|
<p style="color: #666;">
|
||||||
|
@@ -9,7 +9,9 @@ import org.springframework.web.server.ServerWebExchange;
|
|||||||
import org.springframework.web.server.WebFilter;
|
import org.springframework.web.server.WebFilter;
|
||||||
import org.springframework.web.server.WebFilterChain;
|
import org.springframework.web.server.WebFilterChain;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaTokenManager;
|
import cn.dev33.satoken.exception.SaTokenException;
|
||||||
|
import cn.dev33.satoken.filter.SaFilterAuthStrategy;
|
||||||
|
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
|
||||||
import cn.dev33.satoken.reactor.context.SaReactorHolder;
|
import cn.dev33.satoken.reactor.context.SaReactorHolder;
|
||||||
import cn.dev33.satoken.reactor.context.SaReactorSyncHolder;
|
import cn.dev33.satoken.reactor.context.SaReactorSyncHolder;
|
||||||
import cn.dev33.satoken.router.SaRouterUtil;
|
import cn.dev33.satoken.router.SaRouterUtil;
|
||||||
@@ -92,6 +94,41 @@ public class SaReactorFilter implements WebFilter {
|
|||||||
return excludeList;
|
return excludeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------ 执行函数
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 认证函数:每次请求执行
|
||||||
|
*/
|
||||||
|
public SaFilterAuthStrategy auth = r -> {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异常处理函数:每次[认证函数]发生异常时执行此函数
|
||||||
|
*/
|
||||||
|
public SaFilterErrorStrategy error = e -> {
|
||||||
|
throw new SaTokenException(e);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入[认证函数]: 每次请求执行
|
||||||
|
* @param auth see note
|
||||||
|
* @return 对象自身
|
||||||
|
*/
|
||||||
|
public SaReactorFilter setAuth(SaFilterAuthStrategy auth) {
|
||||||
|
this.auth = auth;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入[异常处理函数]:每次[认证函数]发生异常时执行此函数
|
||||||
|
* @param error see note
|
||||||
|
* @return 对象自身
|
||||||
|
*/
|
||||||
|
public SaReactorFilter setError(SaFilterErrorStrategy error) {
|
||||||
|
this.error = error;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------------------ filter
|
// ------------------------ filter
|
||||||
|
|
||||||
@@ -103,11 +140,11 @@ public class SaReactorFilter implements WebFilter {
|
|||||||
SaReactorSyncHolder.setContent(exchange);
|
SaReactorSyncHolder.setContent(exchange);
|
||||||
|
|
||||||
// 执行全局过滤器
|
// 执行全局过滤器
|
||||||
SaRouterUtil.match(includeList, excludeList, () -> SaTokenManager.getSaFilterStrategy().run(null));
|
SaRouterUtil.match(includeList, excludeList, () -> auth.run(null));
|
||||||
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
// 1. 获取异常处理策略结果
|
// 1. 获取异常处理策略结果
|
||||||
Object result = SaTokenManager.getSaFilterErrorStrategy().run(e);
|
Object result = error.run(e);
|
||||||
String resultString = String.valueOf(result);
|
String resultString = String.valueOf(result);
|
||||||
|
|
||||||
// 2. 写入输出流
|
// 2. 写入输出流
|
||||||
|
@@ -12,8 +12,6 @@ import cn.dev33.satoken.config.SaTokenConfig;
|
|||||||
import cn.dev33.satoken.context.SaTokenContext;
|
import cn.dev33.satoken.context.SaTokenContext;
|
||||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocal;
|
import cn.dev33.satoken.context.SaTokenContextForThreadLocal;
|
||||||
import cn.dev33.satoken.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
|
|
||||||
import cn.dev33.satoken.filter.SaFilterStrategy;
|
|
||||||
import cn.dev33.satoken.stp.StpInterface;
|
import cn.dev33.satoken.stp.StpInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -104,26 +102,6 @@ public class SaTokenSpringAutowired {
|
|||||||
SaTokenManager.setSaTokenContext(saTokenContext);
|
SaTokenManager.setSaTokenContext(saTokenContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 注入[sa-token全局过滤器-认证策略]
|
|
||||||
*
|
|
||||||
* @param strategy see note
|
|
||||||
*/
|
|
||||||
@Autowired(required = false)
|
|
||||||
public void setSaFilterStrategy(SaFilterStrategy strategy) {
|
|
||||||
SaTokenManager.setSaFilterStrategy(strategy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 注入[sa-token全局过滤器-异常处理策略]
|
|
||||||
*
|
|
||||||
* @param errorStrategy see note
|
|
||||||
*/
|
|
||||||
@Autowired(required = false)
|
|
||||||
public void setSaFilterErrorStrategy(SaFilterErrorStrategy errorStrategy) {
|
|
||||||
SaTokenManager.setSaFilterErrorStrategy(errorStrategy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
||||||
*
|
*
|
||||||
|
@@ -14,7 +14,7 @@ import javax.servlet.ServletResponse;
|
|||||||
|
|
||||||
import org.springframework.core.annotation.Order;
|
import org.springframework.core.annotation.Order;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaTokenManager;
|
import cn.dev33.satoken.exception.SaTokenException;
|
||||||
import cn.dev33.satoken.router.SaRouterUtil;
|
import cn.dev33.satoken.router.SaRouterUtil;
|
||||||
import cn.dev33.satoken.util.SaTokenConsts;
|
import cn.dev33.satoken.util.SaTokenConsts;
|
||||||
|
|
||||||
@@ -94,6 +94,41 @@ public class SaServletFilter implements Filter {
|
|||||||
return excludeList;
|
return excludeList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// ------------------------ 执行函数
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 认证函数:每次请求执行
|
||||||
|
*/
|
||||||
|
public SaFilterAuthStrategy auth = r -> {};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异常处理函数:每次[认证函数]发生异常时执行此函数
|
||||||
|
*/
|
||||||
|
public SaFilterErrorStrategy error = e -> {
|
||||||
|
throw new SaTokenException(e);
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入[认证函数]: 每次请求执行
|
||||||
|
* @param auth see note
|
||||||
|
* @return 对象自身
|
||||||
|
*/
|
||||||
|
public SaServletFilter setAuth(SaFilterAuthStrategy auth) {
|
||||||
|
this.auth = auth;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入[异常处理函数]:每次[认证函数]发生异常时执行此函数
|
||||||
|
* @param error see note
|
||||||
|
* @return 对象自身
|
||||||
|
*/
|
||||||
|
public SaServletFilter setError(SaFilterErrorStrategy error) {
|
||||||
|
this.error = error;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------------------ doFilter
|
// ------------------------ doFilter
|
||||||
|
|
||||||
@@ -103,11 +138,11 @@ public class SaServletFilter implements Filter {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
// 执行全局过滤器
|
// 执行全局过滤器
|
||||||
SaRouterUtil.match(includeList, excludeList, () -> SaTokenManager.getSaFilterStrategy().run(null));
|
SaRouterUtil.match(includeList, excludeList, () -> auth.run(null));
|
||||||
|
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
// 1. 获取异常处理策略结果
|
// 1. 获取异常处理策略结果
|
||||||
Object result = SaTokenManager.getSaFilterErrorStrategy().run(e);
|
Object result = error.run(e);
|
||||||
String resultString = String.valueOf(result);
|
String resultString = String.valueOf(result);
|
||||||
|
|
||||||
// 2. 写入输出流
|
// 2. 写入输出流
|
||||||
|
@@ -11,8 +11,6 @@ import cn.dev33.satoken.action.SaTokenAction;
|
|||||||
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.dao.SaTokenDao;
|
import cn.dev33.satoken.dao.SaTokenDao;
|
||||||
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
|
|
||||||
import cn.dev33.satoken.filter.SaFilterStrategy;
|
|
||||||
import cn.dev33.satoken.stp.StpInterface;
|
import cn.dev33.satoken.stp.StpInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -95,26 +93,6 @@ public class SaTokenSpringAutowired {
|
|||||||
SaTokenManager.setSaTokenContext(saTokenContext);
|
SaTokenManager.setSaTokenContext(saTokenContext);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 注入[sa-token全局过滤器-认证策略]
|
|
||||||
*
|
|
||||||
* @param strategy see note
|
|
||||||
*/
|
|
||||||
@Autowired(required = false)
|
|
||||||
public void setSaFilterStrategy(SaFilterStrategy strategy) {
|
|
||||||
SaTokenManager.setSaFilterStrategy(strategy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 注入[sa-token全局过滤器-异常处理策略]
|
|
||||||
*
|
|
||||||
* @param errorStrategy see note
|
|
||||||
*/
|
|
||||||
@Autowired(required = false)
|
|
||||||
public void setSaFilterErrorStrategy(SaFilterErrorStrategy errorStrategy) {
|
|
||||||
SaTokenManager.setSaFilterErrorStrategy(errorStrategy);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
* 利用自动匹配特性,获取SpringMVC框架内部使用的路由匹配器
|
||||||
*
|
*
|
||||||
|
Reference in New Issue
Block a user