From b2064c75e6af0c5118b53f9adbf620798d683336 Mon Sep 17 00:00:00 2001 From: click33 <2393584716@qq.com> Date: Thu, 11 May 2023 17:26:56 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=20sa-token-reactor-spring-bo?= =?UTF-8?q?ot-starter3=20=E6=A8=A1=E5=9D=97=E6=B3=A8=E9=87=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../satoken/reactor/context/SaReactorSyncHolder.java | 2 +- .../satoken/reactor/model/SaStorageForReactor.java | 2 +- .../satoken/reactor/context/SaReactorHolder.java | 5 +++-- .../satoken/reactor/context/SaReactorSyncHolder.java | 5 +++-- .../reactor/error/SaReactorSpringBootErrorCode.java | 10 +++++----- .../satoken/reactor/filter/SaReactorFilter.java | 10 ++++++++-- .../satoken/reactor/model/SaRequestForReactor.java | 5 +++-- .../satoken/reactor/model/SaResponseForReactor.java | 5 +++-- .../satoken/reactor/model/SaStorageForReactor.java | 7 ++++--- .../spring/SaTokenContextForSpringReactor.java | 4 ++-- .../reactor/spring/SaTokenContextRegister.java | 12 ++++++------ 11 files changed, 39 insertions(+), 28 deletions(-) diff --git a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java index 105eb7c5..18baaa9c 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java +++ b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java @@ -13,7 +13,7 @@ import cn.dev33.satoken.reactor.model.SaResponseForReactor; import cn.dev33.satoken.reactor.model.SaStorageForReactor; /** - * Reactor上下文操作 [同步],持有当前请求的 ServerWebExchange 全局引用 + * Reactor上下文操作(同步),持有当前请求的 ServerWebExchange 全局引用 * * @author click33 * @since <= 1.34.0 diff --git a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java index 3724ee42..9e667190 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java +++ b/sa-token-starter/sa-token-reactor-spring-boot-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java @@ -13,7 +13,7 @@ import cn.dev33.satoken.context.model.SaStorage; public class SaStorageForReactor implements SaStorage { /** - * 底层Request对象 + * 底层 ServerWebExchange 对象 */ protected ServerWebExchange exchange; diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorHolder.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorHolder.java index f3211360..55ec30dc 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorHolder.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorHolder.java @@ -5,9 +5,10 @@ import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; /** - * Reactor上下文操作 [异步] - * @author click33 + * Reactor 上下文操作(异步),持有当前请求的 ServerWebExchange 全局引用 * + * @author click33 + * @since <= 1.34.0 */ public class SaReactorHolder { diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java index 69aeff06..18baaa9c 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/context/SaReactorSyncHolder.java @@ -13,9 +13,10 @@ import cn.dev33.satoken.reactor.model.SaResponseForReactor; import cn.dev33.satoken.reactor.model.SaStorageForReactor; /** - * Reactor上下文操作 [同步] - * @author click33 + * Reactor上下文操作(同步),持有当前请求的 ServerWebExchange 全局引用 * + * @author click33 + * @since <= 1.34.0 */ public class SaReactorSyncHolder { diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/error/SaReactorSpringBootErrorCode.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/error/SaReactorSpringBootErrorCode.java index b9cc63ff..552d39f7 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/error/SaReactorSpringBootErrorCode.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/error/SaReactorSpringBootErrorCode.java @@ -1,20 +1,20 @@ package cn.dev33.satoken.reactor.error; /** - * 定义 sa-token-reactor-spring-boot-starter 所有异常细分状态码 + * 定义 sa-token-reactor3-spring-boot-starter 所有异常细分状态码 * * @author click33 - * @since: 2022-10-30 + * @since <= 1.34.0 */ public interface SaReactorSpringBootErrorCode { /** 对象转 JSON 字符串失败 */ - public static final int CODE_20203 = 20203; + int CODE_20203 = 20203; /** JSON 字符串转 Map 失败 */ - public static final int CODE_20204 = 20204; + int CODE_20204 = 20204; /** 默认的 Filter 异常处理函数 */ - public static final int CODE_20205 = 20205; + int CODE_20205 = 20205; } diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/filter/SaReactorFilter.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/filter/SaReactorFilter.java index 1177e3ab..99f88481 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/filter/SaReactorFilter.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/filter/SaReactorFilter.java @@ -23,9 +23,13 @@ import cn.dev33.satoken.util.SaTokenConsts; import reactor.core.publisher.Mono; /** - * Reactor全局过滤器 - * @author click33 + * Reactor 全局鉴权过滤器 + *
+ * 默认优先级为 -100,尽量保证在其它过滤器之前执行 + *
* + * @author click33 + * @since <= 1.34.0 */ @Order(SaTokenConsts.ASSEMBLY_ORDER) public class SaReactorFilter implements SaFilter, WebFilter { @@ -132,6 +136,8 @@ public class SaReactorFilter implements SaFilter, WebFilter { String result = (e instanceof BackResultException) ? e.getMessage() : String.valueOf(error.run(e)); // 2. 写入输出流 + // 请注意此处默认 Content-Type 为 text/plain,如果需要返回 JSON 信息,需要在 return 前自行设置 Content-Type 为 application/json + // 例如:SaHolder.getResponse().setHeader("Content-Type", "application/json;charset=UTF-8"); if(exchange.getResponse().getHeaders().getFirst("Content-Type") == null) { exchange.getResponse().getHeaders().set("Content-Type", "text/plain; charset=utf-8"); } diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java index 5e8f4ad2..ba3f9812 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaRequestForReactor.java @@ -18,9 +18,10 @@ import java.util.Map; import java.util.Set; /** - * Request for Reactor - * @author click33 + * 对 SaRequest 包装类的实现(Reactor 响应式编程版) * + * @author click33 + * @since <= 1.34.0 */ public class SaRequestForReactor implements SaRequest { diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaResponseForReactor.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaResponseForReactor.java index 46f6f73d..c9bff081 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaResponseForReactor.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaResponseForReactor.java @@ -8,9 +8,10 @@ import org.springframework.http.server.reactive.ServerHttpResponse; import cn.dev33.satoken.context.model.SaResponse; /** - * Response for Reactor - * @author click33 + * 对 SaResponse 包装类的实现(Reactor 响应式编程版) * + * @author click33 + * @since <= 1.34.0 */ public class SaResponseForReactor implements SaResponse { diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java index 7bb94205..9e667190 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/model/SaStorageForReactor.java @@ -5,14 +5,15 @@ import org.springframework.web.server.ServerWebExchange; import cn.dev33.satoken.context.model.SaStorage; /** - * Storage for Reactor - * @author click33 + * 对 SaStorage 包装类的实现(Reactor 响应式编程版) * + * @author click33 + * @since <= 1.34.0 */ public class SaStorageForReactor implements SaStorage { /** - * 底层Request对象 + * 底层 ServerWebExchange 对象 */ protected ServerWebExchange exchange; diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextForSpringReactor.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextForSpringReactor.java index fec0ffd6..e96dd709 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextForSpringReactor.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextForSpringReactor.java @@ -4,10 +4,10 @@ import cn.dev33.satoken.context.SaTokenContextForThreadLocal; import cn.dev33.satoken.spring.SaPathMatcherHolder; /** - * Sa-Token 上下文处理器 [ Spring Reactor 版本实现 ] + * Sa-Token 上下文处理器 [ Spring Reactor 版本实现 ] ,基于 SaTokenContextForThreadLocal 定制 * * @author click33 - * + * @since <= 1.34.0 */ public class SaTokenContextForSpringReactor extends SaTokenContextForThreadLocal { diff --git a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextRegister.java b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextRegister.java index 2684ab0b..db6ef91d 100644 --- a/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextRegister.java +++ b/sa-token-starter/sa-token-reactor-spring-boot3-starter/src/main/java/cn/dev33/satoken/reactor/spring/SaTokenContextRegister.java @@ -5,17 +5,17 @@ import org.springframework.context.annotation.Bean; import cn.dev33.satoken.context.SaTokenContext; /** - * 注册Sa-Token所需要的Bean - *Bean 的注册与注入应该分开在两个文件中,否则在某些场景下会造成循环依赖 - * @author click33 + * 注册 Sa-Token 所需要的 Bean * + * @author click33 + * @since <= 1.34.0 */ public class SaTokenContextRegister { /** - * 获取容器交互Bean (ThreadLocal版) - * - * @return 容器交互Bean (ThreadLocal版) + * 获取上下文处理器组件 (Spring Reactor 版) + * + * @return / */ @Bean public SaTokenContext getSaTokenContextForSpringReactor() {