增加配置,指定在获取token专属session时是否必须登录

This commit is contained in:
shengzhang 2020-12-27 23:45:47 +08:00
parent a59ee9408b
commit 930c28df6d
3 changed files with 72 additions and 62 deletions

View File

@ -7,54 +7,37 @@ package cn.dev33.satoken.config;
*/
public class SaTokenConfig {
/**
* token名称 (同时也是cookie名称)
*/
/** token名称 (同时也是cookie名称) */
private String tokenName = "satoken";
/**
* token有效期单位/ 默认30天, -1代表永久
*/
/** token有效期单位/秒 默认30天, -1代表永久 */
private long timeout = 30 * 24 * 60 * 60;
/**
* token临时有效期 (指定时间内无操作就视为token过期) 单位/, 默认-1 代表不限制 (例如可以设置为1800代表30分钟内无操作就过期)
*/
/** token临时有效期 (指定时间内无操作就视为token过期) 单位/秒, 默认-1 代表不限制 (例如可以设置为1800代表30分钟内无操作就过期) */
private long activityTimeout = -1;
/**
* 在多人登录同一账号时是否共享会话 (为true时共用一个为false时新登录挤掉旧登录)
*/
/** 在多人登录同一账号时,是否共享会话 (为true时共用一个为false时新登录挤掉旧登录) */
private Boolean isShare = true;
/**
* 是否尝试从请求体里读取token
*/
/** 是否尝试从请求体里读取token */
private Boolean isReadBody = true;
/**
* 是否尝试从header里读取token
*/
/** 是否尝试从header里读取token */
private Boolean isReadHead = true;
/**
* 是否尝试从cookie里读取token
*/
/** 是否尝试从cookie里读取token */
private Boolean isReadCookie = true;
/**
* token风格
*/
/** token风格 */
private String tokenStyle = "uuid";
/**
* 默认dao层实现类中每次清理过期数据间隔的时间 (单位: ) 默认值30秒设置为-1代表不启动定时清理
*/
/** 默认dao层实现类中每次清理过期数据间隔的时间 (单位: 秒) 默认值30秒设置为-1代表不启动定时清理 */
private int dataRefreshPeriod = 30;
/**
* 是否在初始化配置时打印版本字符画
*/
/** 获取token专属session时是否必须登录 */
private Boolean tokenSessionCheckLogin = true;
/** 是否在初始化配置时打印版本字符画 */
private Boolean isV = true;
@ -199,7 +182,23 @@ public class SaTokenConfig {
this.dataRefreshPeriod = dataRefreshPeriod;
}
/**
* @return tokenSessionCheckLogin
*/
public Boolean getTokenSessionCheckLogin() {
return tokenSessionCheckLogin;
}
/**
* @param tokenSessionCheckLogin 要设置的 tokenSessionCheckLogin
*/
public void setTokenSessionCheckLogin(Boolean tokenSessionCheckLogin) {
this.tokenSessionCheckLogin = tokenSessionCheckLogin;
}
/**
* 将对象转为String字符串
*/
@ -207,8 +206,8 @@ public class SaTokenConfig {
public String toString() {
return "SaTokenConfig [tokenName=" + tokenName + ", timeout=" + timeout + ", activityTimeout=" + activityTimeout
+ ", isShare=" + isShare + ", isReadBody=" + isReadBody + ", isReadHead=" + isReadHead
+ ", isReadCookie=" + isReadCookie + ", tokenStyle=" + tokenStyle + ", isV=" + isV
+ ", dataRefreshPeriod=" + dataRefreshPeriod + "]";
+ ", isReadCookie=" + isReadCookie + ", tokenStyle=" + tokenStyle + ", dataRefreshPeriod="
+ dataRefreshPeriod + ", tokenSessionCheckLogin=" + tokenSessionCheckLogin + ", isV=" + isV + "]";
}

View File

@ -61,7 +61,7 @@ public class StpLogic {
public String getTokenValue(){
// 0. 获取相应对象
HttpServletRequest request = SaTokenManager.getSaTokenServlet().getRequest();
SaTokenConfig config = SaTokenManager.getConfig();
SaTokenConfig config = getConfig();
String keyTokenName = getTokenName();
String tokenValue = null;
@ -135,7 +135,7 @@ public class StpLogic {
// 1获取相应对象
HttpServletRequest request = SaTokenManager.getSaTokenServlet().getRequest();
SaTokenConfig config = SaTokenManager.getConfig();
SaTokenConfig config = getConfig();
SaTokenDao dao = SaTokenManager.getSaTokenDao();
// 2获取tokenValue
@ -171,7 +171,7 @@ public class StpLogic {
return;
}
// 如果打开了cookie模式第一步先把cookie清除掉
if(SaTokenManager.getConfig().getIsReadCookie() == true){
if(getConfig().getIsReadCookie() == true){
SaTokenManager.getSaTokenCookie().delCookie(SaTokenManager.getSaTokenServlet().getRequest(), SaTokenManager.getSaTokenServlet().getResponse(), getTokenName());
}
// 尝试从db中获取loginId值
@ -387,7 +387,7 @@ public class StpLogic {
SaSession session = SaTokenManager.getSaTokenDao().getSession(sessionId);
if(session == null && isCreate) {
session = new SaSession(sessionId);
SaTokenManager.getSaTokenDao().saveSession(session, SaTokenManager.getConfig().getTimeout());
SaTokenManager.getSaTokenDao().saveSession(session, getConfig().getTimeout());
}
return session;
}
@ -445,7 +445,9 @@ public class StpLogic {
* @return session会话
*/
public SaSession getTokenSession() {
checkLogin();
if(getConfig().getTokenSessionCheckLogin()) {
checkLogin();
}
return getTokenSessionByToken(getTokenValue());
}
@ -458,11 +460,11 @@ public class StpLogic {
*/
protected void setLastActivityToNow(String tokenValue) {
// 如果token == null 或者 设置了[永不过期], 则立即返回
if(tokenValue == null || SaTokenManager.getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
if(tokenValue == null || getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
return;
}
// [最后操作时间]标记为当前时间戳
SaTokenManager.getSaTokenDao().setValue(getKeyLastActivityTime(tokenValue), String.valueOf(System.currentTimeMillis()), SaTokenManager.getConfig().getTimeout());
SaTokenManager.getSaTokenDao().setValue(getKeyLastActivityTime(tokenValue), String.valueOf(System.currentTimeMillis()), getConfig().getTimeout());
}
/**
@ -471,7 +473,7 @@ public class StpLogic {
*/
protected void clearLastActivity(String tokenValue) {
// 如果token == null 或者 设置了[永不过期], 则立即返回
if(tokenValue == null || SaTokenManager.getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
if(tokenValue == null || getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
return;
}
// 删除[最后操作时间]
@ -486,7 +488,7 @@ public class StpLogic {
*/
public void checkActivityTimeout(String tokenValue) {
// 如果token == null 或者 设置了[永不过期], 则立即返回
if(tokenValue == null || SaTokenManager.getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
if(tokenValue == null || getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
return;
}
// 如果本次请求已经有了[检查标记], 则立即返回
@ -524,7 +526,7 @@ public class StpLogic {
*/
public void updateLastActivityToNow(String tokenValue) {
// 如果token == null 或者 设置了[永不过期], 则立即返回
if(tokenValue == null || SaTokenManager.getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
if(tokenValue == null || getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
return;
}
SaTokenManager.getSaTokenDao().updateValue(getKeyLastActivityTime(tokenValue), String.valueOf(System.currentTimeMillis()));
@ -614,7 +616,7 @@ public class StpLogic {
return SaTokenDao.NOT_VALUE_EXPIRE;
}
// 如果设置了永不过期, 则返回 -1
if(SaTokenManager.getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
if(getConfig().getActivityTimeout() == SaTokenDao.NEVER_EXPIRE) {
return SaTokenDao.NEVER_EXPIRE;
}
// ------ 开始查询
@ -628,7 +630,7 @@ public class StpLogic {
// 计算相差时间
long lastActivityTime = Long.valueOf(lastActivityTimeString);
long apartSecond = (System.currentTimeMillis() - lastActivityTime) / 1000;
long timeout = SaTokenManager.getConfig().getActivityTimeout() - apartSecond;
long timeout = getConfig().getActivityTimeout() - apartSecond;
// 如果 < 0 代表已经过期 返回-2
if(timeout < 0) {
return SaTokenDao.NOT_VALUE_EXPIRE;
@ -709,7 +711,7 @@ public class StpLogic {
* @return key
*/
public String getKeyTokenName() {
return SaTokenManager.getConfig().getTokenName();
return getConfig().getTokenName();
}
/**
* 获取key tokenValue 持久化
@ -717,7 +719,7 @@ public class StpLogic {
* @return key
*/
public String getKeyTokenValue(String tokenValue) {
return SaTokenManager.getConfig().getTokenName() + ":" + loginKey + ":token:" + tokenValue;
return getConfig().getTokenName() + ":" + loginKey + ":token:" + tokenValue;
}
/**
* 获取key id 持久化
@ -725,7 +727,7 @@ public class StpLogic {
* @return key
*/
public String getKeyLoginId(Object loginId) {
return SaTokenManager.getConfig().getTokenName() + ":" + loginKey + ":id:" + loginId;
return getConfig().getTokenName() + ":" + loginKey + ":id:" + loginId;
}
/**
* 获取key session 持久化
@ -733,7 +735,7 @@ public class StpLogic {
* @return key
*/
public String getKeySession(Object loginId) {
return SaTokenManager.getConfig().getTokenName() + ":" + loginKey + ":session:" + loginId;
return getConfig().getTokenName() + ":" + loginKey + ":session:" + loginId;
}
/**
* 获取key tokenValue的专属session
@ -741,7 +743,7 @@ public class StpLogic {
* @return key
*/
public String getKeyTokenSession(String tokenValue) {
return SaTokenManager.getConfig().getTokenName() + ":" + loginKey + ":token-session:" + tokenValue;
return getConfig().getTokenName() + ":" + loginKey + ":token-session:" + tokenValue;
}
/**
* 获取key 指定token的最后操作时间 持久化
@ -749,8 +751,16 @@ public class StpLogic {
* @return key
*/
public String getKeyLastActivityTime(String tokenValue) {
return SaTokenManager.getConfig().getTokenName() + ":" + loginKey + ":last-activity:" + tokenValue;
return getConfig().getTokenName() + ":" + loginKey + ":last-activity:" + tokenValue;
}
/**
* 返回配置对象
*/
public SaTokenConfig getConfig() {
// 为什么再代理一层? 为某些极端业务场景下[需要不同StpLogic不同配置]提供便利
return SaTokenManager.getConfig();
}
}

View File

@ -67,15 +67,16 @@ spring:
---
### 所有可配置项
| 参数名称 | 类型 | 默认值 | 说明 |
| :-------- | :-------- | :-------- | :-------- |
| tokenName | String | satoken | token名称同时也是cookie名称 |
| timeout | long | 2592000 | token有效期单位/秒 默认30天-1代表永久有效 [参考token有效期详解](/fun/token-timeout) |
| activityTimeout | long | -1 | token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒, 默认-1 代表不限制 (例如可以设置为1800代表30分钟内无操作就过期) [参考token有效期详解](/fun/token-timeout) |
| isShare | Boolean | true | 在多人登录同一账号时是否共享会话为true时共用一个为false时新登录挤掉旧登录 |
| isReadBody | Boolean | true | 是否尝试从请求体里读取token |
| isReadHead | Boolean | true | 是否尝试从header里读取token |
| isReadCookie | Boolean | true | 是否尝试从cookie里读取token |
| tokenStyle | String | uuid | token风格, [参考花式token](/use/token-style) |
| dataRefreshPeriod | int | 30 | 默认dao层实现类中每次清理过期数据间隔的时间 (单位: 秒) 默认值30秒设置为-1代表不启动定时清理 |
| isV | Boolean | true | 是否在初始化配置时打印版本字符画 |
| 参数名称 | 类型 | 默认值 | 说明 |
| :-------- | :-------- | :-------- | :-------- |
| tokenName | String | satoken | token名称同时也是cookie名称 |
| timeout | long | 2592000 | token有效期单位/秒 默认30天-1代表永久有效 [参考token有效期详解](/fun/token-timeout) |
| activityTimeout | long | -1 | token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒, 默认-1 代表不限制 (例如可以设置为1800代表30分钟内无操作就过期) [参考token有效期详解](/fun/token-timeout) |
| isShare | Boolean | true | 在多人登录同一账号时是否共享会话为true时共用一个为false时新登录挤掉旧登录 |
| isReadBody | Boolean | true | 是否尝试从请求体里读取token |
| isReadHead | Boolean | true | 是否尝试从header里读取token |
| isReadCookie | Boolean | true | 是否尝试从cookie里读取token |
| tokenStyle | String | uuid | token风格, [参考花式token](/use/token-style) |
| dataRefreshPeriod | int | 30 | 默认dao层实现类中每次清理过期数据间隔的时间 (单位: 秒) 默认值30秒设置为-1代表不启动定时清理 |
| tokenSessionCheckLogin | Boolean | 30 | 获取token专属session时是否必须登录 |
| isV | Boolean | true | 是否在初始化配置时打印版本字符画 |