From 55b9c87d6a03530141243a2d77701ea8939e88f4 Mon Sep 17 00:00:00 2001 From: shengzhang <2393584716@qq.com> Date: Sun, 21 Mar 2021 23:06:06 +0800 Subject: [PATCH] =?UTF-8?q?Session=E6=96=B0=E5=A2=9Etimeout=E6=93=8D?= =?UTF-8?q?=E4=BD=9CAPI?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cn/dev33/satoken/session/SaSession.java | 31 +++++++++++++++++-- .../java/cn/dev33/satoken/stp/StpLogic.java | 11 +++---- 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java b/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java index 2d5d2159..448c3bf2 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java @@ -1,7 +1,5 @@ package cn.dev33.satoken.session; -import cn.dev33.satoken.SaTokenManager; - import java.io.Serializable; import java.util.List; import java.util.Map; @@ -9,6 +7,8 @@ import java.util.Set; import java.util.Vector; import java.util.concurrent.ConcurrentHashMap; +import cn.dev33.satoken.SaTokenManager; + /** * Session Model * @@ -258,6 +258,33 @@ public class SaSession implements Serializable { } } + /** + * 获取此Session的剩余存活时间 (单位: 秒) + * @return 此Session的剩余存活时间 (单位: 秒) + */ + public long getTimeout() { + return SaTokenManager.getSaTokenDao().getSessionTimeout(this.id); + } + + /** + * 修改此Session的剩余存活时间 + * @param timeout 过期时间 (单位: 秒) + */ + public void updateTimeout(long timeout) { + SaTokenManager.getSaTokenDao().updateSessionTimeout(this.id, timeout); + } + + /** + * 修改此Session的最小剩余存活时间 (只有在Session的过期时间低于指定的minTimeout时才会进行修改) + * @param minTimeout 过期时间 (单位: 秒) + */ + public void updateMinTimeout(long minTimeout) { + if(getTimeout() < minTimeout) { + SaTokenManager.getSaTokenDao().updateSessionTimeout(this.id, minTimeout); + } + } + + // ----------------------- 存取值 (类型转换) 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 2be77419..dff30451 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 @@ -244,19 +244,18 @@ public class StpLogic { if(session == null) { session = getSessionByLoginId(loginId); } else { - // 保证此Session的有效期 >= token的有效期 - if(dao.getSessionTimeout(session.getId()) < loginModel.getTimeout()) { - dao.updateSessionTimeout(session.getId(), loginModel.getTimeout()); - } + session.updateMinTimeout(loginModel.getTimeout()); } // 在session上记录token签名 session.addTokenSign(new TokenSign(tokenValue, loginModel.getDevice())); // ------ 4. 持久化其它数据 // token -> uid - dao.set(splicingKeyTokenValue(tokenValue), String.valueOf(loginId), loginModel.getTimeout()); + dao.set(splicingKeyTokenValue(tokenValue), String.valueOf(loginId), loginModel.getTimeout()); + // 写入 [最后操作时间] - setLastActivityToNow(tokenValue); + setLastActivityToNow(tokenValue); + // 在当前会话写入当前tokenValue setTokenValue(tokenValue, loginModel.getCookieTimeout()); }