mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-06-28 13:34:18 +08:00
fix: 修复 sa-token-dubbo 与 sa-token-dubbo3 每次调用都强制需要上下文的问题。
This commit is contained in:
parent
0cd3d66712
commit
e80369eb4b
@ -16,6 +16,7 @@
|
|||||||
package cn.dev33.satoken.context.dubbo.filter;
|
package cn.dev33.satoken.context.dubbo.filter;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
|
import cn.dev33.satoken.context.SaHolder;
|
||||||
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||||
import cn.dev33.satoken.same.SaSameUtil;
|
import cn.dev33.satoken.same.SaSameUtil;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
@ -36,23 +37,28 @@ public class SaTokenDubboConsumerFilter implements Filter {
|
|||||||
@Override
|
@Override
|
||||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||||
|
|
||||||
// 1、追加 Same-Token 参数
|
// 追加 Same-Token 参数
|
||||||
if(SaManager.getConfig().getCheckSameToken()) {
|
if(SaManager.getConfig().getCheckSameToken()) {
|
||||||
RpcContext.getContext().setAttachment(SaSameUtil.SAME_TOKEN, SaSameUtil.getToken());
|
RpcContext.getContext().setAttachment(SaSameUtil.SAME_TOKEN, SaSameUtil.getToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2、调用前,向下传递会话Token
|
// 无上下文时只做简单调用,不传递会话 token
|
||||||
|
if( ! SaHolder.getContext().isValid()) {
|
||||||
|
return invoker.invoke(invocation);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 1、调用前,向下传递会话Token
|
||||||
if(SaManager.getSaTokenContext() != SaTokenContextDefaultImpl.defaultContext) {
|
if(SaManager.getSaTokenContext() != SaTokenContextDefaultImpl.defaultContext) {
|
||||||
RpcContext.getContext().setAttachment(SaTokenConsts.JUST_CREATED, StpUtil.getTokenValueNotCut());
|
RpcContext.getContext().setAttachment(SaTokenConsts.JUST_CREATED, StpUtil.getTokenValueNotCut());
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3、开始调用
|
// 2、开始调用
|
||||||
Result invoke = invoker.invoke(invocation);
|
Result invoke = invoker.invoke(invocation);
|
||||||
|
|
||||||
// 4、调用后,解析回传的Token值
|
// 3、调用后,解析回传的Token值
|
||||||
StpUtil.setTokenValue(invoke.getAttachment(SaTokenConsts.JUST_CREATED_NOT_PREFIX));
|
StpUtil.setTokenValue(invoke.getAttachment(SaTokenConsts.JUST_CREATED_NOT_PREFIX));
|
||||||
|
|
||||||
// 5、返回结果
|
// note
|
||||||
return invoke;
|
return invoke;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -36,7 +36,7 @@ public class SaTokenDubboContextFilter implements Filter {
|
|||||||
public Result invoke(Invoker<?> invoker, Invocation invocation) {
|
public Result invoke(Invoker<?> invoker, Invocation invocation) {
|
||||||
if(SaHolder.getContext().isValid()) {
|
if(SaHolder.getContext().isValid()) {
|
||||||
return invoker.invoke(invocation);
|
return invoker.invoke(invocation);
|
||||||
} else {
|
}
|
||||||
try {
|
try {
|
||||||
SaTokenContextDubboUtil.setContext(RpcContext.getContext());
|
SaTokenContextDubboUtil.setContext(RpcContext.getContext());
|
||||||
return invoker.invoke(invocation);
|
return invoker.invoke(invocation);
|
||||||
@ -44,6 +44,5 @@ public class SaTokenDubboContextFilter implements Filter {
|
|||||||
SaManager.getSaTokenContext().clearContext();
|
SaManager.getSaTokenContext().clearContext();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -16,6 +16,7 @@
|
|||||||
package cn.dev33.satoken.context.dubbo3.filter;
|
package cn.dev33.satoken.context.dubbo3.filter;
|
||||||
|
|
||||||
import cn.dev33.satoken.SaManager;
|
import cn.dev33.satoken.SaManager;
|
||||||
|
import cn.dev33.satoken.context.SaHolder;
|
||||||
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||||
import cn.dev33.satoken.same.SaSameUtil;
|
import cn.dev33.satoken.same.SaSameUtil;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
@ -41,6 +42,11 @@ public class SaTokenDubbo3ConsumerFilter implements Filter {
|
|||||||
RpcContext.getServiceContext().setAttachment(SaSameUtil.SAME_TOKEN,SaSameUtil.getToken());
|
RpcContext.getServiceContext().setAttachment(SaSameUtil.SAME_TOKEN,SaSameUtil.getToken());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 无上下文时只做简单调用,不传递会话 token
|
||||||
|
if( ! SaHolder.getContext().isValid()) {
|
||||||
|
return invoker.invoke(invocation);
|
||||||
|
}
|
||||||
|
|
||||||
// 1. 调用前,向下传递会话Token
|
// 1. 调用前,向下传递会话Token
|
||||||
if(SaManager.getSaTokenContext() != SaTokenContextDefaultImpl.defaultContext) {
|
if(SaManager.getSaTokenContext() != SaTokenContextDefaultImpl.defaultContext) {
|
||||||
RpcContext.getServiceContext().setAttachment(SaTokenConsts.JUST_CREATED, StpUtil.getTokenValueNotCut());
|
RpcContext.getServiceContext().setAttachment(SaTokenConsts.JUST_CREATED, StpUtil.getTokenValueNotCut());
|
||||||
|
@ -36,14 +36,14 @@ public class SaTokenDubbo3ContextFilter implements Filter {
|
|||||||
public Result invoke(Invoker<?> invoker, Invocation invocation) {
|
public Result invoke(Invoker<?> invoker, Invocation invocation) {
|
||||||
if(SaHolder.getContext().isValid()) {
|
if(SaHolder.getContext().isValid()) {
|
||||||
return invoker.invoke(invocation);
|
return invoker.invoke(invocation);
|
||||||
} else {
|
}
|
||||||
try {
|
try {
|
||||||
SaTokenContextDubbo3Util.setContext(RpcContext.getServiceContext());
|
SaTokenContextDubbo3Util.setContext(RpcContext.getServiceContext());
|
||||||
return invoker.invoke(invocation);
|
return invoker.invoke(invocation);
|
||||||
} finally {
|
} finally {
|
||||||
SaManager.getSaTokenContext().clearContext();
|
SaManager.getSaTokenContext().clearContext();
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user