From 92f48256e9d3c9b8306edd49efd23198000bd967 Mon Sep 17 00:00:00 2001 From: click33 <36243476+click33@users.noreply.github.com> Date: Wed, 2 Apr 2025 00:17:56 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=96=B0=E5=A2=9E=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E9=A1=B9=20cookieAutoFillPrefix=EF=BC=9Acookie=20=E6=A8=A1?= =?UTF-8?q?=E5=BC=8F=E6=98=AF=E5=90=A6=E8=87=AA=E5=8A=A8=E5=A1=AB=E5=85=85?= =?UTF-8?q?=20token=20=E5=89=8D=E7=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dev33/satoken/config/SaTokenConfig.java | 26 +++++++++++++++++-- .../java/cn/dev33/satoken/stp/StpLogic.java | 3 +++ sa-token-doc/use/config.md | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java b/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java index 1c4ea749..4d375572 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/config/SaTokenConfig.java @@ -153,6 +153,11 @@ public class SaTokenConfig implements Serializable { */ private String tokenPrefix; + /** + * cookie 模式是否自动填充 token 前缀 + */ + private Boolean cookieAutoFillPrefix = false; + /** * 是否在初始化配置时在控制台打印版本字符画 */ @@ -517,7 +522,23 @@ public class SaTokenConfig implements Serializable { this.tokenPrefix = tokenPrefix; return this; } - + + /** + * @return cookie 模式是否自动填充 token 前缀 + */ + public Boolean getCookieAutoFillPrefix() { + return cookieAutoFillPrefix; + } + + /** + * @param cookieAutoFillPrefix cookie 模式是否自动填充 token 前缀 + * @return 对象自身 + */ + public SaTokenConfig setCookieAutoFillPrefix(Boolean cookieAutoFillPrefix) { + this.cookieAutoFillPrefix = cookieAutoFillPrefix; + return this; + } + /** * @return 是否在初始化配置时在控制台打印版本字符画 */ @@ -877,8 +898,9 @@ public class SaTokenConfig implements Serializable { + ", tokenStyle=" + tokenStyle + ", dataRefreshPeriod=" + dataRefreshPeriod + ", tokenSessionCheckLogin=" + tokenSessionCheckLogin - + ", autoRenew=" + autoRenew + + ", autoRenew=" + autoRenew + ", tokenPrefix=" + tokenPrefix + + ", cookieAutoFillPrefix=" + cookieAutoFillPrefix + ", isPrint=" + isPrint + ", isLog=" + isLog + ", logLevel=" + logLevel diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java index 95e21939..9b647f4b 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java @@ -351,6 +351,9 @@ public class StpLogic { // 4. 最后尝试从 cookie 里读取 if(SaFoxUtil.isEmpty(tokenValue) && config.getIsReadCookie()){ tokenValue = request.getCookieValue(keyTokenName); + if(SaFoxUtil.isNotEmpty(tokenValue) && config.getCookieAutoFillPrefix()) { + tokenValue = config.getTokenPrefix() + SaTokenConsts.TOKEN_CONNECTOR_CHAT + tokenValue; + } } // 5. 至此,不管有没有读取到,都不再尝试了,直接返回 diff --git a/sa-token-doc/use/config.md b/sa-token-doc/use/config.md index 4af03caf..70f888f1 100644 --- a/sa-token-doc/use/config.md +++ b/sa-token-doc/use/config.md @@ -139,6 +139,7 @@ public class SaTokenConfigure { | tokenSessionCheckLogin | Boolean | true | 获取 `Token-Session` 时是否必须登录 (如果配置为true,会在每次获取 `Token-Session` 时校验是否登录),[详解](/use/config?id=配置项详解:tokenSessionCheckLogin) | | autoRenew | Boolean | true | 是否打开自动续签 (如果此值为true,框架会在每次直接或间接调用 `getLoginId()` 时进行一次过期检查与续签操作),[参考:token有效期详解](/fun/token-timeout) | | tokenPrefix | String | null | token前缀,例如填写 `Bearer` 实际传参 `satoken: Bearer xxxx-xxxx-xxxx-xxxx` [参考:自定义Token前缀](/up/token-prefix) | +| cookieAutoFillPrefix | Boolean | false | cookie 模式是否自动填充 token 前缀 | | isPrint | Boolean | true | 是否在初始化配置时打印版本字符画 | | isLog | Boolean | false | 是否打印操作日志 | | logLevel | String | trace | 日志等级(trace、debug、info、warn、error、fatal),此值与 logLevelInt 联动 |