This commit is contained in:
click33 2022-09-30 15:52:16 +08:00
commit abf9affb3c
11 changed files with 122 additions and 72 deletions

View File

@ -18,7 +18,7 @@
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon-web</artifactId>
<version>1.10.0</version>
<version>1.10.4</version>
</dependency>
<!-- Sa-Token 权限认证, 在线文档http://sa-token.dev33.cn/ -->

View File

@ -21,8 +21,8 @@ public class SaTokenConfigure {
* 注册 [sa-token全局过滤器]
*/
@Bean
public SaTokenPathFilter tokenPathFilter() {
return new SaTokenPathFilter()
public void tokenPathFilter() {
Solon.app().before(new SaTokenPathInterceptor()
// 指定 [拦截路由] [放行路由]
.addInclude("/**").addExclude("/favicon.ico")
@ -54,7 +54,6 @@ public class SaTokenConfigure {
.setHeader("X-Content-Type-Options", "nosniff")
;
})
;
);
}
}

View File

@ -20,7 +20,7 @@
<dependency>
<groupId>org.noear</groupId>
<artifactId>solon</artifactId>
<version>1.10.0</version>
<version>1.10.4</version>
</dependency>
<dependency>

View File

@ -71,10 +71,11 @@ public class XPluginImp implements Plugin {
});
// 注入侦听器 Bean
EventBus.subscribe(SaTokenListener.class, bw->{
SaTokenEventCenter.registerListener(bw);
context.subBean(SaTokenListener.class, sl->{
SaTokenEventCenter.registerListener(sl);
});
// 注入权限认证 Bean
context.getWrapAsyn(StpInterface.class, bw->{
SaManager.setStpInterface(bw.raw());

View File

@ -1,24 +1,40 @@
package cn.dev33.satoken.solon.integration;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.strategy.SaStrategy;
import org.noear.solon.core.aspect.Interceptor;
import org.noear.solon.core.aspect.Invocation;
import org.noear.solon.core.handle.Context;
import cn.dev33.satoken.strategy.SaStrategy;
import java.lang.reflect.Method;
/**
* @author noear
* @since 1.4
* @deprecated 1.10改用 SaTokenPathInterceptor
*/
@Deprecated
public class SaTokenAnnotationInterceptor implements Interceptor {
public static final SaTokenAnnotationInterceptor INSTANCE = new SaTokenAnnotationInterceptor();
@Override
public Object doIntercept(Invocation inv) throws Throwable {
// 注解鉴权
SaStrategy.me.checkMethodAnnotation.accept(inv.method().getMethod());
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
Context ctx = Context.current();
if (ctx != null && "1".equals(ctx.attr("_SaTokenPathInterceptor"))) {
// 执行原有逻辑
return inv.invoke();
} else {
Method method = inv.method().getMethod();
if (SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class) == false) {
SaStrategy.me.checkMethodAnnotation.accept(method);
}
// 执行原有逻辑
return inv.invoke();
}
}
}

View File

@ -16,9 +16,14 @@ import java.util.Arrays;
import java.util.List;
/**
* sa-token 基于路由的过滤式鉴权( +SaTokenAnnotationInterceptor )
*
* @author noear
* @since 1.9
* @deprecated 1.10改用 SaTokenPathInterceptor
* @see SaTokenPathInterceptor
*/
@Deprecated
public class SaTokenPathFilter implements Filter {
// ------------------------ 设置此过滤器 拦截 & 放行 的路由

View File

@ -1,23 +1,35 @@
package cn.dev33.satoken.solon.integration;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.exception.BackResultException;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.StopMatchException;
import cn.dev33.satoken.filter.SaFilterAuthStrategy;
import cn.dev33.satoken.filter.SaFilterErrorStrategy;
import cn.dev33.satoken.router.SaRouter;
import cn.dev33.satoken.strategy.SaStrategy;
import org.noear.solon.core.handle.Action;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.handle.Handler;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/**
* sa-token基于路由的拦截式鉴权
* sa-token 基于路由的拦截式鉴权增加了注解的处理
*
* @author kong
* @since 1.9
* @author noear
* @since 1.10
*/
public class SaTokenPathInterceptor implements Handler {
/**
* 是否打开注解鉴权
*/
public boolean isAnnotation = true;
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
@ -156,10 +168,30 @@ public class SaTokenPathInterceptor implements Handler {
@Override
public void handle(Context ctx) throws Throwable {
try {
// 执行全局过滤器
//注处处理
Action action = ctx.action();
if(isAnnotation && action != null){
ctx.attrSet("_SaTokenPathInterceptor", "1");
// 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod();
// 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if(SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
return;
}
// 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method);
}else{
ctx.attrSet("_SaTokenPathInterceptor", "0");
}
//路径规则处理
SaRouter.match(includeList).notMatch(excludeList).check(r -> {
beforeAuth.run(null);
auth.run(null);
beforeAuth.run(action);
auth.run(action);
});
} catch (StopMatchException e) {

View File

@ -1,12 +1,11 @@
package cn.dev33.satoken.solon.model;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.util.PathAnalyzer;
import cn.dev33.satoken.context.SaTokenContext;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.context.model.SaStorage;
import org.noear.solon.core.handle.Context;
import org.noear.solon.core.util.PathAnalyzer;
/**
* @author noear

View File

@ -1,10 +1,9 @@
package cn.dev33.satoken.solon.model;
import org.noear.solon.core.handle.Context;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.util.SaFoxUtil;
import org.noear.solon.core.handle.Context;
/**
* @author noear

View File

@ -1,8 +1,7 @@
package cn.dev33.satoken.solon.model;
import org.noear.solon.core.handle.Context;
import cn.dev33.satoken.context.model.SaResponse;
import org.noear.solon.core.handle.Context;
/**
* @author noear