sa-token-solon-plugin:升级 solon 为 1.9.2; 优化 SaTokenPathInterceptor 同时支持注解、路径、规则处理

This commit is contained in:
noear 2022-09-28 18:21:30 +08:00
parent 81e0403272
commit 05a9d40151
10 changed files with 119 additions and 68 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

@ -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();
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();
// 执行原有逻辑
return inv.invoke();
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
@ -45,12 +44,12 @@ public class SaContextForSolon implements SaTokenContext {
return PathAnalyzer.get(pattern).matches(path);
}
/**
* 此上下文是否有效
* @return /
*/
public boolean isValid() {
return Context.current() != null;
}
/**
* 此上下文是否有效
* @return /
*/
public boolean isValid() {
return Context.current() != null;
}
}

View File

@ -1,20 +1,19 @@
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
* @since 1.4
*/
public class SaRequestForSolon implements SaRequest {
protected Context ctx;
public SaRequestForSolon(){
protected Context ctx;
public SaRequestForSolon(){
ctx = Context.current();
}
@ -44,23 +43,23 @@ public class SaRequestForSolon implements SaRequest {
}
@Override
public String getUrl() {
String currDomain = SaManager.getConfig().getCurrDomain();
if(SaFoxUtil.isEmpty(currDomain) == false) {
return currDomain + this.getRequestPath();
}
return ctx.url();
}
public String getUrl() {
String currDomain = SaManager.getConfig().getCurrDomain();
if(SaFoxUtil.isEmpty(currDomain) == false) {
return currDomain + this.getRequestPath();
}
return ctx.url();
}
@Override
public String getMethod() {
return ctx.method();
}
@Override
public Object forward(String path) {
ctx.forward(path);
return null;
}
public Object forward(String path) {
ctx.forward(path);
return null;
}
}

View File

@ -1,49 +1,48 @@
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
* @since 1.4
*/
public class SaResponseForSolon implements SaResponse {
protected Context ctx;
public SaResponseForSolon() {
ctx = Context.current();
}
public SaResponseForSolon() {
ctx = Context.current();
}
@Override
public Object getSource() {
return ctx;
}
@Override
public Object getSource() {
return ctx;
}
@Override
public SaResponse setStatus(int sc) {
ctx.status(sc);
return this;
}
@Override
public SaResponse setHeader(String name, String value) {
ctx.headerSet(name, value);
return this;
}
@Override
public SaResponse setHeader(String name, String value) {
ctx.headerSet(name, value);
return this;
}
/**
* 在响应头里添加一个值
* @param name 名字
* @param value
* @return 对象自身
* @return 对象自身
*/
public SaResponse addHeader(String name, String value) {
ctx.headerAdd(name, value);
return this;
}
@Override
public Object redirect(String url) {
ctx.redirect(url);

View File

@ -8,8 +8,8 @@ import org.noear.solon.core.handle.Context;
* @since 1.4
*/
public class SaStorageForSolon implements SaStorage {
protected Context ctx;
protected Context ctx;
public SaStorageForSolon() {
ctx = Context.current();
@ -23,7 +23,7 @@ public class SaStorageForSolon implements SaStorage {
@Override
public SaStorageForSolon set(String key, Object value) {
ctx.attrSet(key, value);
return this;
return this;
}
@Override
@ -34,6 +34,6 @@ public class SaStorageForSolon implements SaStorage {
@Override
public SaStorageForSolon delete(String key) {
ctx.attrMap().remove(key);
return this;
return this;
}
}