Merge pull request #743 from fangzhengjin/dev

feat: 支持自定义 autoRenew 逻辑
This commit is contained in:
click33 2025-02-21 21:57:26 +08:00 committed by GitHub
commit 096e2bbb63
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 61 additions and 1 deletions

View File

@ -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;
/**
* 函数式接口自定义自动续期条件
*
* <p> 参数StpLogic 实例 </p>
* <p> 返回Boolean 是否续期 </p>
*
* @author fangzhengjin
* @since 1.41.0
*/
@FunctionalInterface
public interface SaAutoRenewFunction extends Function<StpLogic, Boolean> {
}

View File

@ -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);
}

View File

@ -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;
}
//
/**

View File

@ -65,6 +65,15 @@ public BiFunction<AnnotatedElement, Class<? extends Annotation> , Annotation> ge
public BiFunction<String, String, String> spliceTwoUrl = (url1, url2) -> {
return xxx;
};
/**
* 是否自动续期,每次续期前都会执行,可以加入动态判断逻辑
* <p> 参数 当前 stpLogic 实例对象
* <p> 返回 true 自动续期 false 不自动续期
*/
public Function<StpLogic, Boolean> autoRenew = (stpLogic) -> {
return stpLogic.getConfigOrGlobal().getAutoRenew();
};
```