From 87d2a1156b497195de952822b6fd1c32d0b940cd Mon Sep 17 00:00:00 2001 From: ZhengJin Date: Fri, 21 Feb 2025 21:54:16 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E8=87=AA=E5=AE=9A?= =?UTF-8?q?=E4=B9=89=20autoRenew=20=E9=80=BB=E8=BE=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../fun/strategy/SaAutoRenewFunction.java | 33 +++++++++++++++++++ .../java/cn/dev33/satoken/stp/StpLogic.java | 2 +- .../cn/dev33/satoken/strategy/SaStrategy.java | 18 ++++++++++ sa-token-doc/api/sa-strategy.md | 9 +++++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaAutoRenewFunction.java diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaAutoRenewFunction.java b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaAutoRenewFunction.java new file mode 100644 index 00000000..f68fb9fb --- /dev/null +++ b/sa-token-core/src/main/java/cn/dev33/satoken/fun/strategy/SaAutoRenewFunction.java @@ -0,0 +1,33 @@ +/* + * 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.fun.strategy; + +import cn.dev33.satoken.stp.StpLogic; + +import java.util.function.Function; + +/** + * 函数式接口:自定义自动续期条件 + * + *

参数:StpLogic 实例

+ *

返回:Boolean 是否续期

+ * + * @author fangzhengjin + * @since 1.41.0 + */ +@FunctionalInterface +public interface SaAutoRenewFunction extends Function { +} 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 6d08f7b4..a4a2f768 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 @@ -998,7 +998,7 @@ public class StpLogic { // ------ 至此,loginId 已经是一个合法的值,代表当前会话是一个正常的登录状态了 // 7.2、如果配置了自动续签功能, 则: 更新这个 token 的最后活跃时间 (注意此处的续签是在续 active-timeout,而非 timeout) - if(getConfigOrGlobal().getAutoRenew()) { + if(SaStrategy.instance.autoRenew.apply(this)) { updateLastActiveToNow(tokenValue); } diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java b/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java index 16675f64..facc8bb7 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/strategy/SaStrategy.java @@ -156,6 +156,13 @@ public final class SaStrategy { } }; + /** + * 是否自动续期 + */ + public SaAutoRenewFunction autoRenew = (stpLogic) -> { + return stpLogic.getConfigOrGlobal().getAutoRenew(); + }; + /** * 创建 StpLogic 的算法 */ @@ -221,6 +228,17 @@ public final class SaStrategy { return this; } + /** + * 是否自动续期 + * + * @param autoRenew / + * @return / + */ + public SaStrategy setAutoRenew(SaAutoRenewFunction autoRenew) { + this.autoRenew = autoRenew; + return this; + } + // /** diff --git a/sa-token-doc/api/sa-strategy.md b/sa-token-doc/api/sa-strategy.md index 7529e8cb..23765d94 100644 --- a/sa-token-doc/api/sa-strategy.md +++ b/sa-token-doc/api/sa-strategy.md @@ -65,6 +65,15 @@ public BiFunction , Annotation> ge public BiFunction spliceTwoUrl = (url1, url2) -> { return xxx; }; + +/** + * 是否自动续期,每次续期前都会执行,可以加入动态判断逻辑 + *

参数 当前 stpLogic 实例对象 + *

返回 true 自动续期 false 不自动续期 + */ +public Function autoRenew = (stpLogic) -> { + return stpLogic.getConfigOrGlobal().getAutoRenew(); +}; ```