适配dubbo3

This commit is contained in:
qiudaozhang 2023-02-03 09:44:04 +08:00
parent 532004fa09
commit ae7b35b8b3
14 changed files with 455 additions and 0 deletions

View File

@ -0,0 +1,13 @@
target/
.project
.classpath
.settings
/.idea/
node_modules/
bin/
.settings/
unpackage/
/.apt_generated/
/.apt_generated_tests/

View File

@ -0,0 +1,13 @@
target/
.project
.classpath
.settings
/.idea/
node_modules/
bin/
.settings/
unpackage/
/.apt_generated/
/.apt_generated_tests/

View File

@ -33,6 +33,7 @@
<module>sa-token-temp-jwt</module>
<module>sa-token-jwt</module>
<module>sa-token-context-dubbo</module>
<module>sa-token-context-dubbo3</module>
<module>sa-token-context-grpc</module>
</modules>

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-plugin</artifactId>
<version>${revision}</version>
<relativePath>../pom.xml</relativePath>
</parent>
<name>sa-token-context-dubbo3</name>
<artifactId>sa-token-context-dubbo3</artifactId>
<description>sa-token-context-dubbo3</description>
<properties>
<dubbo3.version>3.1.5</dubbo3.version>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<!-- sa-token-core -->
<dependency>
<groupId>cn.dev33</groupId>
<artifactId>sa-token-core</artifactId>
</dependency>
<!-- dubbo3 -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo</artifactId>
<version>${dubbo3.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,20 @@
package cn.dev33.satoken.context.dubbo3;
import cn.dev33.satoken.context.second.SaTokenSecondContext;
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
/**
* Sa-Token 二级Context - 创建器 [Dubbo3版]
*
* @author kong
*
*/
public class SaTokenSecondContextCreatorForDubbo3 implements SaTokenSecondContextCreator {
@Override
public SaTokenSecondContext create() {
return new SaTokenSecondContextForDubbo3();
}
}

View File

@ -0,0 +1,46 @@
package cn.dev33.satoken.context.dubbo3;
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 cn.dev33.satoken.exception.ApiDisabledException;
import org.apache.dubbo.rpc.RpcContext;
/**
* Sa-Token 上下文 [Dubbo3版本]
*
* @author kong
*
*/
public class SaTokenSecondContextForDubbo3 implements SaTokenSecondContext {
@Override
public SaRequest getRequest() {
return new SaRequestForDubbo3(RpcContext.getServiceContext());
}
@Override
public SaResponse getResponse() {
return new SaResponseForDubbo3(RpcContext.getServiceContext());
}
@Override
public SaStorage getStorage() {
return new SaStorageForDubbo3(RpcContext.getServiceContext());
}
@Override
public boolean matchPath(String pattern, String path) {
throw new ApiDisabledException();
}
@Override
public boolean isValid() {
return RpcContext.getServiceContext() != null;
}
}

View File

@ -0,0 +1,45 @@
package cn.dev33.satoken.context.dubbo3.filter;
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 整合 Dubbo3 Consumer端过滤器
*
* @author kong
*
*/
@Activate(group = {CommonConstants.CONSUMER}, order = -30000)
public class SaTokenDubbo3ConsumerFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// 追加 Same-Token 参数
if(SaManager.getConfig().getCheckSameToken()) {
RpcContext.getServiceContext().setAttachment(SaSameUtil.SAME_TOKEN,SaSameUtil.getToken());
}
// 1. 调用前向下传递会话Token
if(SaManager.getSaTokenContextOrSecond() != SaTokenContextDefaultImpl.defaultContext) {
RpcContext.getServiceContext().setAttachment(SaTokenConsts.JUST_CREATED, StpUtil.getTokenValueNotCut());
}
// 2. 开始调用
Result invoke = invoker.invoke(invocation);
// 3. 调用后解析回传的Token值
StpUtil.setTokenValue(invoke.getAttachment(SaTokenConsts.JUST_CREATED_NOT_PREFIX));
// note
return invoke;
}
}

View File

@ -0,0 +1,36 @@
package cn.dev33.satoken.context.dubbo3.filter;
import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.same.SaSameUtil;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.extension.Activate;
import org.apache.dubbo.rpc.*;
/**
*
* Sa-Token 整合 Dubbo3 Provider端过滤器
*
* @author kong
*
*/
@Activate(group = {CommonConstants.PROVIDER}, order = -30000)
public class SaTokenDubbo3ProviderFilter implements Filter {
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
// RPC 调用鉴权
if(SaManager.getConfig().getCheckSameToken()) {
String idToken = invocation.getAttachment(SaSameUtil.SAME_TOKEN);
// dubbo部分协议会将参数变为小写详细参考https://gitee.com/dromara/sa-token/issues/I4WXQG
if(idToken == null) {
idToken = invocation.getAttachment(SaSameUtil.SAME_TOKEN.toLowerCase());
}
SaSameUtil.checkToken(idToken);
}
// 开始调用
return invoker.invoke(invocation);
}
}

View File

@ -0,0 +1,98 @@
package cn.dev33.satoken.context.dubbo3.model;
import cn.dev33.satoken.context.model.SaRequest;
import org.apache.dubbo.rpc.RpcContext;
/**
* Request for Dubbo3
*
* @author kong
*
*/
public class SaRequestForDubbo3 implements SaRequest {
/**
* 底层对象
*/
protected RpcContext rpcContext;
/**
* 实例化
* @param rpcContext rpcContext对象
*/
public SaRequestForDubbo3(RpcContext rpcContext) {
this.rpcContext = rpcContext;
}
/**
* 获取底层源对象
*/
@Override
public Object getSource() {
return rpcContext;
}
/**
* [请求体] 里获取一个值
*/
@Override
public String getParam(String name) {
// 不传播 url 参数
return null;
}
/**
* [请求头] 里获取一个值
*/
@Override
public String getHeader(String name) {
// 不传播 header 参数
return null;
}
/**
* [Cookie作用域] 里获取一个值
*/
@Override
public String getCookieValue(String name) {
// 不传播 cookie 参数
return null;
}
/**
* 返回当前请求path (不包括上下文名称)
*/
@Override
public String getRequestPath() {
// 不传播 requestPath
return null;
}
/**
* 返回当前请求的urlhttp://xxx.com/test
* @return see note
*/
public String getUrl() {
// 不传播 url
return null;
}
/**
* 返回当前请求的类型
*/
@Override
public String getMethod() {
// 不传播 method
return null;
}
/**
* 转发请求
*/
@Override
public Object forward(String path) {
// 不传播 forward 动作
return null;
}
}

View File

@ -0,0 +1,74 @@
package cn.dev33.satoken.context.dubbo3.model;
import cn.dev33.satoken.context.model.SaResponse;
import org.apache.dubbo.rpc.RpcContext;
/**
* Response for Servlet
* @author kong
*
*/
public class SaResponseForDubbo3 implements SaResponse {
/**
* 底层Request对象
*/
protected RpcContext rpcContext;
/**
* 实例化
* @param rpcContext rpcContext对象
*/
public SaResponseForDubbo3(RpcContext rpcContext) {
this.rpcContext = rpcContext;
}
/**
* 获取底层源对象
*/
@Override
public Object getSource() {
return rpcContext;
}
/**
* 设置响应状态码
*/
@Override
public SaResponse setStatus(int sc) {
// 不回传 status 状态
return this;
}
/**
* 在响应头里写入一个值
*/
@Override
public SaResponse setHeader(String name, String value) {
// 不回传 header响应头
return this;
}
/**
* 在响应头里添加一个值
* @param name 名字
* @param value
* @return 对象自身
*/
public SaResponse addHeader(String name, String value) {
// 不回传 header响应头
return this;
}
/**
* 重定向
*/
@Override
public Object redirect(String url) {
// 不回传 重定向 动作
return null;
}
}

View File

@ -0,0 +1,65 @@
package cn.dev33.satoken.context.dubbo3.model;
import cn.dev33.satoken.context.model.SaStorage;
import cn.dev33.satoken.util.SaTokenConsts;
import org.apache.dubbo.rpc.RpcContext;
/**
* Storage for Servlet
* @author kong
*
*/
public class SaStorageForDubbo3 implements SaStorage {
/**
* 底层对象
*/
protected RpcContext rpcContext;
/**
* 实例化
* @param rpcContext rpcContext对象
*/
public SaStorageForDubbo3(RpcContext rpcContext) {
this.rpcContext = rpcContext;
}
/**
* 获取底层源对象
*/
@Override
public Object getSource() {
return rpcContext;
}
/**
* [Request作用域] 里写入一个值
*/
@Override
public SaStorageForDubbo3 set(String key, Object value) {
rpcContext.setObjectAttachment(key, value);
// 如果是token写入则回传到Consumer端
if(key.equals(SaTokenConsts.JUST_CREATED_NOT_PREFIX)) {
RpcContext.getServerContext().setAttachment(key, value);
}
return this;
}
/**
* [Request作用域] 里获取一个值
*/
@Override
public Object get(String key) {
return rpcContext.getObjectAttachment(key);
}
/**
* [Request作用域] 里删除一个值
*/
@Override
public SaStorageForDubbo3 delete(String key) {
rpcContext.removeAttachment(key);
return this;
}
}

View File

@ -0,0 +1,2 @@
saTokenDubbo3ConsumerFilter=cn.dev33.satoken.context.dubbo3.filter.SaTokenDubbo3ConsumerFilter
saTokenDubbo3ProviderFilter=cn.dev33.satoken.context.dubbo3.filter.SaTokenDubbo3ProviderFilter

View File

@ -0,0 +1 @@
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.context.dubbo3.SaTokenSecondContextCreatorForDubbo3

View File

@ -0,0 +1 @@
cn.dev33.satoken.context.dubbo3.SaTokenSecondContextCreatorForDubbo3