1
0
mirror of synced 2026-05-22 14:43:15 +00:00

refactor: 重构所有 rpc 组件的 SaTokenContext 上下文读写策略 & 删除二级上下文模块

This commit is contained in:
click33
2025-04-07 05:41:30 +08:00
parent c6a081ebf6
commit 3acc7bd7af
50 changed files with 526 additions and 585 deletions
@@ -1,34 +0,0 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.dubbo;
import cn.dev33.satoken.context.second.SaTokenSecondContext;
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
/**
* Sa-Token 二级上下文 - 创建器 [ Dubbo版 ]
*
* @author click33
* @since 1.34.0
*/
public class SaTokenSecondContextCreatorForDubbo implements SaTokenSecondContextCreator {
@Override
public SaTokenSecondContext create() {
return new SaTokenSecondContextForDubbo();
}
}
@@ -15,20 +15,14 @@
*/
package cn.dev33.satoken.context.dubbo.filter;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcContext;
import org.apache.dubbo.rpc.RpcException;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
/**
* Sa-Token 整合 Dubbo Consumer 端(调用端)过滤器
@@ -36,7 +30,7 @@ import cn.dev33.satoken.util.SaTokenConsts;
* @author click33
* @since 1.34.0
*/
@Activate(group = {CommonConstants.CONSUMER}, order = -30000)
@Activate(group = {CommonConstants.CONSUMER}, order = SaTokenConsts.RPC_PERMISSION_FILTER_ORDER)
public class SaTokenDubboConsumerFilter implements Filter {
@Override
@@ -0,0 +1,44 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.dubbo.filter;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.dubbo.util.SaDubboContextUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
/**
* Sa-Token 整合 Dubbo 上下文初始化过滤器
*
* @author click33
* @since 1.42.0
*/
@Activate(group = {CommonConstants.PROVIDER}, order = SaTokenConsts.RPC_CONTEXT_FILTER_ORDER)
public class SaTokenDubboContextFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) {
try {
SaDubboContextUtil.setContext(RpcContext.getContext());
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
}
}
}
@@ -15,16 +15,15 @@
*/
package cn.dev33.satoken.context.dubbo.filter;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
import org.apache.dubbo.rpc.RpcException;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.same.SaSameUtil;
/**
* Sa-Token 整合 Dubbo Provider端(被调用端)过滤器
@@ -32,12 +31,11 @@ import cn.dev33.satoken.same.SaSameUtil;
* @author click33
* @since 1.34.0
*/
@Activate(group = {CommonConstants.PROVIDER}, order = -30000)
@Activate(group = {CommonConstants.PROVIDER}, order = SaTokenConsts.RPC_PERMISSION_FILTER_ORDER)
public class SaTokenDubboProviderFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
public Result invoke(Invoker<?> invoker, Invocation invocation) {
// RPC 调用鉴权
if(SaManager.getConfig().getCheckSameToken()) {
String idToken = invocation.getAttachment(SaSameUtil.SAME_TOKEN);
@@ -13,43 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.dubbo;
package cn.dev33.satoken.context.dubbo.util;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.dubbo.model.SaRequestForDubbo;
import cn.dev33.satoken.context.dubbo.model.SaResponseForDubbo;
import cn.dev33.satoken.context.dubbo.model.SaStorageForDubbo;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.context.second.SaTokenSecondContext;
import org.apache.dubbo.rpc.RpcContext;
/**
* Sa-Token 二级上下文 [ Dubbo版本 ]
*
* SaTokenContext 上下文读写工具类
*
* @author click33
* @since 1.34.0
* @since 1.42.0
*/
public class SaTokenSecondContextForDubbo implements SaTokenSecondContext {
public class SaDubboContextUtil {
@Override
public SaRequest getRequest() {
return new SaRequestForDubbo(RpcContext.getContext());
/**
* 写入当前上下文
* @param rpcContext /
*/
public static void setContext(RpcContext rpcContext) {
SaRequest saRequest = new SaRequestForDubbo(RpcContext.getContext());
SaResponse saResponse = new SaResponseForDubbo(RpcContext.getContext());
SaStorage saStorage = new SaStorageForDubbo(RpcContext.getContext());
SaManager.getSaTokenContext().setContext(saRequest, saResponse, saStorage);
}
@Override
public SaResponse getResponse() {
return new SaResponseForDubbo(RpcContext.getContext());
/**
* 清除当前上下文
*/
public static void clearContext() {
SaManager.getSaTokenContext().clearContext();
}
@Override
public SaStorage getStorage() {
return new SaStorageForDubbo(RpcContext.getContext());
}
@Override
public boolean isValid() {
return RpcContext.getContext() != null;
}
}
@@ -1,34 +0,0 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.plugin;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.dubbo.SaTokenSecondContextForDubbo;
/**
* SaToken 插件安装:二级上下文 (dubbo 版)
*
* @author click33
* @since 1.41.0
*/
public class SaTokenPluginForDubbo implements SaTokenPlugin {
@Override
public void install() {
SaManager.setSaTokenSecondContext(new SaTokenSecondContextForDubbo());
}
}
@@ -1,2 +1,3 @@
saTokenDubboConsumerFilter=cn.dev33.satoken.context.dubbo.filter.SaTokenDubboConsumerFilter
saTokenDubboProviderFilter=cn.dev33.satoken.context.dubbo.filter.SaTokenDubboProviderFilter
saTokenDubboProviderFilter=cn.dev33.satoken.context.dubbo.filter.SaTokenDubboProviderFilter
saTokenDubboContextFilter=cn.dev33.satoken.context.dubbo.filter.SaTokenDubboContextFilter
@@ -1 +0,0 @@
cn.dev33.satoken.plugin.SaTokenPluginForDubbo
@@ -1,35 +0,0 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.dubbo3;
import cn.dev33.satoken.context.second.SaTokenSecondContext;
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
/**
* Sa-Token 二级上下文 - 创建器 [Dubbo3版]
*
* @author click33
* @since 1.34.0
*/
public class SaTokenSecondContextCreatorForDubbo3 implements SaTokenSecondContextCreator {
@Override
public SaTokenSecondContext create() {
return new SaTokenSecondContextForDubbo3();
}
}
@@ -30,11 +30,11 @@ import org.apache.dubbo.rpc.*;
* @author click33
* @since 1.34.0
*/
@Activate(group = {CommonConstants.CONSUMER}, order = -30000)
@Activate(group = {CommonConstants.CONSUMER}, order = SaTokenConsts.RPC_PERMISSION_FILTER_ORDER)
public class SaTokenDubbo3ConsumerFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
public Result invoke(Invoker<?> invoker, Invocation invocation) {
// 追加 Same-Token 参数
if(SaManager.getConfig().getCheckSameToken()) {
@@ -0,0 +1,44 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.dubbo3.filter;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.dubbo3.util.SaDubbo3ContextUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
/**
* Sa-Token 整合 Dubbo3 上下文初始化过滤器
*
* @author click33
* @since 1.42.0
*/
@Activate(group = {CommonConstants.PROVIDER}, order = SaTokenConsts.RPC_CONTEXT_FILTER_ORDER)
public class SaTokenDubbo3ContextFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) {
try {
SaDubbo3ContextUtil.setContext(RpcContext.getServiceContext());
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
}
}
}
@@ -17,9 +17,13 @@ package cn.dev33.satoken.context.dubbo3.filter;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
import org.apache.dubbo.rpc.Filter;
import org.apache.dubbo.rpc.Invocation;
import org.apache.dubbo.rpc.Invoker;
import org.apache.dubbo.rpc.Result;
/**
* Sa-Token 整合 Dubbo3 Provider端(被调用端)过滤器
@@ -27,11 +31,11 @@ import org.apache.dubbo.rpc.*;
* @author click33
* @since 1.34.0
*/
@Activate(group = {CommonConstants.PROVIDER}, order = -30000)
@Activate(group = {CommonConstants.PROVIDER}, order = SaTokenConsts.RPC_PERMISSION_FILTER_ORDER)
public class SaTokenDubbo3ProviderFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
public Result invoke(Invoker<?> invoker, Invocation invocation) {
// RPC 调用鉴权
if(SaManager.getConfig().getCheckSameToken()) {
@@ -13,43 +13,42 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.dubbo3;
package cn.dev33.satoken.context.dubbo3.util;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.dubbo3.model.SaRequestForDubbo3;
import cn.dev33.satoken.context.dubbo3.model.SaResponseForDubbo3;
import cn.dev33.satoken.context.dubbo3.model.SaStorageForDubbo3;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.context.second.SaTokenSecondContext;
import org.apache.dubbo.rpc.RpcContext;
/**
* Sa-Token 二级上下文 [ Dubbo3版本 ]
*
* SaTokenContext 上下文读写工具类
*
* @author click33
* @since 1.34.0
* @since 1.42.0
*/
public class SaTokenSecondContextForDubbo3 implements SaTokenSecondContext {
public class SaDubbo3ContextUtil {
@Override
public SaRequest getRequest() {
return new SaRequestForDubbo3(RpcContext.getServiceContext());
/**
* 写入当前上下文
* @param rpcContext /
*/
public static void setContext(RpcContext rpcContext) {
SaRequest saRequest = new SaRequestForDubbo3(RpcContext.getServiceContext());
SaResponse saResponse = new SaResponseForDubbo3(RpcContext.getServiceContext());
SaStorage saStorage = new SaStorageForDubbo3(RpcContext.getServiceContext());
SaManager.getSaTokenContext().setContext(saRequest, saResponse, saStorage);
}
@Override
public SaResponse getResponse() {
return new SaResponseForDubbo3(RpcContext.getServiceContext());
/**
* 清除当前上下文
*/
public static void clearContext() {
SaManager.getSaTokenContext().clearContext();
}
@Override
public SaStorage getStorage() {
return new SaStorageForDubbo3(RpcContext.getServiceContext());
}
@Override
public boolean isValid() {
return RpcContext.getServiceContext() != null;
}
}
@@ -1,34 +0,0 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.plugin;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.dubbo3.SaTokenSecondContextForDubbo3;
/**
* SaToken 插件安装:二级上下文 (dubbo3 版)
*
* @author click33
* @since 1.41.0
*/
public class SaTokenPluginForDubbo3 implements SaTokenPlugin {
@Override
public void install() {
SaManager.setSaTokenSecondContext(new SaTokenSecondContextForDubbo3());
}
}
@@ -1,2 +1,3 @@
saTokenDubbo3ConsumerFilter=cn.dev33.satoken.context.dubbo3.filter.SaTokenDubbo3ConsumerFilter
saTokenDubbo3ProviderFilter=cn.dev33.satoken.context.dubbo3.filter.SaTokenDubbo3ProviderFilter
saTokenDubbo3ProviderFilter=cn.dev33.satoken.context.dubbo3.filter.SaTokenDubbo3ProviderFilter
saTokenDubbo3ContextFilter=cn.dev33.satoken.context.dubbo3.filter.SaTokenDubbo3ContextFilter
@@ -1 +0,0 @@
cn.dev33.satoken.plugin.SaTokenPluginForDubbo3
@@ -1,36 +0,0 @@
/*
* Copyright 2020-2099 sa-token.cc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.grpc;
import cn.dev33.satoken.context.second.SaTokenSecondContext;
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
import org.springframework.stereotype.Component;
/**
* Sa-Token 二级Context - 创建器 [Grpc版]
*
* @author lym
* @since 1.34.0
*/
@Component
public class SaTokenSecondContextCreatorForGrpc implements SaTokenSecondContextCreator {
@Override
public SaTokenSecondContext create() {
return new SaTokenSecondContextForGrpc();
}
}
@@ -17,15 +17,11 @@ package cn.dev33.satoken.context.grpc.interceptor;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.grpc.constants.GrpcContextConstants;
import cn.dev33.satoken.context.grpc.util.SaGrpcContextUtil;
import cn.dev33.satoken.same.SaSameUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import io.grpc.ForwardingServerCall;
import io.grpc.Metadata;
import io.grpc.ServerCall;
import io.grpc.ServerCallHandler;
import io.grpc.ServerInterceptor;
import io.grpc.Status;
import io.grpc.*;
import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
/**
@@ -38,26 +34,34 @@ import net.devh.boot.grpc.server.interceptor.GrpcGlobalServerInterceptor;
public class SaTokenGrpcServerInterceptor implements ServerInterceptor {
@Override
public <ReqT, RespT> ServerCall.Listener<ReqT> interceptCall(ServerCall<ReqT, RespT> call, Metadata headers, ServerCallHandler<ReqT, RespT> next) {
// RPC 调用鉴权
if (SaManager.getConfig().getCheckSameToken()) {
String sameToken = headers.get(GrpcContextConstants.SA_SAME_TOKEN);
SaSameUtil.checkToken(sameToken);
}
String tokenFromClient = headers.get(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX);
StpUtil.setTokenValue(tokenFromClient);
try{
// 初始化上下文
SaGrpcContextUtil.setContext();
return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
/**
* 结束响应时,若本服务生成了新token,将其传回客户端
*/
@Override
public void close(Status status, Metadata responseHeaders) {
String justCreateToken = StpUtil.getTokenValue();
if (!SaFoxUtil.equals(justCreateToken, tokenFromClient) && SaFoxUtil.isNotEmpty(justCreateToken)) {
responseHeaders.put(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX, justCreateToken);
}
super.close(status, responseHeaders);
// RPC 调用鉴权
if (SaManager.getConfig().getCheckSameToken()) {
String sameToken = headers.get(GrpcContextConstants.SA_SAME_TOKEN);
SaSameUtil.checkToken(sameToken);
}
}, headers);
String tokenFromClient = headers.get(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX);
StpUtil.setTokenValue(tokenFromClient);
return next.startCall(new ForwardingServerCall.SimpleForwardingServerCall<ReqT, RespT>(call) {
/**
* 结束响应时,若本服务生成了新token,将其传回客户端
*/
@Override
public void close(Status status, Metadata responseHeaders) {
String justCreateToken = StpUtil.getTokenValue();
if (!SaFoxUtil.equals(justCreateToken, tokenFromClient) && SaFoxUtil.isNotEmpty(justCreateToken)) {
responseHeaders.put(GrpcContextConstants.SA_JUST_CREATED_NOT_PREFIX, justCreateToken);
}
super.close(status, responseHeaders);
}
}, headers);
}finally {
// 清除上下文
SaGrpcContextUtil.clearContext();
}
}
}
@@ -13,43 +13,40 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.dev33.satoken.context.grpc;
package cn.dev33.satoken.context.grpc.util;
import cn.dev33.satoken.context.grpc.context.SaTokenGrpcContext;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.grpc.model.SaRequestForGrpc;
import cn.dev33.satoken.context.grpc.model.SaResponseForGrpc;
import cn.dev33.satoken.context.grpc.model.SaStorageForGrpc;
import cn.dev33.satoken.context.model.SaRequest;
import cn.dev33.satoken.context.model.SaResponse;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.context.second.SaTokenSecondContext;
/**
* Sa-Token 上下文 [grpc版本]
* SaTokenContext 上下文读写工具类
*
* @author lym
* @since 1.34.0
* @author click33
* @since 1.42.0
*/
public class SaTokenSecondContextForGrpc implements SaTokenSecondContext {
public class SaGrpcContextUtil {
@Override
public SaRequest getRequest() {
return new SaRequestForGrpc();
}
/**
* 写入当前上下文
*/
public static void setContext() {
SaRequest saRequest = new SaRequestForGrpc();
SaResponse saResponse = new SaResponseForGrpc();
SaStorage saStorage = new SaStorageForGrpc();
SaManager.getSaTokenContext().setContext(saRequest, saResponse, saStorage);
}
@Override
public SaResponse getResponse() {
return new SaResponseForGrpc();
}
@Override
public SaStorage getStorage() {
return new SaStorageForGrpc();
}
@Override
public boolean isValid() {
return SaTokenGrpcContext.isNotNull();
}
/**
* 清除当前上下文
*/
public static void clearContext() {
SaManager.getSaTokenContext().clearContext();
}
}