mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-20 18:47:25 +08:00
重构:全局过滤器执行函数放到成员变量里
This commit is contained in:
@@ -5,7 +5,7 @@ package cn.dev33.satoken.filter;
|
||||
* @author kong
|
||||
*
|
||||
*/
|
||||
public interface SaFilterStrategy {
|
||||
public interface SaFilterAuthStrategy {
|
||||
|
||||
/**
|
||||
* 执行方法
|
@@ -122,7 +122,7 @@ sa-token API 众多,请恕此处无法为您逐一展示,更多示例请戳
|
||||
|
||||
|
||||
## Star 趋势
|
||||
[](https://giteye.net/chart/77YQZ6UK)
|
||||
[](https://giteye.net/chart/77YQZ6UK)
|
||||
|
||||
[](https://starchart.cc/dromara/sa-token)
|
||||
|
||||
|
@@ -10,7 +10,7 @@
|
||||
|
||||
User-Session和Token-Session到底有什么不同?下面这张图可以解释两者的区别
|
||||
|
||||

|
||||

|
||||
|
||||
简而言之:
|
||||
- `Token-Session` 以token为主,只要token不同,那么对应的Session对象就不同
|
||||
|
@@ -20,6 +20,11 @@ body{font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Roboto,Oxygen,Ubuntu
|
||||
|
||||
/* GitHub折线图最大宽度 */
|
||||
[alt=github-chart]{max-width: 897px;}
|
||||
/* 大屏幕时,某些图片限制一下宽度 */
|
||||
@media screen and (min-width: 800px) {
|
||||
[title=s-w]{max-width: 80%;}
|
||||
}
|
||||
|
||||
|
||||
/* 公众号table */
|
||||
.gzh-table{ /* table-layout:fixed !important; */}
|
||||
|
@@ -33,50 +33,39 @@ public class SaTokenConfigure {
|
||||
*/
|
||||
@Bean
|
||||
public SaServletFilter getSaReactorFilter() {
|
||||
return new SaServletFilter()
|
||||
.addInclude("/**")
|
||||
.addExclude("/favicon.ico");
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册 [sa-token全局过滤器-认证策略]
|
||||
*/
|
||||
@Bean
|
||||
public SaFilterStrategy getSaFilterStrategy() {
|
||||
return r -> {
|
||||
System.out.println("---------- 进入sa-token全局过滤器 -----------");
|
||||
|
||||
// 登录验证 -- 拦截所有路由,并排除/user/doLogin 用于开放登录
|
||||
SaRouterUtil.match("/**", "/user/doLogin", () -> StpUtil.checkLogin());
|
||||
|
||||
// 权限认证 -- 不同模块, 校验不同权限
|
||||
SaRouterUtil.match("/user/**", () -> StpUtil.checkPermission("user"));
|
||||
SaRouterUtil.match("/admin/**", () -> StpUtil.checkPermission("admin"));
|
||||
SaRouterUtil.match("/goods/**", () -> StpUtil.checkPermission("goods"));
|
||||
SaRouterUtil.match("/orders/**", () -> StpUtil.checkPermission("orders"));
|
||||
SaRouterUtil.match("/notice/**", () -> StpUtil.checkPermission("notice"));
|
||||
SaRouterUtil.match("/comment/**", () -> StpUtil.checkPermission("comment"));
|
||||
|
||||
// 匹配 restful 风格路由
|
||||
SaRouterUtil.match("/article/get/{id}", () -> StpUtil.checkPermission("article"));
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* 注册 [sa-token全局过滤器-异常处理策略]
|
||||
*/
|
||||
@Bean
|
||||
public SaFilterErrorStrategy getSaFilterErrorStrategy() {
|
||||
return e -> AjaxJson.getError(e.getMessage());
|
||||
return new SaServletFilter()
|
||||
|
||||
// 指定 [拦截路由]
|
||||
.addInclude("/**")
|
||||
|
||||
// 指定 [放行路由]
|
||||
.addExclude("/favicon.ico")
|
||||
|
||||
// 指定[认证函数]: 每次请求执行
|
||||
.setAuth(r -> {
|
||||
System.out.println("---------- 进入sa-token全局认证 -----------");
|
||||
|
||||
// 登录验证 -- 拦截所有路由,并排除/user/doLogin 用于开放登录
|
||||
SaRouterUtil.match("/**", "/user/doLogin", () -> StpUtil.checkLogin());
|
||||
|
||||
// 更多拦截处理方式,请参考“路由拦截式鉴权”章节
|
||||
})
|
||||
|
||||
// 指定[异常处理函数]:每次[认证函数]发生异常时执行此函数
|
||||
.setError(e -> {
|
||||
System.out.println("---------- 进入sa-token异常处理 -----------");
|
||||
return AjaxJson.getError(e.getMessage());
|
||||
})
|
||||
;
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
|
||||
### 注意事项
|
||||
- 在`[认证策略]`里,你可以和拦截器里一致的代码,进行路由匹配鉴权
|
||||
- 由于过滤器中抛出的异常不进入全局异常处理,所以你必须提供`[异常处理策略]`来处理`[认证策略]`里抛出的异常
|
||||
- 在`[异常处理策略]`里的返回值,将作为字符串输出到前端,如果需要定制化返回数据,请注意其中的格式转换
|
||||
- 在`[认证函数]`里,你可以写和拦截器里一致的代码,进行路由匹配鉴权,参考:[路由拦截式鉴权](/use/route-check)
|
||||
- 由于过滤器中抛出的异常不进入全局异常处理,所以你必须提供`[异常处理函数]`来处理`[认证函数]`里抛出的异常
|
||||
- 在`[异常处理函数]`里的返回值,将作为字符串输出到前端,如果需要定制化返回数据,请注意其中的格式转换
|
||||
|
||||
|
||||
### 在WebFlux中使用过滤器
|
||||
@@ -96,12 +85,10 @@ public class SaTokenConfigure {
|
||||
@Bean
|
||||
public SaReactorFilter getSaReactorFilter() {
|
||||
return new SaReactorFilter()
|
||||
.addInclude("/**")
|
||||
.addExclude("/favicon.ico");
|
||||
// 其它代码...
|
||||
;
|
||||
}
|
||||
|
||||
// 其它代码 ...
|
||||
|
||||
}
|
||||
```
|
||||
|
@@ -35,7 +35,7 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
|
||||
.title-logo:hover{transform: scale(1.2, 1.2);} */
|
||||
|
||||
.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;}
|
||||
/* .content-box p{line-height: 30px; padding: 0px 1em;} */
|
||||
|
||||
.main-box{animation: changes 60s 0.2s linear infinite normal; background-attachment: ;} /* normal | alternate */
|
||||
@keyframes changes {
|
||||
@@ -60,6 +60,9 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
|
||||
}
|
||||
/* 微信二维码 */
|
||||
.wx-qr-box{margin-top: 50px; margin-bottom: 20px;}
|
||||
.qr-item{display: inline-block;}
|
||||
.qr-item p{font-size: 12px; padding: 0 0.5em;}
|
||||
/* .qr-item a{color: #42B983;} */
|
||||
.wx-qr{width: 150px;}
|
||||
.wx-qr-box p{margin-top: 10px; color: #666;}
|
||||
.wx-qr,.dro-qr{cursor: pointer;}
|
||||
@@ -134,6 +137,11 @@ body{font-size: 16px; color: #34495E; font-family: "Source Sans Pro","Helvetica
|
||||
footer{position: static; line-height: 40px;}
|
||||
}
|
||||
|
||||
@media screen and (min-width: 1700px) {
|
||||
.content-box h1{margin-top: 120px;}
|
||||
}
|
||||
|
||||
|
||||
/* 闪光背景 */
|
||||
/* .main-box{
|
||||
background-size: 500%;
|
||||
|
@@ -51,7 +51,7 @@
|
||||
<div class="btn-box">
|
||||
<a href="https://github.com/dromara/sa-token" target="_blank">GitHub</a>
|
||||
<a href="https://gitee.com/dromara/sa-token" target="_blank">码云</a>
|
||||
<a href="https://jq.qq.com/?_wv=1027&k=45H977HM" target="_blank">加QQ群</a>
|
||||
<!-- <a href="https://jq.qq.com/?_wv=1027&k=45H977HM" target="_blank">加QQ群</a> -->
|
||||
<!-- <a href="http://sa-app.dev33.cn/wall.html?name=sa-token" target="_blank">需求墙</a> -->
|
||||
<a href="doc/index.html" target="_self" class="doc-btn">开发文档</a>
|
||||
</div>
|
||||
@@ -65,8 +65,14 @@
|
||||
<a href="https://github.com/dromara/sa-token/blob/master/LICENSE"><img src="https://img.shields.io/github/license/dromara/sa-token.svg?style=flat-square"></a>
|
||||
</h4>
|
||||
<div class="wx-qr-box">
|
||||
<img class="wx-qr" src="https://oss.dev33.cn/sa-token/wx-qr-300.png" >
|
||||
<p style="">(扫码加入微信交流群,请备注: sa)</p>
|
||||
<div class="qr-item">
|
||||
<img class="wx-qr" src="https://oss.dev33.cn/sa-token/qq-group.png" >
|
||||
<p style="">QQ交流群: 1002350610 <a href="https://jq.qq.com/?_wv=1027&k=45H977HM" target="_blank">点击加入</a></p>
|
||||
</div>
|
||||
<div class="qr-item">
|
||||
<img class="wx-qr" src="https://oss.dev33.cn/sa-token/wx-qr-300.png" >
|
||||
<p>微信交流群:扫码加入 (请备注: sa)</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
Reference in New Issue
Block a user