mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-10-21 19:17:25 +08:00
简化 redis 和 rpc 相关集成包的包名
This commit is contained in:
12
sa-token-plugin/sa-token-dubbo/.gitignore
vendored
Normal file
12
sa-token-plugin/sa-token-dubbo/.gitignore
vendored
Normal file
@@ -0,0 +1,12 @@
|
||||
target/
|
||||
|
||||
node_modules/
|
||||
bin/
|
||||
.settings/
|
||||
unpackage/
|
||||
.classpath
|
||||
.project
|
||||
|
||||
.factorypath
|
||||
|
||||
.idea/
|
40
sa-token-plugin/sa-token-dubbo/pom.xml
Normal file
40
sa-token-plugin/sa-token-dubbo/pom.xml
Normal 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/maven-v4_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>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<name>sa-token-dubbo</name>
|
||||
<artifactId>sa-token-dubbo</artifactId>
|
||||
<description>sa-token-dubbo</description>
|
||||
|
||||
<dependencies>
|
||||
<!-- sa-token-core -->
|
||||
<dependency>
|
||||
<groupId>cn.dev33</groupId>
|
||||
<artifactId>sa-token-core</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- dubbo -->
|
||||
<dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- <dependency>
|
||||
<groupId>org.apache.dubbo</groupId>
|
||||
<artifactId>dubbo-spring-boot-starter</artifactId>
|
||||
<version>2.7.11</version>
|
||||
</dependency> -->
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@@ -0,0 +1,19 @@
|
||||
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();
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,47 @@
|
||||
package cn.dev33.satoken.context.dubbo;
|
||||
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
|
||||
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 cn.dev33.satoken.exception.ApiDisabledException;
|
||||
|
||||
/**
|
||||
* Sa-Token 二级上下文 [ Dubbo版本 ]
|
||||
*
|
||||
* @author click33
|
||||
* @since <= 1.34.0
|
||||
*/
|
||||
public class SaTokenSecondContextForDubbo implements SaTokenSecondContext {
|
||||
|
||||
@Override
|
||||
public SaRequest getRequest() {
|
||||
return new SaRequestForDubbo(RpcContext.getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaResponse getResponse() {
|
||||
return new SaResponseForDubbo(RpcContext.getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaStorage getStorage() {
|
||||
return new SaStorageForDubbo(RpcContext.getContext());
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean matchPath(String pattern, String path) {
|
||||
throw new ApiDisabledException();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return RpcContext.getContext() != null;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,50 @@
|
||||
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;
|
||||
|
||||
/**
|
||||
* Sa-Token 整合 Dubbo Consumer 端(调用端)过滤器
|
||||
*
|
||||
* @author click33
|
||||
* @since <= 1.34.0
|
||||
*/
|
||||
@Activate(group = {CommonConstants.CONSUMER}, order = -30000)
|
||||
public class SaTokenDubboConsumerFilter implements Filter {
|
||||
|
||||
@Override
|
||||
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
|
||||
|
||||
// 1、追加 Same-Token 参数
|
||||
if(SaManager.getConfig().getCheckSameToken()) {
|
||||
RpcContext.getContext().setAttachment(SaSameUtil.SAME_TOKEN, SaSameUtil.getToken());
|
||||
}
|
||||
|
||||
// 2、调用前,向下传递会话Token
|
||||
if(SaManager.getSaTokenContextOrSecond() != SaTokenContextDefaultImpl.defaultContext) {
|
||||
RpcContext.getContext().setAttachment(SaTokenConsts.JUST_CREATED, StpUtil.getTokenValueNotCut());
|
||||
}
|
||||
|
||||
// 3、开始调用
|
||||
Result invoke = invoker.invoke(invocation);
|
||||
|
||||
// 4、调用后,解析回传的Token值
|
||||
StpUtil.setTokenValue(invoke.getAttachment(SaTokenConsts.JUST_CREATED_NOT_PREFIX));
|
||||
|
||||
// 5、返回结果
|
||||
return invoke;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,41 @@
|
||||
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.RpcException;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.same.SaSameUtil;
|
||||
|
||||
/**
|
||||
* Sa-Token 整合 Dubbo Provider端(被调用端)过滤器
|
||||
*
|
||||
* @author click33
|
||||
* @since <= 1.34.0
|
||||
*/
|
||||
@Activate(group = {CommonConstants.PROVIDER}, order = -30000)
|
||||
public class SaTokenDubboProviderFilter 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);
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,119 @@
|
||||
package cn.dev33.satoken.context.dubbo.model;
|
||||
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 对 SaRequest 包装类的实现(Dubbo 版)
|
||||
*
|
||||
* @author click33
|
||||
* @since <= 1.34.0
|
||||
*/
|
||||
public class SaRequestForDubbo implements SaRequest {
|
||||
|
||||
/**
|
||||
* 底层对象
|
||||
*/
|
||||
protected RpcContext rpcContext;
|
||||
|
||||
/**
|
||||
* 实例化
|
||||
* @param rpcContext rpcContext对象
|
||||
*/
|
||||
public SaRequestForDubbo(RpcContext rpcContext) {
|
||||
this.rpcContext = rpcContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取底层源对象
|
||||
*/
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return rpcContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在 [请求体] 里获取一个值
|
||||
*/
|
||||
@Override
|
||||
public String getParam(String name) {
|
||||
// 不传播 url 参数
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 [请求体] 里提交的所有参数名称
|
||||
* @return 参数名称列表
|
||||
*/
|
||||
@Override
|
||||
public List<String> getParamNames(){
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取 [请求体] 里提交的所有参数
|
||||
* @return 参数列表
|
||||
*/
|
||||
@Override
|
||||
public Map<String, String> getParamMap(){
|
||||
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;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回当前请求的url,例:http://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;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,74 @@
|
||||
package cn.dev33.satoken.context.dubbo.model;
|
||||
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
|
||||
/**
|
||||
* 对 SaResponse 包装类的实现(Dubbo 版)
|
||||
*
|
||||
* @author click33
|
||||
* @since <= 1.34.0
|
||||
*/
|
||||
public class SaResponseForDubbo implements SaResponse {
|
||||
|
||||
/**
|
||||
* 底层Request对象
|
||||
*/
|
||||
protected RpcContext rpcContext;
|
||||
|
||||
/**
|
||||
* 实例化
|
||||
* @param rpcContext rpcContext对象
|
||||
*/
|
||||
public SaResponseForDubbo(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;
|
||||
}
|
||||
|
||||
|
||||
}
|
@@ -0,0 +1,66 @@
|
||||
package cn.dev33.satoken.context.dubbo.model;
|
||||
|
||||
import cn.dev33.satoken.context.model.SaStorage;
|
||||
import cn.dev33.satoken.util.SaTokenConsts;
|
||||
import org.apache.dubbo.rpc.RpcContext;
|
||||
|
||||
/**
|
||||
* 对 SaStorage 包装类的实现(Dubbo 版)
|
||||
*
|
||||
* @author click33
|
||||
* @since <= 1.34.0
|
||||
*/
|
||||
public class SaStorageForDubbo implements SaStorage {
|
||||
|
||||
/**
|
||||
* 底层对象
|
||||
*/
|
||||
protected RpcContext rpcContext;
|
||||
|
||||
/**
|
||||
* 实例化
|
||||
* @param rpcContext rpcContext对象
|
||||
*/
|
||||
public SaStorageForDubbo(RpcContext rpcContext) {
|
||||
this.rpcContext = rpcContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取底层源对象
|
||||
*/
|
||||
@Override
|
||||
public Object getSource() {
|
||||
return rpcContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 在 [Request作用域] 里写入一个值
|
||||
*/
|
||||
@Override
|
||||
public SaStorageForDubbo 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 SaStorageForDubbo delete(String key) {
|
||||
rpcContext.removeAttachment(key);
|
||||
return this;
|
||||
}
|
||||
|
||||
}
|
@@ -0,0 +1,2 @@
|
||||
saTokenDubboConsumerFilter=cn.dev33.satoken.context.dubbo.filter.SaTokenDubboConsumerFilter
|
||||
saTokenDubboProviderFilter=cn.dev33.satoken.context.dubbo.filter.SaTokenDubboProviderFilter
|
@@ -0,0 +1 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=cn.dev33.satoken.context.dubbo.SaTokenSecondContextCreatorForDubbo
|
@@ -0,0 +1 @@
|
||||
cn.dev33.satoken.context.dubbo.SaTokenSecondContextCreatorForDubbo
|
Reference in New Issue
Block a user