Files
sa-token/sa-token-dev/src/main/java/cn/dev33/satoken/session/SaSessionCustomUtil.java
2020-02-06 00:52:49 +08:00

45 lines
1.4 KiB
Java
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package cn.dev33.satoken.session;
import cn.dev33.satoken.SaTokenManager;
/**
* 自定义sa-session工具类
* @author kong
*
*/
public class SaSessionCustomUtil {
// 添加上指定前缀防止恶意伪造session
public static String session_key = "custom";
public static String getSessionKey(String sessionId) {
return SaTokenManager.getConfig().getTokenName() + ":" + session_key + ":session:" + sessionId;
}
/** 指定key的session是否存在 */
public boolean isExists(String sessionId) {
return SaTokenManager.getDao().getSaSession(getSessionKey(sessionId)) != null;
}
/** 获取指定key的session, 如果没有is_create=是否新建并返回 */
public static SaSession getSessionById(String sessionId, boolean is_create) {
SaSession session = SaTokenManager.getDao().getSaSession(getSessionKey(sessionId));
if(session == null && is_create) {
session = new SaSession(getSessionKey(sessionId));
SaTokenManager.getDao().saveSaSession(session, SaTokenManager.getConfig().getTimeout());
}
return session;
}
/** 获取指定key的session, 如果没有则新建并返回 */
public static SaSession getSessionById(String sessionId) {
return getSessionById(sessionId, true);
}
/** 删除指定key的session */
public static void delSessionById(String sessionId) {
SaTokenManager.getDao().delSaSession(getSessionKey(sessionId));
}
}