mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-06-28 13:34:18 +08:00
refactor: 重构所有 rpc 组件的 SaTokenContext 上下文读写策略 & 删除二级上下文模块
This commit is contained in:
parent
c6a081ebf6
commit
3acc7bd7af
@ -21,9 +21,7 @@ import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoaderDefaultImpl;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.config.SaTokenConfigFactory;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.SaTokenContextDefaultImpl;
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocal;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContext;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.dao.SaTokenDaoDefaultImpl;
|
||||
import cn.dev33.satoken.error.SaErrorCode;
|
||||
@ -148,7 +146,7 @@ public class SaManager {
|
||||
}
|
||||
|
||||
/**
|
||||
* 一级上下文 SaTokenContextContext
|
||||
* 上下文 SaTokenContext
|
||||
*/
|
||||
private volatile static SaTokenContext saTokenContext;
|
||||
public static void setSaTokenContext(SaTokenContext saTokenContext) {
|
||||
@ -166,42 +164,6 @@ public class SaManager {
|
||||
return saTokenContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 二级上下文 SaTokenSecondContext
|
||||
*/
|
||||
private volatile static SaTokenSecondContext saTokenSecondContext;
|
||||
public static void setSaTokenSecondContext(SaTokenSecondContext saTokenSecondContext) {
|
||||
SaManager.saTokenSecondContext = saTokenSecondContext;
|
||||
SaTokenEventCenter.doRegisterComponent("SaTokenSecondContext", saTokenSecondContext);
|
||||
}
|
||||
public static SaTokenSecondContext getSaTokenSecondContext() {
|
||||
return saTokenSecondContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取一个可用的 SaTokenContext (按照一级上下文、二级上下文、默认上下文的顺序来判断)
|
||||
* @return /
|
||||
*/
|
||||
public static SaTokenContext getSaTokenContextOrSecond() {
|
||||
|
||||
// s1. 一级Context可用时返回一级Context
|
||||
if(saTokenContext != null) {
|
||||
if(saTokenSecondContext == null || saTokenContext.isValid()) {
|
||||
// 因为 isValid 是一个耗时操作,所以此处假定:二级Context为null的情况下无需验证一级Context有效性
|
||||
// 这样可以提升6倍左右的上下文获取速度
|
||||
return saTokenContext;
|
||||
}
|
||||
}
|
||||
|
||||
// s2. 一级Context不可用时判断二级Context是否可用
|
||||
if(saTokenSecondContext != null && saTokenSecondContext.isValid()) {
|
||||
return saTokenSecondContext;
|
||||
}
|
||||
|
||||
// s3. 都不行,就返回默认的 Context
|
||||
return SaTokenContextDefaultImpl.defaultContext;
|
||||
}
|
||||
|
||||
/**
|
||||
* 临时 token 认证模块
|
||||
*/
|
||||
|
@ -18,6 +18,7 @@ package cn.dev33.satoken.context;
|
||||
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.model.SaTokenContextModelBox;
|
||||
|
||||
/**
|
||||
* Sa-Token 上下文处理器
|
||||
@ -30,37 +31,59 @@ import cn.dev33.satoken.context.model.SaStorage;
|
||||
public interface SaTokenContext {
|
||||
|
||||
/**
|
||||
* 获取当前请求的 Request 包装对象
|
||||
* 初始化上下文
|
||||
*
|
||||
* @param req /
|
||||
* @param res /
|
||||
* @param stg /
|
||||
*/
|
||||
void setContext(SaRequest req, SaResponse res, SaStorage stg);
|
||||
|
||||
/**
|
||||
* 清除化上下文
|
||||
*/
|
||||
void clearContext();
|
||||
|
||||
/**
|
||||
* 判断当前上下文是否可用
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
boolean isValid();
|
||||
|
||||
/**
|
||||
* 获取 Box 对象
|
||||
*/
|
||||
SaTokenContextModelBox getModelBox();
|
||||
|
||||
/**
|
||||
* 获取当前上下文的 Request 包装对象
|
||||
* @see SaRequest
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
SaRequest getRequest();
|
||||
default SaRequest getRequest() {
|
||||
return getModelBox().getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前请求的 Response 包装对象
|
||||
* 获取当前上下文的 Response 包装对象
|
||||
* @see SaResponse
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
SaResponse getResponse();
|
||||
default SaResponse getResponse(){
|
||||
return getModelBox().getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前请求的 Storage 包装对象
|
||||
* 获取当前上下文的 Storage 包装对象
|
||||
* @see SaStorage
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
SaStorage getStorage();
|
||||
|
||||
/**
|
||||
* 判断:在本次请求中,此上下文是否可用。
|
||||
* <p> 例如在部分 rpc 调用时, 一级上下文会返回 false,这时候框架就会选择使用二级上下文来处理请求 </p>
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
default boolean isValid() {
|
||||
return false;
|
||||
default SaStorage getStorage(){
|
||||
return getModelBox().getStorage();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package cn.dev33.satoken.context;
|
||||
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.model.SaTokenContextModelBox;
|
||||
import cn.dev33.satoken.error.SaErrorCode;
|
||||
import cn.dev33.satoken.exception.SaTokenContextException;
|
||||
|
||||
@ -44,6 +45,26 @@ public class SaTokenContextDefaultImpl implements SaTokenContext {
|
||||
*/
|
||||
public static final String ERROR_MESSAGE = "未能获取有效的上下文处理器";
|
||||
|
||||
@Override
|
||||
public void setContext(SaRequest req, SaResponse res, SaStorage stg) {
|
||||
throw new SaTokenContextException(ERROR_MESSAGE).setCode(SaErrorCode.CODE_10001);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void clearContext() {
|
||||
throw new SaTokenContextException(ERROR_MESSAGE).setCode(SaErrorCode.CODE_10001);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
throw new SaTokenContextException(ERROR_MESSAGE).setCode(SaErrorCode.CODE_10001);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaTokenContextModelBox getModelBox() {
|
||||
throw new SaTokenContextException(ERROR_MESSAGE).setCode(SaErrorCode.CODE_10001);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaRequest getRequest() {
|
||||
throw new SaTokenContextException(ERROR_MESSAGE).setCode(SaErrorCode.CODE_10001);
|
||||
|
@ -13,23 +13,34 @@
|
||||
* 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;
|
||||
|
||||
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContext;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||
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.model.SaTokenContextModelBox;
|
||||
|
||||
/**
|
||||
* Sa-Token 二级上下文 - 创建器 [Dubbo3版]
|
||||
* Sa-Token 上下文处理器次级实现:只读上下文
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.34.0
|
||||
* @since 1.42.0
|
||||
*/
|
||||
public class SaTokenSecondContextCreatorForDubbo3 implements SaTokenSecondContextCreator {
|
||||
public interface SaTokenContextForReadOnly extends SaTokenContext {
|
||||
|
||||
@Override
|
||||
public SaTokenSecondContext create() {
|
||||
return new SaTokenSecondContextForDubbo3();
|
||||
default void setContext(SaRequest req, SaResponse res, SaStorage stg) {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
default void clearContext() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
default SaTokenContextModelBox getModelBox() {
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
@ -18,6 +18,7 @@ package cn.dev33.satoken.context;
|
||||
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.model.SaTokenContextModelBox;
|
||||
|
||||
/**
|
||||
* Sa-Token 上下文处理器 [ ThreadLocal 版本 ]
|
||||
@ -35,23 +36,23 @@ import cn.dev33.satoken.context.model.SaStorage;
|
||||
public class SaTokenContextForThreadLocal implements SaTokenContext {
|
||||
|
||||
@Override
|
||||
public SaRequest getRequest() {
|
||||
return SaTokenContextForThreadLocalStorage.getRequest();
|
||||
public void setContext(SaRequest req, SaResponse res, SaStorage stg) {
|
||||
SaTokenContextForThreadLocalStaff.setModelBox(req, res, stg);
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaResponse getResponse() {
|
||||
return SaTokenContextForThreadLocalStorage.getResponse();
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaStorage getStorage() {
|
||||
return SaTokenContextForThreadLocalStorage.getStorage();
|
||||
public void clearContext() {
|
||||
SaTokenContextForThreadLocalStaff.clearModelBox();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return SaTokenContextForThreadLocalStorage.getBox() != null;
|
||||
return SaTokenContextForThreadLocalStaff.getModelBoxOrNull() != null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public SaTokenContextModelBox getModelBox() {
|
||||
return SaTokenContextForThreadLocalStaff.getModelBox();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -18,6 +18,7 @@ package cn.dev33.satoken.context;
|
||||
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.model.SaTokenContextModelBox;
|
||||
import cn.dev33.satoken.error.SaErrorCode;
|
||||
import cn.dev33.satoken.exception.SaTokenContextException;
|
||||
|
||||
@ -29,12 +30,12 @@ import cn.dev33.satoken.exception.SaTokenContextException;
|
||||
* @author click33
|
||||
* @since 1.16.0
|
||||
*/
|
||||
public class SaTokenContextForThreadLocalStorage {
|
||||
public class SaTokenContextForThreadLocalStaff {
|
||||
|
||||
/**
|
||||
* 基于 ThreadLocal 的 [ Box 存储器 ]
|
||||
*/
|
||||
public static ThreadLocal<Box> boxThreadLocal = new InheritableThreadLocal<>();
|
||||
public static ThreadLocal<SaTokenContextModelBox> modelBoxThreadLocal = new InheritableThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 初始化当前线程的 [ Box 存储器 ]
|
||||
@ -42,34 +43,34 @@ public class SaTokenContextForThreadLocalStorage {
|
||||
* @param response {@link SaResponse}
|
||||
* @param storage {@link SaStorage}
|
||||
*/
|
||||
public static void setBox(SaRequest request, SaResponse response, SaStorage storage) {
|
||||
Box bok = new Box(request, response, storage);
|
||||
boxThreadLocal.set(bok);
|
||||
public static void setModelBox(SaRequest request, SaResponse response, SaStorage storage) {
|
||||
SaTokenContextModelBox bok = new SaTokenContextModelBox(request, response, storage);
|
||||
modelBoxThreadLocal.set(bok);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前线程的 [ Box 存储器 ]
|
||||
*/
|
||||
public static void clearBox() {
|
||||
boxThreadLocal.remove();
|
||||
public static void clearModelBox() {
|
||||
modelBoxThreadLocal.remove();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的 [ Box 存储器 ]
|
||||
* @return /
|
||||
*/
|
||||
public static Box getBox() {
|
||||
return boxThreadLocal.get();
|
||||
public static SaTokenContextModelBox getModelBoxOrNull() {
|
||||
return modelBoxThreadLocal.get();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前线程的 [ Box 存储器 ], 如果为空则抛出异常
|
||||
* @return /
|
||||
*/
|
||||
public static Box getBoxNotNull() {
|
||||
Box box = boxThreadLocal.get();
|
||||
public static SaTokenContextModelBox getModelBox() {
|
||||
SaTokenContextModelBox box = modelBoxThreadLocal.get();
|
||||
if(box == null) {
|
||||
throw new SaTokenContextException("未能获取有效的上下文").setCode(SaErrorCode.CODE_10002);
|
||||
throw new SaTokenContextException("SaTokenContext 上下文尚未初始化").setCode(SaErrorCode.CODE_10002);
|
||||
}
|
||||
return box;
|
||||
}
|
||||
@ -80,7 +81,7 @@ public class SaTokenContextForThreadLocalStorage {
|
||||
* @return /
|
||||
*/
|
||||
public static SaRequest getRequest() {
|
||||
return getBoxNotNull().getRequest();
|
||||
return getModelBox().getRequest();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -89,7 +90,7 @@ public class SaTokenContextForThreadLocalStorage {
|
||||
* @return /
|
||||
*/
|
||||
public static SaResponse getResponse() {
|
||||
return getBoxNotNull().getResponse();
|
||||
return getModelBox().getResponse();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -98,59 +99,8 @@ public class SaTokenContextForThreadLocalStorage {
|
||||
* @return /
|
||||
*/
|
||||
public static SaStorage getStorage() {
|
||||
return getBoxNotNull().getStorage();
|
||||
return getModelBox().getStorage();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Box 临时内部类,用于存储 [ SaRequest、SaResponse、SaStorage ] 三个包装对象
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.16.0
|
||||
*/
|
||||
public static class Box {
|
||||
|
||||
public SaRequest request;
|
||||
|
||||
public SaResponse response;
|
||||
|
||||
public SaStorage storage;
|
||||
|
||||
public Box(SaRequest request, SaResponse response, SaStorage storage){
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
public SaRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
public void setRequest(SaRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public SaResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public void setResponse(SaResponse response) {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public SaStorage getStorage() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
public void setStorage(SaStorage storage) {
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Box [request=" + request + ", response=" + response + ", storage=" + storage + "]";
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,52 @@
|
||||
package cn.dev33.satoken.context.model;
|
||||
|
||||
/**
|
||||
* Box 盒子类,用于存储 [ SaRequest、SaResponse、SaStorage ] 三个包装对象
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.16.0
|
||||
*/
|
||||
public class SaTokenContextModelBox {
|
||||
|
||||
public SaRequest request;
|
||||
|
||||
public SaResponse response;
|
||||
|
||||
public SaStorage storage;
|
||||
|
||||
public SaTokenContextModelBox(SaRequest request, SaResponse response, SaStorage storage) {
|
||||
this.request = request;
|
||||
this.response = response;
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
public SaRequest getRequest() {
|
||||
return request;
|
||||
}
|
||||
|
||||
public void setRequest(SaRequest request) {
|
||||
this.request = request;
|
||||
}
|
||||
|
||||
public SaResponse getResponse() {
|
||||
return response;
|
||||
}
|
||||
|
||||
public void setResponse(SaResponse response) {
|
||||
this.response = response;
|
||||
}
|
||||
|
||||
public SaStorage getStorage() {
|
||||
return storage;
|
||||
}
|
||||
|
||||
public void setStorage(SaStorage storage) {
|
||||
this.storage = storage;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Box [request=" + request + ", response=" + response + ", storage=" + storage + "]";
|
||||
}
|
||||
|
||||
}
|
@ -1,32 +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.second;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
|
||||
/**
|
||||
* Sa-Token 二级Context - 基础接口
|
||||
*
|
||||
* <p> (利用继承机制实现区别 [ 一级Context ] 与 [ 二级Context ] 的目的)
|
||||
*
|
||||
* @see SaTokenContext SaTokenContext 上下文处理器
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.28.0
|
||||
*/
|
||||
public interface SaTokenSecondContext extends SaTokenContext {
|
||||
|
||||
}
|
@ -1,33 +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.second;
|
||||
|
||||
/**
|
||||
* Sa-Token 二级Context - 创建器
|
||||
*
|
||||
* @author click33
|
||||
* @since 1.28.0
|
||||
*/
|
||||
@FunctionalInterface
|
||||
public interface SaTokenSecondContextCreator {
|
||||
|
||||
/**
|
||||
* 创建一个二级 Context 处理器
|
||||
* @return /
|
||||
*/
|
||||
SaTokenSecondContext create();
|
||||
|
||||
}
|
@ -206,6 +206,16 @@ public class SaTokenConsts {
|
||||
*/
|
||||
public static final int SA_TOKEN_CONTEXT_FILTER_ORDER = -104;
|
||||
|
||||
/**
|
||||
* RPC 框架权限过滤器的注册顺序
|
||||
*/
|
||||
public static final int RPC_PERMISSION_FILTER_ORDER = -30000;
|
||||
|
||||
/**
|
||||
* RPC 框架上下文过滤器的注册顺序
|
||||
*/
|
||||
public static final int RPC_CONTEXT_FILTER_ORDER = -30005;
|
||||
|
||||
/**
|
||||
* Content-Type key
|
||||
*/
|
||||
|
@ -1,32 +1,33 @@
|
||||
package com.pj.more;
|
||||
package com.pj.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import com.pj.service.DemoService;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@DubboReference
|
||||
private DemoService demoService;
|
||||
|
||||
// Consumer端登录,状态传播到Provider端
|
||||
// Consumer端登录,状态传播到Provider端 --- http://localhost:8081/test
|
||||
@RequestMapping("test")
|
||||
public String test() {
|
||||
public SaResult test() {
|
||||
demoService.isLogin("----------- 登录前 ");
|
||||
|
||||
StpUtil.login(10001);
|
||||
|
||||
demoService.isLogin("----------- 登录后 ");
|
||||
|
||||
return "ok";
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// Provider端登录,状态回传到Consumer端
|
||||
// Provider端登录,状态回传到Consumer端 --- http://localhost:8081/test2
|
||||
@RequestMapping("test2")
|
||||
public String test2() {
|
||||
public SaResult test2() {
|
||||
System.out.println("----------- 登录前 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
@ -35,38 +36,38 @@ public class TestController {
|
||||
|
||||
System.out.println("----------- 登录后 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return "ok";
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// Consumer端登录,状态在Consumer端保持
|
||||
// Consumer端登录,状态在Consumer端保持 --- http://localhost:8081/test3
|
||||
@RequestMapping("test3")
|
||||
public String test3() {
|
||||
public SaResult test3() {
|
||||
System.out.println("----------- 登录前 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
StpUtil.login(10003);
|
||||
demoService.isLogin("----------- Provider状态");
|
||||
|
||||
|
||||
System.out.println("----------- 登录后 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return "ok";
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// Provider端登录,状态在Provider端保持
|
||||
// Provider端登录,状态在Provider端保持 --- http://localhost:8081/test4
|
||||
@RequestMapping("test4")
|
||||
public String test4() {
|
||||
public SaResult test4() {
|
||||
// 登录
|
||||
demoService.doLogin(10004);
|
||||
|
||||
// 打印一下
|
||||
demoService.isLogin("----------- 会话信息 ");
|
||||
|
||||
return "ok";
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.pj.more;
|
||||
package com.pj.service;
|
||||
|
||||
public interface DemoService {
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.pj.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import com.pj.service.DemoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@Autowired
|
||||
private DemoService demoService;
|
||||
|
||||
// test
|
||||
@RequestMapping("test")
|
||||
public SaResult test() {
|
||||
demoService.isLogin("----------- 登录前 " + StpUtil.isLogin());
|
||||
|
||||
StpUtil.login(10001);
|
||||
|
||||
demoService.isLogin("----------- 登录后 " + StpUtil.isLogin());
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.pj.more;
|
||||
package com.pj.service;
|
||||
|
||||
public interface DemoService {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.pj.more;
|
||||
package com.pj.service;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
|
@ -1,11 +1,12 @@
|
||||
package com.pj.more;
|
||||
package com.pj.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import com.pj.service.DemoService;
|
||||
import org.apache.dubbo.config.annotation.DubboReference;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@ -14,19 +15,19 @@ public class TestController {
|
||||
|
||||
// Consumer端登录,状态传播到Provider端
|
||||
@RequestMapping("test")
|
||||
public String test() {
|
||||
public SaResult test() {
|
||||
demoService.isLogin("----------- 登录前 ");
|
||||
|
||||
StpUtil.login(10001);
|
||||
|
||||
demoService.isLogin("----------- 登录后 ");
|
||||
|
||||
return "ok";
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// Provider端登录,状态回传到Consumer端
|
||||
@RequestMapping("test2")
|
||||
public String test2() {
|
||||
public SaResult test2() {
|
||||
System.out.println("----------- 登录前 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
@ -35,14 +36,14 @@ public class TestController {
|
||||
|
||||
System.out.println("----------- 登录后 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return "ok";
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// Consumer端登录,状态在Consumer端保持
|
||||
@RequestMapping("test3")
|
||||
public String test3() {
|
||||
public SaResult test3() {
|
||||
System.out.println("----------- 登录前 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
@ -52,21 +53,21 @@ public class TestController {
|
||||
|
||||
System.out.println("----------- 登录后 ");
|
||||
System.out.println("Token值:" + StpUtil.getTokenValue());
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return "ok";
|
||||
System.out.println("是否登录:" + StpUtil.isLogin());
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
// Provider端登录,状态在Provider端保持
|
||||
@RequestMapping("test4")
|
||||
public String test4() {
|
||||
public SaResult test4() {
|
||||
// 登录
|
||||
demoService.doLogin(10004);
|
||||
|
||||
// 打印一下
|
||||
demoService.isLogin("----------- 会话信息 ");
|
||||
|
||||
return "ok";
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.pj.more;
|
||||
package com.pj.service;
|
||||
|
||||
public interface DemoService {
|
||||
|
@ -0,0 +1,28 @@
|
||||
package com.pj.controller;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import com.pj.service.DemoService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
@RestController
|
||||
public class TestController {
|
||||
|
||||
@Autowired
|
||||
private DemoService demoService;
|
||||
|
||||
// test
|
||||
@RequestMapping("test")
|
||||
public SaResult test() {
|
||||
demoService.isLogin("----------- 登录前 " + StpUtil.isLogin());
|
||||
|
||||
StpUtil.login(10001);
|
||||
|
||||
demoService.isLogin("----------- 登录后 " + StpUtil.isLogin());
|
||||
|
||||
return SaResult.ok();
|
||||
}
|
||||
|
||||
}
|
@ -1,4 +1,4 @@
|
||||
package com.pj.more;
|
||||
package com.pj.service;
|
||||
|
||||
public interface DemoService {
|
||||
|
@ -1,4 +1,4 @@
|
||||
package com.pj.more;
|
||||
package com.pj.service;
|
||||
|
||||
import org.apache.dubbo.config.annotation.DubboService;
|
||||
|
@ -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
|
@ -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();
|
||||
}
|
||||
|
||||
}
|
@ -15,11 +15,11 @@
|
||||
*/
|
||||
package cn.dev33.satoken.servlet.util;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage;
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage.Box;
|
||||
import cn.dev33.satoken.SaManager;
|
||||
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.model.SaTokenContextModelBox;
|
||||
import cn.dev33.satoken.fun.SaFunction;
|
||||
import cn.dev33.satoken.servlet.model.SaRequestForServlet;
|
||||
import cn.dev33.satoken.servlet.model.SaResponseForServlet;
|
||||
@ -45,14 +45,14 @@ public class SaTokenContextUtil {
|
||||
SaRequest req = new SaRequestForServlet(request);
|
||||
SaResponse res = new SaResponseForServlet(response);
|
||||
SaStorage stg = new SaStorageForServlet(request);
|
||||
SaTokenContextForThreadLocalStorage.setBox(req, res, stg);
|
||||
SaManager.getSaTokenContext().setContext(req, res, stg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前上下文
|
||||
*/
|
||||
public static void clearContext() {
|
||||
SaTokenContextForThreadLocalStorage.clearBox();
|
||||
SaManager.getSaTokenContext().clearContext();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,11 +71,11 @@ public class SaTokenContextUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 Box
|
||||
* 获取当前 ModelBox
|
||||
* @return /
|
||||
*/
|
||||
public static Box getBox() {
|
||||
return SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
public static SaTokenContextModelBox getModelBox() {
|
||||
return SaManager.getSaTokenContext().getModelBox();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,8 +83,7 @@ public class SaTokenContextUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
return (HttpServletRequest) box.getRequest().getSource();
|
||||
return (HttpServletRequest) getModelBox().getRequest().getSource();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,8 +91,7 @@ public class SaTokenContextUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
return (HttpServletResponse) box.getResponse().getSource();
|
||||
return (HttpServletResponse) getModelBox().getResponse().getSource();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.jboot;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.SaTokenContextForReadOnly;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.context.model.SaStorage;
|
||||
@ -28,7 +28,7 @@ import io.jboot.web.controller.JbootControllerContext;
|
||||
/**
|
||||
* Sa-Token 上线文处理器 [Jboot 版本实现]
|
||||
*/
|
||||
public class SaTokenContextForJboot implements SaTokenContext {
|
||||
public class SaTokenContextForJboot implements SaTokenContextForReadOnly {
|
||||
|
||||
public SaTokenContextForJboot() {
|
||||
// 重写路由匹配算法
|
||||
@ -63,6 +63,6 @@ public class SaTokenContextForJboot implements SaTokenContext {
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return SaTokenContext.super.isValid();
|
||||
return JbootControllerContext.get() != null;
|
||||
}
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.jfinal;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.SaTokenContextForReadOnly;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.context.model.SaStorage;
|
||||
@ -27,7 +27,7 @@ import cn.dev33.satoken.strategy.SaStrategy;
|
||||
/**
|
||||
* Sa-Token 上线文处理器 [Jfinal 版本实现]
|
||||
*/
|
||||
public class SaTokenContextForJfinal implements SaTokenContext {
|
||||
public class SaTokenContextForJfinal implements SaTokenContextForReadOnly {
|
||||
|
||||
public SaTokenContextForJfinal() {
|
||||
// 重写路由匹配算法
|
||||
@ -62,6 +62,7 @@ public class SaTokenContextForJfinal implements SaTokenContext {
|
||||
|
||||
@Override
|
||||
public boolean isValid() {
|
||||
return SaTokenContext.super.isValid();
|
||||
return SaControllerContext.get() != null;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -15,11 +15,11 @@
|
||||
*/
|
||||
package cn.dev33.satoken.reactor.context;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage;
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage.Box;
|
||||
import cn.dev33.satoken.SaManager;
|
||||
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.model.SaTokenContextModelBox;
|
||||
import cn.dev33.satoken.fun.SaRetGenericFunction;
|
||||
import cn.dev33.satoken.reactor.model.SaRequestForReactor;
|
||||
import cn.dev33.satoken.reactor.model.SaResponseForReactor;
|
||||
@ -42,14 +42,14 @@ public class SaReactorSyncHolder {
|
||||
SaRequest request = new SaRequestForReactor(exchange.getRequest());
|
||||
SaResponse response = new SaResponseForReactor(exchange.getResponse());
|
||||
SaStorage storage = new SaStorageForReactor(exchange);
|
||||
SaTokenContextForThreadLocalStorage.setBox(request, response, storage);
|
||||
SaManager.getSaTokenContext().setContext(request, response, storage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在同步上下文清除 ServerWebExchange
|
||||
*/
|
||||
public static void clearContext() {
|
||||
SaTokenContextForThreadLocalStorage.clearBox();
|
||||
SaManager.getSaTokenContext().clearContext();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,7 +57,7 @@ public class SaReactorSyncHolder {
|
||||
* @return /
|
||||
*/
|
||||
public static ServerWebExchange getExchange() {
|
||||
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
SaTokenContextModelBox box = SaManager.getSaTokenContext().getModelBox();
|
||||
return (ServerWebExchange)box.getStorage().getSource();
|
||||
}
|
||||
|
||||
|
@ -15,11 +15,11 @@
|
||||
*/
|
||||
package cn.dev33.satoken.reactor.context;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage;
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage.Box;
|
||||
import cn.dev33.satoken.SaManager;
|
||||
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.model.SaTokenContextModelBox;
|
||||
import cn.dev33.satoken.fun.SaRetGenericFunction;
|
||||
import cn.dev33.satoken.reactor.model.SaRequestForReactor;
|
||||
import cn.dev33.satoken.reactor.model.SaResponseForReactor;
|
||||
@ -42,14 +42,14 @@ public class SaReactorSyncHolder {
|
||||
SaRequest request = new SaRequestForReactor(exchange.getRequest());
|
||||
SaResponse response = new SaResponseForReactor(exchange.getResponse());
|
||||
SaStorage storage = new SaStorageForReactor(exchange);
|
||||
SaTokenContextForThreadLocalStorage.setBox(request, response, storage);
|
||||
SaManager.getSaTokenContext().setContext(request, response, storage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在同步上下文清除 ServerWebExchange
|
||||
*/
|
||||
public static void clearContext() {
|
||||
SaTokenContextForThreadLocalStorage.clearBox();
|
||||
SaManager.getSaTokenContext().clearContext();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -57,7 +57,7 @@ public class SaReactorSyncHolder {
|
||||
* @return /
|
||||
*/
|
||||
public static ServerWebExchange getExchange() {
|
||||
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
SaTokenContextModelBox box = SaManager.getSaTokenContext().getModelBox();
|
||||
return (ServerWebExchange)box.getStorage().getSource();
|
||||
}
|
||||
|
||||
|
@ -15,11 +15,11 @@
|
||||
*/
|
||||
package cn.dev33.satoken.servlet.util;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage;
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage.Box;
|
||||
import cn.dev33.satoken.SaManager;
|
||||
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.model.SaTokenContextModelBox;
|
||||
import cn.dev33.satoken.fun.SaFunction;
|
||||
import cn.dev33.satoken.servlet.model.SaRequestForServlet;
|
||||
import cn.dev33.satoken.servlet.model.SaResponseForServlet;
|
||||
@ -45,14 +45,14 @@ public class SaTokenContextUtil {
|
||||
SaRequest req = new SaRequestForServlet(request);
|
||||
SaResponse res = new SaResponseForServlet(response);
|
||||
SaStorage stg = new SaStorageForServlet(request);
|
||||
SaTokenContextForThreadLocalStorage.setBox(req, res, stg);
|
||||
SaManager.getSaTokenContext().setContext(req, res, stg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前上下文
|
||||
*/
|
||||
public static void clearContext() {
|
||||
SaTokenContextForThreadLocalStorage.clearBox();
|
||||
SaManager.getSaTokenContext().clearContext();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -71,11 +71,11 @@ public class SaTokenContextUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 Box
|
||||
* 获取当前 ModelBox
|
||||
* @return /
|
||||
*/
|
||||
public static Box getBox() {
|
||||
return SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
public static SaTokenContextModelBox getModelBox() {
|
||||
return SaManager.getSaTokenContext().getModelBox();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -83,8 +83,7 @@ public class SaTokenContextUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static HttpServletRequest getRequest() {
|
||||
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
return (HttpServletRequest) box.getRequest().getSource();
|
||||
return (HttpServletRequest) getModelBox().getRequest().getSource();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -92,8 +91,7 @@ public class SaTokenContextUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static HttpServletResponse getResponse() {
|
||||
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
return (HttpServletResponse) box.getResponse().getSource();
|
||||
return (HttpServletResponse) getModelBox().getResponse().getSource();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,6 @@ import cn.dev33.satoken.apikey.SaApiKeyTemplate;
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoader;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.httpauth.basic.SaHttpBasicTemplate;
|
||||
import cn.dev33.satoken.httpauth.basic.SaHttpBasicUtil;
|
||||
@ -121,17 +120,6 @@ public class SaBeanInject {
|
||||
SaManager.setSaTokenContext(saTokenContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入二级上下文Bean
|
||||
*
|
||||
* @param saTokenSecondContextCreator 二级上下文创建器
|
||||
*/
|
||||
@Condition(onBean = SaTokenSecondContextCreator.class)
|
||||
@Bean
|
||||
public void setSaTokenContext(SaTokenSecondContextCreator saTokenSecondContextCreator) {
|
||||
SaManager.setSaTokenSecondContext(saTokenSecondContextCreator.create());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入侦听器Bean
|
||||
*
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.solon.model;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.SaTokenContextForReadOnly;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.context.model.SaStorage;
|
||||
@ -27,7 +27,7 @@ import org.noear.solon.core.handle.Context;
|
||||
* @author noear
|
||||
* @since 1.4
|
||||
*/
|
||||
public class SaContextForSolon implements SaTokenContext {
|
||||
public class SaContextForSolon implements SaTokenContextForReadOnly {
|
||||
|
||||
/**
|
||||
* 获取当前请求的Request对象
|
||||
|
@ -15,11 +15,11 @@
|
||||
*/
|
||||
package cn.dev33.satoken.solon.util;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage;
|
||||
import cn.dev33.satoken.context.SaTokenContextForThreadLocalStorage.Box;
|
||||
import cn.dev33.satoken.SaManager;
|
||||
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.model.SaTokenContextModelBox;
|
||||
import cn.dev33.satoken.fun.SaFunction;
|
||||
import cn.dev33.satoken.solon.model.SaRequestForSolon;
|
||||
import cn.dev33.satoken.solon.model.SaResponseForSolon;
|
||||
@ -41,14 +41,14 @@ public class SaTokenContextUtil {
|
||||
SaRequest req = new SaRequestForSolon(ctx);
|
||||
SaResponse res = new SaResponseForSolon(ctx);
|
||||
SaStorage stg = new SaStorageForSolon(ctx);
|
||||
SaTokenContextForThreadLocalStorage.setBox(req, res, stg);
|
||||
SaManager.getSaTokenContext().setContext(req, res, stg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除当前上下文
|
||||
*/
|
||||
public static void clearContext() {
|
||||
SaTokenContextForThreadLocalStorage.clearBox();
|
||||
SaManager.getSaTokenContext().clearContext();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -66,11 +66,11 @@ public class SaTokenContextUtil {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前 Box
|
||||
* 获取当前 ModelBox
|
||||
* @return /
|
||||
*/
|
||||
public static Box getBox() {
|
||||
return SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
public static SaTokenContextModelBox getModelBox() {
|
||||
return SaManager.getSaTokenContext().getModelBox();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -78,8 +78,7 @@ public class SaTokenContextUtil {
|
||||
* @return /
|
||||
*/
|
||||
public static Context getContext() {
|
||||
Box box = SaTokenContextForThreadLocalStorage.getBoxNotNull();
|
||||
return (Context) box.getStorage().getSource();
|
||||
return (Context) getModelBox().getStorage().getSource();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -21,7 +21,6 @@ import cn.dev33.satoken.apikey.SaApiKeyTemplate;
|
||||
import cn.dev33.satoken.apikey.loader.SaApiKeyDataLoader;
|
||||
import cn.dev33.satoken.config.SaTokenConfig;
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.second.SaTokenSecondContextCreator;
|
||||
import cn.dev33.satoken.dao.SaTokenDao;
|
||||
import cn.dev33.satoken.httpauth.basic.SaHttpBasicTemplate;
|
||||
import cn.dev33.satoken.httpauth.basic.SaHttpBasicUtil;
|
||||
@ -115,16 +114,6 @@ public class SaBeanInject {
|
||||
SaManager.setSaTokenContext(saTokenContext);
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入二级上下文Bean
|
||||
*
|
||||
* @param saTokenSecondContextCreator 二级上下文创建器
|
||||
*/
|
||||
@Autowired(required = false)
|
||||
public void setSaTokenContext(SaTokenSecondContextCreator saTokenSecondContextCreator) {
|
||||
SaManager.setSaTokenSecondContext(saTokenSecondContextCreator.create());
|
||||
}
|
||||
|
||||
/**
|
||||
* 注入侦听器Bean
|
||||
*
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.spring;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.SaTokenContextForReadOnly;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.context.model.SaStorage;
|
||||
@ -31,7 +31,7 @@ import cn.dev33.satoken.servlet.model.SaStorageForServlet;
|
||||
* @author click33
|
||||
* @since 1.19.0
|
||||
*/
|
||||
public class SaTokenContextForSpring implements SaTokenContext {
|
||||
public class SaTokenContextForSpring implements SaTokenContextForReadOnly {
|
||||
|
||||
/**
|
||||
* 获取当前请求的 Request 包装对象
|
||||
|
@ -15,7 +15,7 @@
|
||||
*/
|
||||
package cn.dev33.satoken.spring;
|
||||
|
||||
import cn.dev33.satoken.context.SaTokenContext;
|
||||
import cn.dev33.satoken.context.SaTokenContextForReadOnly;
|
||||
import cn.dev33.satoken.context.model.SaRequest;
|
||||
import cn.dev33.satoken.context.model.SaResponse;
|
||||
import cn.dev33.satoken.context.model.SaStorage;
|
||||
@ -31,7 +31,7 @@ import cn.dev33.satoken.servlet.model.SaStorageForServlet;
|
||||
* @author click33
|
||||
* @since 1.34.0
|
||||
*/
|
||||
public class SaTokenContextForSpringInJakartaServlet implements SaTokenContext {
|
||||
public class SaTokenContextForSpringInJakartaServlet implements SaTokenContextForReadOnly {
|
||||
|
||||
/**
|
||||
* 获取当前请求的 Request 包装对象
|
||||
|
Loading…
Reference in New Issue
Block a user