fix: 修复 sa-token-dubbo 模块,部分场景下无法正确处理上下文的问题

This commit is contained in:
click33
2025-04-16 15:31:01 +08:00
parent 95f4d62c8e
commit cc681672df
9 changed files with 68 additions and 45 deletions

View File

@@ -10,12 +10,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.3.1.RELEASE</version>-->
<version>2.5.15</version>
<!-- <version>2.5.15</version>-->
<version>3.4.3</version>
</parent>
<!-- 指定一些属性 -->
<properties>
<java.version>1.8</java.version>
<java.version>17</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<sa-token.version>1.42.0</sa-token.version>
<dubbo.version>3.2.2</dubbo.version>
@@ -33,7 +34,7 @@
<!-- Sa-Token -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<artifactId>sa-token-spring-boot3-starter</artifactId>
<version>${sa-token.version}</version>
</dependency>

View File

@@ -2,18 +2,19 @@ server:
# 端口号
port: 8081
spring:
# redis配置
redis:
# Redis数据库索引默认为0
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码默认为空
password:
# 连接超时时间
spring:
data:
# redis配置
redis:
# Redis数据库索引默认为0
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码默认为空
password:
# 连接超时时间
dubbo:
application:

View File

@@ -10,12 +10,13 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<!--<version>2.3.1.RELEASE</version>-->
<version>2.5.15</version>
<!-- <version>2.5.15</version>-->
<version>3.4.3</version>
</parent>
<!-- 指定一些属性 -->
<properties>
<java.version>1.8</java.version>
<java.version>17</java.version>
<maven-jar-plugin.version>3.1.1</maven-jar-plugin.version>
<sa-token.version>1.42.0</sa-token.version>
<dubbo.version>3.2.2</dubbo.version>
@@ -33,7 +34,7 @@
<!-- Sa-Token -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-spring-boot-starter</artifactId>
<artifactId>sa-token-spring-boot3-starter</artifactId>
<version>${sa-token.version}</version>
</dependency>

View File

@@ -14,9 +14,9 @@ import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Dubbo3ProviderApplication {
public static void main(String[] args) {
public static void main(String[] args) throws Exception {
SpringApplication.run(Dubbo3ProviderApplication.class, args);
System.out.println("Dubbo3ProviderApplication 启动成功");
}
}

View File

@@ -9,9 +9,18 @@ import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@Autowired
private DemoService demoService;
// 如果把 @Autowired 改为 @DubboReference
// 则可能在首次调用 dubbo 服务时控制台出现以下异常(只打印异常信息,不影响调用):
// java.lang.reflect.InaccessibleObjectException: Unable to make field private byte java.lang.StackTraceElement.format accessible:
// module java.base does not "opens java.lang" to unnamed module @3a52dba3
//
// 在启动参数上加上如下即可解决:
// --add-opens java.base/java.math=ALL-UNNAMED
@Autowired
public DemoService demoService;
// test
@RequestMapping("test")

View File

@@ -2,19 +2,20 @@ server:
# 端口号
port: 8080
spring:
# redis配置
redis:
# Redis数据库索引默认为0
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码默认为空
password:
# 连接超时时间
timeout: 10s
spring:
data:
# redis配置
redis:
# Redis数据库索引默认为0
database: 0
# Redis服务器地址
host: 127.0.0.1
# Redis服务器连接端口
port: 6379
# Redis服务器连接密码默认为空
password:
# 连接超时时间
timeout: 10s
# Dubbo
dubbo:

View File

@@ -16,5 +16,5 @@ public class SaTokenSpringBoot3Application {
SpringApplication.run(SaTokenSpringBoot3Application.class, args);
System.out.println("\n启动成功Sa-Token配置如下" + SaManager.getConfig());
}
}

View File

@@ -16,6 +16,7 @@
package cn.dev33.satoken.context.dubbo.filter;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.dubbo.util.SaTokenContextDubboUtil;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants;
@@ -33,11 +34,15 @@ public class SaTokenDubboContextFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) {
try {
SaTokenContextDubboUtil.setContext(RpcContext.getContext());
if(SaHolder.getContext().isValid()) {
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
} else {
try {
SaTokenContextDubboUtil.setContext(RpcContext.getContext());
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
}
}
}

View File

@@ -16,6 +16,7 @@
package cn.dev33.satoken.context.dubbo3.filter;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.context.SaHolder;
import cn.dev33.satoken.context.dubbo3.util.SaTokenContextDubbo3Util;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.common.constants.CommonConstants;
@@ -33,11 +34,15 @@ public class SaTokenDubbo3ContextFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) {
try {
SaTokenContextDubbo3Util.setContext(RpcContext.getServiceContext());
if(SaHolder.getContext().isValid()) {
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
} else {
try {
SaTokenContextDubbo3Util.setContext(RpcContext.getServiceContext());
return invoker.invoke(invocation);
} finally {
SaManager.getSaTokenContext().clearContext();
}
}
}