mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 01:58:05 +08:00
SaSession 新增字段:type、loginType、loginId、token
This commit is contained in:
@@ -29,29 +29,56 @@ public class SaSession implements SaSetValueInterface, Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/**
|
||||
* 在 Session 上存储用户对象时建议使用的key
|
||||
* 在 SaSession 上存储用户对象时建议使用的 key
|
||||
*/
|
||||
public static final String USER = "USER";
|
||||
|
||||
/**
|
||||
* 在 Session 上存储角色时建议使用的key
|
||||
* 在 SaSession 上存储角色列表时建议使用的 key
|
||||
*/
|
||||
public static final String ROLE_LIST = "ROLE_LIST";
|
||||
|
||||
/**
|
||||
* 在 Session 上存储权限时建议使用的key
|
||||
* 在 SaSession 上存储权限列表时建议使用的 key
|
||||
*/
|
||||
public static final String PERMISSION_LIST = "PERMISSION_LIST";
|
||||
|
||||
/** 此 Session 的 id */
|
||||
/**
|
||||
* 此 SaSession 的 id
|
||||
*/
|
||||
private String id;
|
||||
|
||||
/** 此 Session 的创建时间(时间戳) */
|
||||
/**
|
||||
* 此 SaSession 的 类型
|
||||
*/
|
||||
private String type;
|
||||
|
||||
/**
|
||||
* 所属 loginType
|
||||
*/
|
||||
private String loginType;
|
||||
|
||||
/**
|
||||
* 所属 loginId (当此 SaSession 属于 Account-Session 时,此值有效)
|
||||
*/
|
||||
private Object loginId;
|
||||
|
||||
/**
|
||||
* 所属 Token (当此 SaSession 属于 Token-Session 时,此值有效)
|
||||
*/
|
||||
private String token;
|
||||
|
||||
/**
|
||||
* 此 SaSession 的创建时间(13位时间戳)
|
||||
*/
|
||||
private long createTime;
|
||||
|
||||
/** 此 Session 的所有挂载数据 */
|
||||
/**
|
||||
* 所有挂载数据
|
||||
*/
|
||||
private final Map<String, Object> dataMap = new ConcurrentHashMap<>();
|
||||
|
||||
|
||||
// ----------------------- 构建相关
|
||||
|
||||
/**
|
||||
@@ -77,16 +104,16 @@ public class SaSession implements SaSetValueInterface, Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取此 Session 的 id
|
||||
* @return 此 Session 的id
|
||||
* 获取:此 SaSession 的 id
|
||||
* @return /
|
||||
*/
|
||||
public String getId() {
|
||||
return id;
|
||||
return this.id;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入此 Session 的 id
|
||||
* @param id SessionId
|
||||
* 写入:此 SaSession 的 id
|
||||
* @param id /
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaSession setId(String id) {
|
||||
@@ -95,16 +122,90 @@ public class SaSession implements SaSetValueInterface, Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回当前会话创建时间(时间戳)
|
||||
* @return 时间戳
|
||||
* 获取:此 SaSession 的 类型
|
||||
*
|
||||
* @return /
|
||||
*/
|
||||
public long getCreateTime() {
|
||||
return createTime;
|
||||
public String getType() {
|
||||
return this.type;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入此 Session 的创建时间(时间戳)
|
||||
* @param createTime 时间戳
|
||||
* 设置:此 SaSession 的 类型
|
||||
*
|
||||
* @param type /
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaSession setType(String type) {
|
||||
this.type = type;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取:所属 loginType
|
||||
* @return /
|
||||
*/
|
||||
public String getLoginType() {
|
||||
return this.loginType;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置:所属 loginType
|
||||
* @param loginType /
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaSession setLoginType(String loginType) {
|
||||
this.loginType = loginType;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取:所属 loginId (当此 SaSession 属于 Account-Session 时,此值有效)
|
||||
* @return /
|
||||
*/
|
||||
public Object getLoginId() {
|
||||
return this.loginId;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置:所属 loginId (当此 SaSession 属于 Account-Session 时,此值有效)
|
||||
* @param loginId /
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaSession setLoginId(Object loginId) {
|
||||
this.loginId = loginId;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取:所属 Token (当此 SaSession 属于 Token-Session 时,此值有效)
|
||||
* @return /
|
||||
*/
|
||||
public String getToken() {
|
||||
return this.token;
|
||||
}
|
||||
|
||||
/**
|
||||
* 设置:所属 Token (当此 SaSession 属于 Token-Session 时,此值有效)
|
||||
* @param token /
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaSession setToken(String token) {
|
||||
this.token = token;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* 返回:当前 SaSession 的创建时间(13位时间戳)
|
||||
* @return /
|
||||
*/
|
||||
public long getCreateTime() {
|
||||
return this.createTime;
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入:此 SaSession 的创建时间(13位时间戳)
|
||||
* @param createTime /
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaSession setCreateTime(long createTime) {
|
||||
@@ -292,6 +393,7 @@ public class SaSession implements SaSetValueInterface, Serializable {
|
||||
return value == SaTokenDao.NEVER_EXPIRE ? Long.MAX_VALUE : value;
|
||||
}
|
||||
|
||||
|
||||
// ----------------------- 存取值 (类型转换)
|
||||
|
||||
// ---- 重写接口方法
|
||||
@@ -346,19 +448,20 @@ public class SaSession implements SaSetValueInterface, Serializable {
|
||||
return this;
|
||||
}
|
||||
|
||||
// ---- 其它方法
|
||||
|
||||
// ----------------------- 其它方法
|
||||
|
||||
/**
|
||||
* 返回当前Session的所有key
|
||||
* 返回当前 Session 挂载数据的所有 key
|
||||
*
|
||||
* @return 所有值的key列表
|
||||
* @return key 列表
|
||||
*/
|
||||
public Set<String> keys() {
|
||||
return dataMap.keySet();
|
||||
}
|
||||
|
||||
/**
|
||||
* 清空所有值
|
||||
* 清空所有挂载数据
|
||||
*/
|
||||
public void clear() {
|
||||
dataMap.clear();
|
||||
@@ -366,7 +469,7 @@ public class SaSession implements SaSetValueInterface, Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据挂载集合(如果更新map里的值,请调用session.update()方法避免产生脏数据 )
|
||||
* 获取数据挂载集合(如果更新map里的值,请调用 session.update() 方法避免产生脏数据 )
|
||||
*
|
||||
* @return 返回底层储存值的map对象
|
||||
*/
|
||||
@@ -375,7 +478,7 @@ public class SaSession implements SaSetValueInterface, Serializable {
|
||||
}
|
||||
|
||||
/**
|
||||
* 写入数据集合 (不改变底层对象,只将此dataMap所有数据进行替换)
|
||||
* 写入数据集合 (不改变底层对象引用,只将此 dataMap 所有数据进行替换)
|
||||
* @param dataMap 数据集合
|
||||
*/
|
||||
public void refreshDataMap(Map<String, Object> dataMap) {
|
||||
|
@@ -4,6 +4,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import cn.dev33.satoken.annotation.SaCheckDisable;
|
||||
@@ -867,12 +868,24 @@ public class StpLogic {
|
||||
* 获取指定key的Session, 如果Session尚未创建,isCreate=是否新建并返回
|
||||
* @param sessionId SessionId
|
||||
* @param isCreate 是否新建
|
||||
* @param appendOperation 如果这个 SaSession 是新建的,则要追加执行的动作
|
||||
* @return Session对象
|
||||
*/
|
||||
public SaSession getSessionBySessionId(String sessionId, boolean isCreate) {
|
||||
public SaSession getSessionBySessionId(String sessionId, boolean isCreate, Consumer<SaSession> appendOperation) {
|
||||
|
||||
// 先检查这个 SaSession 是否已经存在,如果不存在且 isCreate=true,则新建并返回
|
||||
SaSession session = getSaTokenDao().getSession(sessionId);
|
||||
|
||||
if(session == null && isCreate) {
|
||||
// 创建这个 SaSession
|
||||
session = SaStrategy.me.createSession.apply(sessionId);
|
||||
|
||||
// 追加操作
|
||||
if(appendOperation != null) {
|
||||
appendOperation.accept(session);
|
||||
}
|
||||
|
||||
// 将这个 SaSession 入库
|
||||
getSaTokenDao().setSession(session, getConfig().getTimeout());
|
||||
}
|
||||
return session;
|
||||
@@ -884,7 +897,7 @@ public class StpLogic {
|
||||
* @return Session对象
|
||||
*/
|
||||
public SaSession getSessionBySessionId(String sessionId) {
|
||||
return getSessionBySessionId(sessionId, false);
|
||||
return getSessionBySessionId(sessionId, false, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -894,7 +907,12 @@ public class StpLogic {
|
||||
* @return Session对象
|
||||
*/
|
||||
public SaSession getSessionByLoginId(Object loginId, boolean isCreate) {
|
||||
return getSessionBySessionId(splicingKeySession(loginId), isCreate);
|
||||
return getSessionBySessionId(splicingKeySession(loginId), isCreate, session -> {
|
||||
// 设定这个 SaSession 的各种基础信息:类型、账号体系、账号id
|
||||
session.setType(SaTokenConsts.SESSION_TYPE__ACCOUNT);
|
||||
session.setLoginType(getLoginType());
|
||||
session.setLoginId(loginId);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -903,7 +921,7 @@ public class StpLogic {
|
||||
* @return Session对象
|
||||
*/
|
||||
public SaSession getSessionByLoginId(Object loginId) {
|
||||
return getSessionBySessionId(splicingKeySession(loginId), true);
|
||||
return getSessionByLoginId(loginId, true);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -933,7 +951,12 @@ public class StpLogic {
|
||||
* @return session对象
|
||||
*/
|
||||
public SaSession getTokenSessionByToken(String tokenValue, boolean isCreate) {
|
||||
return getSessionBySessionId(splicingKeyTokenSession(tokenValue), isCreate);
|
||||
return getSessionBySessionId(splicingKeyTokenSession(tokenValue), isCreate, session -> {
|
||||
// 设定这个 SaSession 的各种基础信息:类型、账号体系、Token 值
|
||||
session.setType(SaTokenConsts.SESSION_TYPE__TOKEN);
|
||||
session.setLoginType(getLoginType());
|
||||
session.setToken(tokenValue);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -942,7 +965,7 @@ public class StpLogic {
|
||||
* @return Session对象
|
||||
*/
|
||||
public SaSession getTokenSessionByToken(String tokenValue) {
|
||||
return getSessionBySessionId(splicingKeyTokenSession(tokenValue), true);
|
||||
return getTokenSessionByToken(tokenValue, true);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -10,7 +10,7 @@ public class SaTokenConsts {
|
||||
private SaTokenConsts() {
|
||||
}
|
||||
|
||||
// =================== sa-token版本信息 ===================
|
||||
// ------------------ Sa-Token 版本信息
|
||||
|
||||
/**
|
||||
* Sa-Token 当前版本号
|
||||
@@ -32,7 +32,7 @@ public class SaTokenConsts {
|
||||
*/
|
||||
public static final String DEV_DOC_URL = "https://sa-token.cc";
|
||||
|
||||
// =================== 常量key标记 ===================
|
||||
// ------------------ 常量key标记
|
||||
|
||||
/**
|
||||
* 常量key标记: 如果token为本次请求新创建的,则以此字符串为key存储在当前request中
|
||||
@@ -101,7 +101,7 @@ public class SaTokenConsts {
|
||||
public static final String DEFAULT_TEMP_TOKEN_SERVICE = "record";
|
||||
|
||||
|
||||
// =================== token-style 相关 ===================
|
||||
// ------------------ token-style 相关
|
||||
|
||||
/**
|
||||
* Token风格: uuid
|
||||
@@ -134,7 +134,25 @@ public class SaTokenConsts {
|
||||
public static final String TOKEN_STYLE_TIK = "tik";
|
||||
|
||||
|
||||
// =================== 其它 ===================
|
||||
// ------------------ SaSession 的类型
|
||||
|
||||
/**
|
||||
* SaSession 的类型: Account-Session
|
||||
*/
|
||||
public static final String SESSION_TYPE__ACCOUNT = "Account-Session";
|
||||
|
||||
/**
|
||||
* SaSession 的类型: Token-Session
|
||||
*/
|
||||
public static final String SESSION_TYPE__TOKEN = "Token-Session";
|
||||
|
||||
/**
|
||||
* SaSession 的类型: Custom-Session
|
||||
*/
|
||||
public static final String SESSION_TYPE__CUSTOM = "Custom-Session";
|
||||
|
||||
|
||||
// ------------------ 其它
|
||||
|
||||
/**
|
||||
* 连接Token前缀和Token值的字符
|
||||
|
@@ -1,10 +1,9 @@
|
||||
package com.pj;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
import cn.dev33.satoken.SaManager;
|
||||
|
||||
/**
|
||||
* Sa-Token 测试
|
||||
* @author kong
|
||||
|
@@ -1,11 +1,10 @@
|
||||
package com.pj.test;
|
||||
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import cn.dev33.satoken.stp.StpUtil;
|
||||
import cn.dev33.satoken.util.SaResult;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
/**
|
||||
* 测试专用Controller
|
||||
|
Reference in New Issue
Block a user