全局过滤器增加限定[拦截路径]与[排除路径]功能

This commit is contained in:
shengzhang 2021-04-15 00:45:03 +08:00
parent 2fcf71af41
commit b7859b67d3
7 changed files with 184 additions and 16 deletions

View File

@ -21,7 +21,17 @@ public class SaTokenConfigure implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 注册注解拦截器
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**");
registry.addInterceptor(new SaAnnotationInterceptor()).addPathPatterns("/**").excludePathPatterns("");
}
// /**
// * 注册 [sa-token全局过滤器]
// */
// @Bean
// public SaServletFilter getSaReactorFilter() {
// return new SaServletFilter()
// .addInclude("/**")
// .addExclude("/favicon.ico");
// }
}

View File

@ -24,7 +24,9 @@ public class SaTokenConfigure {
*/
@Bean
public SaReactorFilter getSaReactorFilter() {
return new SaReactorFilter();
return new SaReactorFilter()
.addInclude("/**")
.addExclude("/favicon.ico");
}
/**

View File

@ -33,7 +33,9 @@ public class SaTokenConfigure {
*/
@Bean
public SaServletFilter getSaReactorFilter() {
return new SaServletFilter();
return new SaServletFilter()
.addInclude("/**")
.addExclude("/favicon.ico");
}
/**
@ -93,7 +95,9 @@ public class SaTokenConfigure {
*/
@Bean
public SaReactorFilter getSaReactorFilter() {
return new SaReactorFilter();
return new SaReactorFilter()
.addInclude("/**")
.addExclude("/favicon.ico");
}
// 其它代码 ...

View File

@ -29,12 +29,12 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
.main-box{display: flex; align-items: center; text-align: center; }
.fenge{min-height: 90px;}
.content-box{color: #000; flex: 1;}
.content-box h1{font-size: 90px; font-weight: 400; position: relative; margin-top: 40px; /* margin-top: 15vh; */}
.content-box h1{font-size: 100px; font-weight: 400; position: relative; margin-top: 40px; /* margin-top: 15vh; */}
.content-box h1 small{font-size: 18px; position: absolute; bottom: 10px; margin-left: 5px; font-weight: 100;}
.title-logo{width: 221px; cursor: pointer; transition: all 0.2s;}
.title-logo:hover{transform: scale(1.2, 1.2);}
/* .title-logo{width: 221px; cursor: pointer; transition: all 0.2s;}
.title-logo:hover{transform: scale(1.2, 1.2);} */
.sub-title{font-size: 24px; font-weight: 400; margin-top: 30px; margin-bottom: 25px; color: #6a8bad; color: #444;}
.sub-title{font-size: 26px; font-weight: 400; margin-top: 30px; margin-bottom: 25px; color: #6a8bad; color: #444;}
.content-box p{line-height: 30px; padding: 0px 1em;}
.main-box{animation: changes 60s 0.2s linear infinite normal; background-attachment: ;} /* normal | alternate */
@ -44,12 +44,12 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
}
/* 几个按钮 */
.btn-box{margin-top: 50px; margin-bottom: 28px;}
.btn-box{margin-top: 50px; margin-bottom: 40px;}
.btn-box a{border: 1px #42B983 solid; border-radius: 2em; box-sizing: border-box; color: #3eaf7c; display: inline-block;transition: all 0.1s;}
.btn-box a{font-size: 14px; background-color: rgba(0,255,0,0.03); letter-spacing: 1px; margin: 0 0.5em; padding: 0.8em 1.5em; margin-bottom: 14px; text-decoration: none; }
.btn-box a{font-size: 14px; background-color: rgba(0,255,0,0.04); letter-spacing: 1px; padding: 1em 2em; margin: 0 0.5em; margin-bottom: 14px; text-decoration: none; }
/* 最后一个加深底色 */
.btn-box a:last-child {color: #fff; background-color: #42B983; border: 1px green solid;}
.btn-box a:hover{/* transform: scale(1.05, 1.05); */padding: 0.8em 1.7em;margin: 0 0.3em;}
.btn-box a:hover{/* transform: scale(1.05, 1.05); */padding: 1em 2.3em; margin: 0 0.2em;}
/* 按钮发光动画 */
.btn-box a:last-child{animation: bganimation 3s infinite;}
@ -59,7 +59,7 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
100%{box-shadow: 0 0 20px #FFF;}
}
/* 微信二维码 */
.wx-qr-box{margin-top: 40px; margin-bottom: 20px;}
.wx-qr-box{margin-top: 50px; margin-bottom: 20px;}
.wx-qr{width: 150px;}
.wx-qr-box p{margin-top: 10px; color: #666;}
.wx-qr,.dro-qr{cursor: pointer;}

View File

@ -66,7 +66,7 @@
</h4>
<div class="wx-qr-box">
<img class="wx-qr" src="https://oss.dev33.cn/sa-token/wx-qr-300.png" >
<p style="">(扫码加微信,备注: sa-token邀您加入技术群聊</p>
<p style="">(扫码加微信交流群备注: sa</p>
</div>
</div>
</div>

View File

@ -1,5 +1,9 @@
package cn.dev33.satoken.reactor.filter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.springframework.core.annotation.Order;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.server.WebFilter;
@ -8,6 +12,7 @@ import org.springframework.web.server.WebFilterChain;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.reactor.context.SaReactorHolder;
import cn.dev33.satoken.reactor.context.SaReactorSyncHolder;
import cn.dev33.satoken.router.SaRouterUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import reactor.core.publisher.Mono;
@ -19,6 +24,77 @@ import reactor.core.publisher.Mono;
@Order(SaTokenConsts.ASSEMBLY_ORDER)
public class SaReactorFilter implements WebFilter {
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
/**
* 拦截路由
*/
private List<String> includeList = new ArrayList<>();
/**
* 放行路由
*/
private List<String> excludeList = new ArrayList<>();
/**
* 添加 [拦截路由]
* @param paths 路由
* @return 对象自身
*/
public SaReactorFilter addInclude(String... paths) {
includeList.addAll(Arrays.asList(paths));
return this;
}
/**
* 添加 [放行路由]
* @param paths 路由
* @return 对象自身
*/
public SaReactorFilter addExclude(String... paths) {
excludeList.addAll(Arrays.asList(paths));
return this;
}
/**
* 写入 [拦截路由] 集合
* @param pathList 路由集合
* @return 对象自身
*/
public SaReactorFilter setIncludeList(List<String> pathList) {
includeList = pathList;
return this;
}
/**
* 写入 [放行路由] 集合
* @param pathList 路由集合
* @return 对象自身
*/
public SaReactorFilter setExcludeList(List<String> pathList) {
excludeList = pathList;
return this;
}
/**
* 获取 [拦截路由] 集合
* @return see note
*/
public List<String> getIncludeList() {
return includeList;
}
/**
* 获取 [放行路由] 集合
* @return see note
*/
public List<String> getExcludeList() {
return excludeList;
}
// ------------------------ filter
@Override
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
// ---------- 全局认证处理
@ -27,7 +103,7 @@ public class SaReactorFilter implements WebFilter {
SaReactorSyncHolder.setContent(exchange);
// 执行全局过滤器
SaTokenManager.getSaFilterStrategy().run(null);
SaRouterUtil.match(includeList, excludeList, () -> SaTokenManager.getSaFilterStrategy().run(null));
} catch (Throwable e) {
// 1. 获取异常处理策略结果
@ -61,5 +137,4 @@ public class SaReactorFilter implements WebFilter {
});
}
}

View File

@ -1,6 +1,9 @@
package cn.dev33.satoken.filter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
@ -12,6 +15,7 @@ import javax.servlet.ServletResponse;
import org.springframework.core.annotation.Order;
import cn.dev33.satoken.SaTokenManager;
import cn.dev33.satoken.router.SaRouterUtil;
import cn.dev33.satoken.util.SaTokenConsts;
/**
@ -22,13 +26,84 @@ import cn.dev33.satoken.util.SaTokenConsts;
@Order(SaTokenConsts.ASSEMBLY_ORDER)
public class SaServletFilter implements Filter {
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
/**
* 拦截路由
*/
private List<String> includeList = new ArrayList<>();
/**
* 放行路由
*/
private List<String> excludeList = new ArrayList<>();
/**
* 添加 [拦截路由]
* @param paths 路由
* @return 对象自身
*/
public SaServletFilter addInclude(String... paths) {
includeList.addAll(Arrays.asList(paths));
return this;
}
/**
* 添加 [放行路由]
* @param paths 路由
* @return 对象自身
*/
public SaServletFilter addExclude(String... paths) {
excludeList.addAll(Arrays.asList(paths));
return this;
}
/**
* 写入 [拦截路由] 集合
* @param pathList 路由集合
* @return 对象自身
*/
public SaServletFilter setIncludeList(List<String> pathList) {
includeList = pathList;
return this;
}
/**
* 写入 [放行路由] 集合
* @param pathList 路由集合
* @return 对象自身
*/
public SaServletFilter setExcludeList(List<String> pathList) {
excludeList = pathList;
return this;
}
/**
* 获取 [拦截路由] 集合
* @return see note
*/
public List<String> getIncludeList() {
return includeList;
}
/**
* 获取 [放行路由] 集合
* @return see note
*/
public List<String> getExcludeList() {
return excludeList;
}
// ------------------------ doFilter
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
throws IOException, ServletException {
try {
// 执行全局过滤器
SaTokenManager.getSaFilterStrategy().run(null);
SaRouterUtil.match(includeList, excludeList, () -> SaTokenManager.getSaFilterStrategy().run(null));
} catch (Throwable e) {
// 1. 获取异常处理策略结果
@ -53,4 +128,6 @@ public class SaServletFilter implements Filter {
public void destroy() {
}
}