mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-06-28 13:34:18 +08:00
sa-token-solon-plugin:升级 solon 为 1.9.2; 优化 SaTokenPathInterceptor 同时支持注解、路径、规则处理
This commit is contained in:
parent
81e0403272
commit
05a9d40151
@ -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.0</version>
|
<version>1.10.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- Sa-Token 权限认证, 在线文档:http://sa-token.dev33.cn/ -->
|
<!-- Sa-Token 权限认证, 在线文档:http://sa-token.dev33.cn/ -->
|
||||||
|
@ -20,7 +20,7 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.noear</groupId>
|
<groupId>org.noear</groupId>
|
||||||
<artifactId>solon</artifactId>
|
<artifactId>solon</artifactId>
|
||||||
<version>1.10.0</version>
|
<version>1.10.4</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<dependency>
|
<dependency>
|
||||||
|
@ -71,10 +71,11 @@ public class XPluginImp implements Plugin {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// 注入侦听器 Bean
|
// 注入侦听器 Bean
|
||||||
EventBus.subscribe(SaTokenListener.class, bw->{
|
context.subBean(SaTokenListener.class, sl->{
|
||||||
SaTokenEventCenter.registerListener(bw);
|
SaTokenEventCenter.registerListener(sl);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
// 注入权限认证 Bean
|
// 注入权限认证 Bean
|
||||||
context.getWrapAsyn(StpInterface.class, bw->{
|
context.getWrapAsyn(StpInterface.class, bw->{
|
||||||
SaManager.setStpInterface(bw.raw());
|
SaManager.setStpInterface(bw.raw());
|
||||||
|
@ -1,24 +1,40 @@
|
|||||||
package cn.dev33.satoken.solon.integration;
|
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.Interceptor;
|
||||||
import org.noear.solon.core.aspect.Invocation;
|
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
|
* @author noear
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
|
* @deprecated 1.10,改用 SaTokenPathInterceptor
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public class SaTokenAnnotationInterceptor implements Interceptor {
|
public class SaTokenAnnotationInterceptor implements Interceptor {
|
||||||
|
|
||||||
public static final SaTokenAnnotationInterceptor INSTANCE = new SaTokenAnnotationInterceptor();
|
public static final SaTokenAnnotationInterceptor INSTANCE = new SaTokenAnnotationInterceptor();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object doIntercept(Invocation inv) throws Throwable {
|
public Object doIntercept(Invocation inv) throws Throwable {
|
||||||
// 注解鉴权
|
// 如果此 Method 或其所属 Class 标注了 @SaIgnore,则忽略掉鉴权
|
||||||
SaStrategy.me.checkMethodAnnotation.accept(inv.method().getMethod());
|
Context ctx = Context.current();
|
||||||
|
|
||||||
// 执行原有逻辑
|
if (ctx != null && "1".equals(ctx.attr("_SaTokenPathInterceptor"))) {
|
||||||
return inv.invoke();
|
// 执行原有逻辑
|
||||||
|
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();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
@ -16,9 +16,14 @@ import java.util.Arrays;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* sa-token 基于路由的过滤式鉴权( +SaTokenAnnotationInterceptor )
|
||||||
|
*
|
||||||
* @author noear
|
* @author noear
|
||||||
* @since 1.9
|
* @since 1.9
|
||||||
|
* @deprecated 1.10,改用 SaTokenPathInterceptor
|
||||||
|
* @see SaTokenPathInterceptor
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public class SaTokenPathFilter implements Filter {
|
public class SaTokenPathFilter implements Filter {
|
||||||
|
|
||||||
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
|
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
|
||||||
|
@ -1,23 +1,35 @@
|
|||||||
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 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.Context;
|
||||||
import org.noear.solon.core.handle.Handler;
|
import org.noear.solon.core.handle.Handler;
|
||||||
|
|
||||||
|
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基于路由的拦截式鉴权
|
* sa-token 基于路由的拦截式鉴权(增加了注解的处理)
|
||||||
|
*
|
||||||
* @author kong
|
* @author kong
|
||||||
|
* @since 1.9
|
||||||
|
* @author noear
|
||||||
|
* @since 1.10
|
||||||
*/
|
*/
|
||||||
public class SaTokenPathInterceptor implements Handler {
|
public class SaTokenPathInterceptor implements Handler {
|
||||||
|
/**
|
||||||
|
* 是否打开注解鉴权
|
||||||
|
*/
|
||||||
|
public boolean isAnnotation = true;
|
||||||
|
|
||||||
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
|
// ------------------------ 设置此过滤器 拦截 & 放行 的路由
|
||||||
|
|
||||||
@ -156,10 +168,30 @@ public class SaTokenPathInterceptor implements Handler {
|
|||||||
@Override
|
@Override
|
||||||
public void handle(Context ctx) throws Throwable {
|
public void handle(Context ctx) throws Throwable {
|
||||||
try {
|
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 -> {
|
SaRouter.match(includeList).notMatch(excludeList).check(r -> {
|
||||||
beforeAuth.run(null);
|
beforeAuth.run(action);
|
||||||
auth.run(null);
|
auth.run(action);
|
||||||
});
|
});
|
||||||
|
|
||||||
} catch (StopMatchException e) {
|
} catch (StopMatchException e) {
|
||||||
|
@ -1,12 +1,11 @@
|
|||||||
package cn.dev33.satoken.solon.model;
|
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.SaTokenContext;
|
||||||
import cn.dev33.satoken.context.model.SaRequest;
|
import cn.dev33.satoken.context.model.SaRequest;
|
||||||
import cn.dev33.satoken.context.model.SaResponse;
|
import cn.dev33.satoken.context.model.SaResponse;
|
||||||
import cn.dev33.satoken.context.model.SaStorage;
|
import cn.dev33.satoken.context.model.SaStorage;
|
||||||
|
import org.noear.solon.core.handle.Context;
|
||||||
|
import org.noear.solon.core.util.PathAnalyzer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author noear
|
* @author noear
|
||||||
@ -45,12 +44,12 @@ public class SaContextForSolon implements SaTokenContext {
|
|||||||
return PathAnalyzer.get(pattern).matches(path);
|
return PathAnalyzer.get(pattern).matches(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 此上下文是否有效
|
* 此上下文是否有效
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public boolean isValid() {
|
public boolean isValid() {
|
||||||
return Context.current() != null;
|
return Context.current() != null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,20 +1,19 @@
|
|||||||
package cn.dev33.satoken.solon.model;
|
package cn.dev33.satoken.solon.model;
|
||||||
|
|
||||||
import org.noear.solon.core.handle.Context;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
import cn.dev33.satoken.context.model.SaRequest;
|
import cn.dev33.satoken.context.model.SaRequest;
|
||||||
import cn.dev33.satoken.util.SaFoxUtil;
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
|
import org.noear.solon.core.handle.Context;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author noear
|
* @author noear
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
public class SaRequestForSolon implements SaRequest {
|
public class SaRequestForSolon implements SaRequest {
|
||||||
|
|
||||||
protected Context ctx;
|
protected Context ctx;
|
||||||
|
|
||||||
public SaRequestForSolon(){
|
public SaRequestForSolon(){
|
||||||
ctx = Context.current();
|
ctx = Context.current();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -44,23 +43,23 @@ public class SaRequestForSolon implements SaRequest {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getUrl() {
|
public String getUrl() {
|
||||||
String currDomain = SaManager.getConfig().getCurrDomain();
|
String currDomain = SaManager.getConfig().getCurrDomain();
|
||||||
if(SaFoxUtil.isEmpty(currDomain) == false) {
|
if(SaFoxUtil.isEmpty(currDomain) == false) {
|
||||||
return currDomain + this.getRequestPath();
|
return currDomain + this.getRequestPath();
|
||||||
}
|
}
|
||||||
return ctx.url();
|
return ctx.url();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getMethod() {
|
public String getMethod() {
|
||||||
return ctx.method();
|
return ctx.method();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object forward(String path) {
|
public Object forward(String path) {
|
||||||
ctx.forward(path);
|
ctx.forward(path);
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -1,49 +1,48 @@
|
|||||||
package cn.dev33.satoken.solon.model;
|
package cn.dev33.satoken.solon.model;
|
||||||
|
|
||||||
import org.noear.solon.core.handle.Context;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.context.model.SaResponse;
|
import cn.dev33.satoken.context.model.SaResponse;
|
||||||
|
import org.noear.solon.core.handle.Context;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author noear
|
* @author noear
|
||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
public class SaResponseForSolon implements SaResponse {
|
public class SaResponseForSolon implements SaResponse {
|
||||||
|
|
||||||
protected Context ctx;
|
protected Context ctx;
|
||||||
|
|
||||||
public SaResponseForSolon() {
|
public SaResponseForSolon() {
|
||||||
ctx = Context.current();
|
ctx = Context.current();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object getSource() {
|
public Object getSource() {
|
||||||
return ctx;
|
return ctx;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SaResponse setStatus(int sc) {
|
public SaResponse setStatus(int sc) {
|
||||||
ctx.status(sc);
|
ctx.status(sc);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public SaResponse setHeader(String name, String value) {
|
public SaResponse setHeader(String name, String value) {
|
||||||
ctx.headerSet(name, value);
|
ctx.headerSet(name, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 在响应头里添加一个值
|
* 在响应头里添加一个值
|
||||||
* @param name 名字
|
* @param name 名字
|
||||||
* @param value 值
|
* @param value 值
|
||||||
* @return 对象自身
|
* @return 对象自身
|
||||||
*/
|
*/
|
||||||
public SaResponse addHeader(String name, String value) {
|
public SaResponse addHeader(String name, String value) {
|
||||||
ctx.headerAdd(name, value);
|
ctx.headerAdd(name, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object redirect(String url) {
|
public Object redirect(String url) {
|
||||||
ctx.redirect(url);
|
ctx.redirect(url);
|
||||||
|
@ -8,8 +8,8 @@ import org.noear.solon.core.handle.Context;
|
|||||||
* @since 1.4
|
* @since 1.4
|
||||||
*/
|
*/
|
||||||
public class SaStorageForSolon implements SaStorage {
|
public class SaStorageForSolon implements SaStorage {
|
||||||
|
|
||||||
protected Context ctx;
|
protected Context ctx;
|
||||||
|
|
||||||
public SaStorageForSolon() {
|
public SaStorageForSolon() {
|
||||||
ctx = Context.current();
|
ctx = Context.current();
|
||||||
@ -23,7 +23,7 @@ public class SaStorageForSolon implements SaStorage {
|
|||||||
@Override
|
@Override
|
||||||
public SaStorageForSolon set(String key, Object value) {
|
public SaStorageForSolon set(String key, Object value) {
|
||||||
ctx.attrSet(key, value);
|
ctx.attrSet(key, value);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -34,6 +34,6 @@ public class SaStorageForSolon implements SaStorage {
|
|||||||
@Override
|
@Override
|
||||||
public SaStorageForSolon delete(String key) {
|
public SaStorageForSolon delete(String key) {
|
||||||
ctx.attrMap().remove(key);
|
ctx.attrMap().remove(key);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user