sa-token-solon-plugin:升级 solon 为 1.10.9;调整 SaTokenPathFilter 让其支持注解验证;删除 SaTokenAnnotationInterceptor(不再需要了)

This commit is contained in:
noear 2022-10-25 20:55:37 +08:00
parent 39915d2403
commit 0300a29f55
6 changed files with 177 additions and 219 deletions

View File

@ -50,7 +50,7 @@
<jackson-datatype-jsr310.version>2.11.2</jackson-datatype-jsr310.version> <jackson-datatype-jsr310.version>2.11.2</jackson-datatype-jsr310.version>
<servlet-api.version>3.1.0</servlet-api.version> <servlet-api.version>3.1.0</servlet-api.version>
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version> <thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<solon.version>1.10.4</solon.version> <solon.version>1.10.9</solon.version>
<solon-test.version>1.9.1</solon-test.version> <solon-test.version>1.9.1</solon-test.version>
<noear-redisx.version>1.4.3</noear-redisx.version> <noear-redisx.version>1.4.3</noear-redisx.version>
<jfinal.version>4.9.17</jfinal.version> <jfinal.version>4.9.17</jfinal.version>

View File

@ -18,7 +18,7 @@
<dependency> <dependency>
<groupId>org.noear</groupId> <groupId>org.noear</groupId>
<artifactId>solon-web</artifactId> <artifactId>solon-web</artifactId>
<version>1.10.4</version> <version>${solon.version}</version>
</dependency> </dependency>
<!-- Sa-Token 权限认证, 在线文档https://sa-token.cc/ --> <!-- Sa-Token 权限认证, 在线文档https://sa-token.cc/ -->

View File

@ -5,12 +5,6 @@ import org.noear.solon.core.AopContext;
import org.noear.solon.core.Plugin; import org.noear.solon.core.Plugin;
import cn.dev33.satoken.SaManager; import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.SaCheckBasic;
import cn.dev33.satoken.annotation.SaCheckDisable;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaCheckSafe;
import cn.dev33.satoken.basic.SaBasicTemplate; import cn.dev33.satoken.basic.SaBasicTemplate;
import cn.dev33.satoken.basic.SaBasicUtil; import cn.dev33.satoken.basic.SaBasicUtil;
import cn.dev33.satoken.config.SaTokenConfig; import cn.dev33.satoken.config.SaTokenConfig;
@ -23,7 +17,6 @@ import cn.dev33.satoken.listener.SaTokenEventCenter;
import cn.dev33.satoken.listener.SaTokenListener; import cn.dev33.satoken.listener.SaTokenListener;
import cn.dev33.satoken.same.SaSameTemplate; import cn.dev33.satoken.same.SaSameTemplate;
import cn.dev33.satoken.sign.SaSignTemplate; import cn.dev33.satoken.sign.SaSignTemplate;
import cn.dev33.satoken.solon.integration.SaTokenAnnotationInterceptor;
import cn.dev33.satoken.solon.model.SaContextForSolon; import cn.dev33.satoken.solon.model.SaContextForSolon;
import cn.dev33.satoken.stp.StpInterface; import cn.dev33.satoken.stp.StpInterface;
import cn.dev33.satoken.stp.StpLogic; import cn.dev33.satoken.stp.StpLogic;
@ -39,13 +32,6 @@ public class XPluginImp implements Plugin {
@Override @Override
public void start(AopContext context) { public void start(AopContext context) {
context.beanAroundAdd(SaCheckPermission.class, SaTokenAnnotationInterceptor.INSTANCE);
context.beanAroundAdd(SaCheckRole.class, SaTokenAnnotationInterceptor.INSTANCE);
context.beanAroundAdd(SaCheckLogin.class, SaTokenAnnotationInterceptor.INSTANCE);
context.beanAroundAdd(SaCheckSafe.class, SaTokenAnnotationInterceptor.INSTANCE);
context.beanAroundAdd(SaCheckDisable.class, SaTokenAnnotationInterceptor.INSTANCE);
context.beanAroundAdd(SaCheckBasic.class, SaTokenAnnotationInterceptor.INSTANCE);
//集成初始化 //集成初始化
// 注入上下文Bean // 注入上下文Bean

View File

@ -1,40 +0,0 @@
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 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 {
// 如果此 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

@ -1,197 +1,215 @@
package cn.dev33.satoken.solon.integration; package cn.dev33.satoken.solon.integration;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.exception.BackResultException; import cn.dev33.satoken.exception.BackResultException;
import cn.dev33.satoken.exception.SaTokenException; import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.exception.StopMatchException; import cn.dev33.satoken.exception.StopMatchException;
import cn.dev33.satoken.filter.SaFilterAuthStrategy; import cn.dev33.satoken.filter.SaFilterAuthStrategy;
import cn.dev33.satoken.filter.SaFilterErrorStrategy; import cn.dev33.satoken.filter.SaFilterErrorStrategy;
import cn.dev33.satoken.router.SaRouter; import cn.dev33.satoken.router.SaRouter;
import org.noear.solon.Utils; import cn.dev33.satoken.strategy.SaStrategy;
import org.noear.solon.core.handle.Context; import org.noear.solon.Solon;
import org.noear.solon.core.handle.Filter; import org.noear.solon.core.handle.*;
import org.noear.solon.core.handle.FilterChain;
import java.lang.reflect.Method;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
* sa-token 基于路由的过滤式鉴权( +SaTokenAnnotationInterceptor ) * sa-token 基于路由的过滤式鉴权增加了注解的处理使用优先级要低些
* *
* @author noear * @author noear
* @since 1.9 * @since 1.10
* @deprecated 1.10改用 SaTokenPathInterceptor
* @see SaTokenPathInterceptor
*/ */
@Deprecated
public class SaTokenPathFilter implements Filter { public class SaTokenPathFilter implements Filter {
/**
* 是否打开注解鉴权
*/
public boolean isAnnotation = true;
// ------------------------ 设置此过滤器 拦截 & 放行 的路由 // ------------------------ 设置此过滤器 拦截 & 放行 的路由
/** /**
* 拦截路由 * 拦截路由
*/ */
protected List<String> includeList = new ArrayList<>(); protected List<String> includeList = new ArrayList<>();
/** /**
* 放行路由 * 放行路由
*/ */
protected List<String> excludeList = new ArrayList<>(); protected List<String> excludeList = new ArrayList<>();
/** /**
* 添加 [拦截路由] * 添加 [拦截路由]
* *
* @param paths 路由 * @param paths 路由
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenPathFilter addInclude(String... paths) { public SaTokenPathFilter addInclude(String... paths) {
includeList.addAll(Arrays.asList(paths)); includeList.addAll(Arrays.asList(paths));
return this; return this;
} }
/** /**
* 添加 [放行路由] * 添加 [放行路由]
* *
* @param paths 路由 * @param paths 路由
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenPathFilter addExclude(String... paths) { public SaTokenPathFilter addExclude(String... paths) {
excludeList.addAll(Arrays.asList(paths)); excludeList.addAll(Arrays.asList(paths));
return this; return this;
} }
/** /**
* 写入 [拦截路由] 集合 * 写入 [拦截路由] 集合
* *
* @param pathList 路由集合 * @param pathList 路由集合
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenPathFilter setIncludeList(List<String> pathList) { public SaTokenPathFilter setIncludeList(List<String> pathList) {
includeList = pathList; includeList = pathList;
return this; return this;
} }
/** /**
* 写入 [放行路由] 集合 * 写入 [放行路由] 集合
* *
* @param pathList 路由集合 * @param pathList 路由集合
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenPathFilter setExcludeList(List<String> pathList) { public SaTokenPathFilter setExcludeList(List<String> pathList) {
excludeList = pathList; excludeList = pathList;
return this; return this;
} }
/** /**
* 获取 [拦截路由] 集合 * 获取 [拦截路由] 集合
* *
* @return see note * @return see note
*/ */
public List<String> getIncludeList() { public List<String> getIncludeList() {
return includeList; return includeList;
} }
/** /**
* 获取 [放行路由] 集合 * 获取 [放行路由] 集合
* *
* @return see note * @return see note
*/ */
public List<String> getExcludeList() { public List<String> getExcludeList() {
return excludeList; return excludeList;
} }
// ------------------------ 钩子函数 // ------------------------ 钩子函数
/** /**
* 认证函数每次请求执行 * 认证函数每次请求执行
*/ */
protected SaFilterAuthStrategy auth = r -> { protected SaFilterAuthStrategy auth = r -> {
}; };
/** /**
* 异常处理函数每次[认证函数]发生异常时执行此函数 * 异常处理函数每次[认证函数]发生异常时执行此函数
*/ */
protected SaFilterErrorStrategy error = e -> { protected SaFilterErrorStrategy error = e -> {
if (e instanceof SaTokenException) { if (e instanceof SaTokenException) {
throw (SaTokenException) e; throw (SaTokenException) e;
} else { } else {
throw new SaTokenException(e); throw new SaTokenException(e);
} }
}; };
/** /**
* 前置函数在每次[认证函数]之前执行 * 前置函数在每次[认证函数]之前执行
*/ */
protected SaFilterAuthStrategy beforeAuth = r -> { protected SaFilterAuthStrategy beforeAuth = r -> {
}; };
/** /**
* 写入[认证函数]: 每次请求执行 * 写入[认证函数]: 每次请求执行
* *
* @param auth see note * @param auth see note
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenPathFilter setAuth(SaFilterAuthStrategy auth) { public SaTokenPathFilter setAuth(SaFilterAuthStrategy auth) {
this.auth = auth; this.auth = auth;
return this; return this;
} }
/** /**
* 写入[异常处理函数]每次[认证函数]发生异常时执行此函数 * 写入[异常处理函数]每次[认证函数]发生异常时执行此函数
* *
* @param error see note * @param error see note
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenPathFilter setError(SaFilterErrorStrategy error) { public SaTokenPathFilter setError(SaFilterErrorStrategy error) {
this.error = error; this.error = error;
return this; return this;
} }
/** /**
* 写入[前置函数]在每次[认证函数]之前执行 * 写入[前置函数]在每次[认证函数]之前执行
* *
* @param beforeAuth see note * @param beforeAuth see note
* @return 对象自身 * @return 对象自身
*/ */
public SaTokenPathFilter setBeforeAuth(SaFilterAuthStrategy beforeAuth) { public SaTokenPathFilter setBeforeAuth(SaFilterAuthStrategy beforeAuth) {
this.beforeAuth = beforeAuth; this.beforeAuth = beforeAuth;
return this; return this;
} }
@Override @Override
public void doFilter(Context ctx, FilterChain chain) throws Throwable { public void doFilter(Context ctx, FilterChain chain) throws Throwable {
try { try {
// 执行全局过滤器 //查找当前主处理
SaRouter.match(includeList).notMatch(excludeList).check(r -> { Handler mainHandler = Solon.app().router().matchMain(ctx);
beforeAuth.run(null);
auth.run(null);
});
} catch (StopMatchException e) { //如果是静态文件则不处理静态文件不在路由中
if (mainHandler != null) {
Action action = (mainHandler instanceof Action ? (Action) mainHandler : null);
} catch (SaTokenException e) { if (isAnnotation && action != null) {
// 1. 获取异常处理策略结果 // 获取此请求对应的 Method 处理函数
Object result; Method method = action.method().getMethod();
if (e instanceof BackResultException) {
result = e.getMessage();
} else {
result = error.run(e);
}
// 2. 写入输出流 // 如果此 Method 或其所属 Class 标注了 @SaIgnore则忽略掉鉴权
if(result != null) { if (SaStrategy.me.isAnnotationPresent.apply(method, SaIgnore.class)) {
ctx.render(result); return;
} }
ctx.setHandled(true);
return;
} catch (Throwable e) {
// 异常解包
throw Utils.throwableUnwrap(e); //solon 的最后层还有保底处理
}
// 执行 // 注解校验
chain.doFilter(ctx); SaStrategy.me.checkMethodAnnotation.accept(method);
} }
//路径规则处理
SaRouter.match(includeList).notMatch(excludeList).check(r -> {
beforeAuth.run(mainHandler);
auth.run(mainHandler);
});
}
} catch (StopMatchException e) {
} catch (SaTokenException e) {
// 1. 获取异常处理策略结果
Object result;
if (e instanceof BackResultException) {
result = e.getMessage();
} else {
result = error.run(e);
}
// 2. 写入输出流
if (result != null) {
ctx.render(result);
}
ctx.setHandled(true);
return;
}
chain.doFilter(ctx);
}
} }

View File

@ -18,10 +18,8 @@ import java.util.Arrays;
import java.util.List; import java.util.List;
/** /**
* sa-token 基于路由的拦截式鉴权增加了注解的处理 * sa-token 基于路由的拦截式鉴权增加了注解的处理使用优先级要高些
* *
* @author kong
* @since 1.9
* @author noear * @author noear
* @since 1.10 * @since 1.10
*/ */
@ -172,8 +170,6 @@ public class SaTokenPathInterceptor implements Handler {
Action action = ctx.action(); Action action = ctx.action();
if(isAnnotation && action != null){ if(isAnnotation && action != null){
ctx.attrSet("_SaTokenPathInterceptor", "1");
// 获取此请求对应的 Method 处理函数 // 获取此请求对应的 Method 处理函数
Method method = action.method().getMethod(); Method method = action.method().getMethod();
@ -184,8 +180,6 @@ public class SaTokenPathInterceptor implements Handler {
// 注解校验 // 注解校验
SaStrategy.me.checkMethodAnnotation.accept(method); SaStrategy.me.checkMethodAnnotation.accept(method);
}else{
ctx.attrSet("_SaTokenPathInterceptor", "0");
} }
//路径规则处理 //路径规则处理