Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
shengzhang_
sa-token
提交
8c35d838
sa-token
项目概览
shengzhang_
/
sa-token
通知
68
Star
16
Fork
4
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
sa-token
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
8c35d838
编写于
3月 05, 2021
作者:
shengzhang_
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
`SaSession`新增`refreshDataMap`方法,用于一次性重置整个数据集合
上级
1eae6861
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
52 addition
and
42 deletion
+52
-42
sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java
...ore/src/main/java/cn/dev33/satoken/session/SaSession.java
+10
-0
sa-token-core/src/main/java/cn/dev33/satoken/session/SaSessionCustomUtil.java
...in/java/cn/dev33/satoken/session/SaSessionCustomUtil.java
+4
-4
sa-token-core/src/main/java/cn/dev33/satoken/stp/SaLoginModel.java
...core/src/main/java/cn/dev33/satoken/stp/SaLoginModel.java
+1
-1
sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java
...ken-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java
+36
-36
sa-token-demo-jwt/src/main/java/com/pj/satoken/jwt/SaTokenJwtUtil.java
...-jwt/src/main/java/com/pj/satoken/jwt/SaTokenJwtUtil.java
+1
-1
未找到文件。
sa-token-core/src/main/java/cn/dev33/satoken/session/SaSession.java
浏览文件 @
8c35d838
...
...
@@ -226,6 +226,16 @@ public class SaSession implements Serializable {
return
dataMap
;
}
/**
* 写入数据集合 (不改变底层对象,只将此dataMap所有数据进行替换)
* @param dataMap 数据集合
*/
public
void
refreshDataMap
(
Map
<
String
,
Object
>
dataMap
)
{
this
.
dataMap
.
clear
();
this
.
dataMap
.
putAll
(
dataMap
);
this
.
update
();
}
// ----------------------- 一些操作
/**
...
...
sa-token-core/src/main/java/cn/dev33/satoken/session/SaSessionCustomUtil.java
浏览文件 @
8c35d838
...
...
@@ -21,7 +21,7 @@ public class SaSessionCustomUtil {
* @param sessionId 会话id
* @return sessionId
*/
public
static
String
get
SessionKey
(
String
sessionId
)
{
public
static
String
splicing
SessionKey
(
String
sessionId
)
{
return
SaTokenManager
.
getConfig
().
getTokenName
()
+
":"
+
sessionKey
+
":session:"
+
sessionId
;
}
...
...
@@ -32,7 +32,7 @@ public class SaSessionCustomUtil {
* @return 是否存在
*/
public
boolean
isExists
(
String
sessionId
)
{
return
SaTokenManager
.
getSaTokenDao
().
getSession
(
get
SessionKey
(
sessionId
))
!=
null
;
return
SaTokenManager
.
getSaTokenDao
().
getSession
(
splicing
SessionKey
(
sessionId
))
!=
null
;
}
/**
...
...
@@ -43,7 +43,7 @@ public class SaSessionCustomUtil {
* @return SaSession
*/
public
static
SaSession
getSessionById
(
String
sessionId
,
boolean
isCreate
)
{
SaSession
session
=
SaTokenManager
.
getSaTokenDao
().
getSession
(
get
SessionKey
(
sessionId
));
SaSession
session
=
SaTokenManager
.
getSaTokenDao
().
getSession
(
splicing
SessionKey
(
sessionId
));
if
(
session
==
null
&&
isCreate
)
{
session
=
SaTokenManager
.
getSaTokenAction
().
createSession
(
sessionId
);
SaTokenManager
.
getSaTokenDao
().
setSession
(
session
,
SaTokenManager
.
getConfig
().
getTimeout
());
...
...
@@ -67,7 +67,7 @@ public class SaSessionCustomUtil {
* @param sessionId 指定key
*/
public
static
void
deleteSessionById
(
String
sessionId
)
{
SaTokenManager
.
getSaTokenDao
().
deleteSession
(
get
SessionKey
(
sessionId
));
SaTokenManager
.
getSaTokenDao
().
deleteSession
(
splicing
SessionKey
(
sessionId
));
}
}
sa-token-core/src/main/java/cn/dev33/satoken/stp/SaLoginModel.java
浏览文件 @
8c35d838
...
...
@@ -5,7 +5,7 @@ import cn.dev33.satoken.config.SaTokenConfig;
import
cn.dev33.satoken.util.SaTokenConsts
;
/**
* 调用
StpUtil.setLogin()时的配置 Model
* 调用
`StpUtil.setLogin()` 时的 [配置参数 Model ]
* @author kong
*
*/
...
...
sa-token-core/src/main/java/cn/dev33/satoken/stp/StpLogic.java
浏览文件 @
8c35d838
...
...
@@ -72,7 +72,7 @@ public class StpLogic {
* @return 此StpLogic的token名称
*/
public
String
getTokenName
()
{
return
get
KeyTokenName
();
return
splicing
KeyTokenName
();
}
/**
...
...
@@ -96,8 +96,8 @@ public class StpLogic {
String
tokenValue
=
null
;
// 1. 尝试从request里读取
if
(
request
.
getAttribute
(
get
KeyJustCreatedSave
())
!=
null
)
{
tokenValue
=
String
.
valueOf
(
request
.
getAttribute
(
get
KeyJustCreatedSave
()));
if
(
request
.
getAttribute
(
splicing
KeyJustCreatedSave
())
!=
null
)
{
tokenValue
=
String
.
valueOf
(
request
.
getAttribute
(
splicing
KeyJustCreatedSave
()));
}
// 2. 尝试从请求体里面读取
if
(
tokenValue
==
null
&&
config
.
getIsReadBody
()){
...
...
@@ -197,7 +197,7 @@ public class StpLogic {
for
(
TokenSign
tokenSign
:
tokenSignList
)
{
if
(
tokenSign
.
getDevice
().
equals
(
loginModel
.
getDevice
()))
{
// 1. 将此token 标记为已顶替
dao
.
update
(
get
KeyTokenValue
(
tokenSign
.
getValue
()),
NotLoginException
.
BE_REPLACED
);
dao
.
update
(
splicing
KeyTokenValue
(
tokenSign
.
getValue
()),
NotLoginException
.
BE_REPLACED
);
// 2. 清理掉[token-最后操作时间]
clearLastActivity
(
tokenSign
.
getValue
());
// 3. 清理账号session上的token签名记录
...
...
@@ -226,9 +226,9 @@ public class StpLogic {
// ------ 4. 持久化其它数据
// token -> uid
dao
.
set
(
get
KeyTokenValue
(
tokenValue
),
String
.
valueOf
(
loginId
),
loginModel
.
getTimeout
());
dao
.
set
(
splicing
KeyTokenValue
(
tokenValue
),
String
.
valueOf
(
loginId
),
loginModel
.
getTimeout
());
// 将token保存到本次request里
request
.
setAttribute
(
get
KeyJustCreatedSave
(),
tokenValue
);
request
.
setAttribute
(
splicing
KeyJustCreatedSave
(),
tokenValue
);
// 写入 [最后操作时间]
setLastActivityToNow
(
tokenValue
);
// 注入Cookie
...
...
@@ -269,7 +269,7 @@ public class StpLogic {
if
(
loginId
==
null
||
NotLoginException
.
ABNORMAL_LIST
.
contains
(
loginId
))
{
return
;
}
SaTokenManager
.
getSaTokenDao
().
delete
(
get
KeyTokenValue
(
tokenValue
));
SaTokenManager
.
getSaTokenDao
().
delete
(
splicing
KeyTokenValue
(
tokenValue
));
// 3. 尝试清理账号session上的token签名 (如果为null或已被标记为异常, 那么无需继续执行 )
SaSession
session
=
getSessionByLoginId
(
loginId
,
false
);
...
...
@@ -313,7 +313,7 @@ public class StpLogic {
// 2. 清理掉[token-最后操作时间]
clearLastActivity
(
tokenValue
);
// 3. 标记:已被踢下线
SaTokenManager
.
getSaTokenDao
().
update
(
get
KeyTokenValue
(
tokenValue
),
NotLoginException
.
KICK_OUT
);
SaTokenManager
.
getSaTokenDao
().
update
(
splicing
KeyTokenValue
(
tokenValue
),
NotLoginException
.
KICK_OUT
);
// 4. 清理账号session上的token签名
session
.
removeTokenSign
(
tokenValue
);
}
...
...
@@ -475,7 +475,7 @@ public class StpLogic {
* @return loginId
*/
public
String
getLoginIdNotHandle
(
String
tokenValue
)
{
return
SaTokenManager
.
getSaTokenDao
().
get
(
get
KeyTokenValue
(
tokenValue
));
return
SaTokenManager
.
getSaTokenDao
().
get
(
splicing
KeyTokenValue
(
tokenValue
));
}
...
...
@@ -512,7 +512,7 @@ public class StpLogic {
* @return SaSession
*/
public
SaSession
getSessionByLoginId
(
Object
loginId
,
boolean
isCreate
)
{
return
getSessionBySessionId
(
get
KeySession
(
loginId
),
isCreate
);
return
getSessionBySessionId
(
splicing
KeySession
(
loginId
),
isCreate
);
}
/**
...
...
@@ -551,7 +551,7 @@ public class StpLogic {
* @return session会话
*/
public
SaSession
getTokenSessionByToken
(
String
tokenValue
,
boolean
isCreate
)
{
return
getSessionBySessionId
(
get
KeyTokenSession
(
tokenValue
),
isCreate
);
return
getSessionBySessionId
(
splicing
KeyTokenSession
(
tokenValue
),
isCreate
);
}
/**
...
...
@@ -560,7 +560,7 @@ public class StpLogic {
* @return session会话
*/
public
SaSession
getTokenSessionByToken
(
String
tokenValue
)
{
return
getSessionBySessionId
(
get
KeyTokenSession
(
tokenValue
),
true
);
return
getSessionBySessionId
(
splicing
KeyTokenSession
(
tokenValue
),
true
);
}
/**
...
...
@@ -579,7 +579,7 @@ public class StpLogic {
// 随机一个token送给Ta
tokenValue
=
createTokenValue
(
null
);
// Request做上标记
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
setAttribute
(
get
KeyJustCreatedSave
(),
tokenValue
);
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
setAttribute
(
splicing
KeyJustCreatedSave
(),
tokenValue
);
// 写入 [最后操作时间]
setLastActivityToNow
(
tokenValue
);
// cookie注入
...
...
@@ -590,7 +590,7 @@ public class StpLogic {
}
}
// 返回这个token对应的专属session
return
getSessionBySessionId
(
get
KeyTokenSession
(
getTokenValue
()),
isCreate
);
return
getSessionBySessionId
(
splicing
KeyTokenSession
(
getTokenValue
()),
isCreate
);
}
/**
...
...
@@ -614,7 +614,7 @@ public class StpLogic {
return
;
}
// 将[最后操作时间]标记为当前时间戳
SaTokenManager
.
getSaTokenDao
().
set
(
get
KeyLastActivityTime
(
tokenValue
),
String
.
valueOf
(
System
.
currentTimeMillis
()),
getConfig
().
getTimeout
());
SaTokenManager
.
getSaTokenDao
().
set
(
splicing
KeyLastActivityTime
(
tokenValue
),
String
.
valueOf
(
System
.
currentTimeMillis
()),
getConfig
().
getTimeout
());
}
/**
...
...
@@ -627,7 +627,7 @@ public class StpLogic {
return
;
}
// 删除[最后操作时间]
SaTokenManager
.
getSaTokenDao
().
delete
(
get
KeyLastActivityTime
(
tokenValue
));
SaTokenManager
.
getSaTokenDao
().
delete
(
splicing
KeyLastActivityTime
(
tokenValue
));
// 清除标记
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
removeAttribute
(
SaTokenConsts
.
TOKEN_ACTIVITY_TIMEOUT_CHECKED_KEY
);
}
...
...
@@ -679,7 +679,7 @@ public class StpLogic {
if
(
tokenValue
==
null
||
getConfig
().
getActivityTimeout
()
==
SaTokenDao
.
NEVER_EXPIRE
)
{
return
;
}
SaTokenManager
.
getSaTokenDao
().
update
(
get
KeyLastActivityTime
(
tokenValue
),
String
.
valueOf
(
System
.
currentTimeMillis
()));
SaTokenManager
.
getSaTokenDao
().
update
(
splicing
KeyLastActivityTime
(
tokenValue
),
String
.
valueOf
(
System
.
currentTimeMillis
()));
}
/**
...
...
@@ -699,7 +699,7 @@ public class StpLogic {
* @return token剩余有效时间
*/
public
long
getTokenTimeout
()
{
return
SaTokenManager
.
getSaTokenDao
().
getTimeout
(
get
KeyTokenValue
(
getTokenValue
()));
return
SaTokenManager
.
getSaTokenDao
().
getTimeout
(
splicing
KeyTokenValue
(
getTokenValue
()));
}
/**
...
...
@@ -708,7 +708,7 @@ public class StpLogic {
* @return token剩余有效时间
*/
public
long
getTokenTimeoutByLoginId
(
Object
loginId
)
{
return
SaTokenManager
.
getSaTokenDao
().
getTimeout
(
get
KeyTokenValue
(
getTokenValueByLoginId
(
loginId
)));
return
SaTokenManager
.
getSaTokenDao
().
getTimeout
(
splicing
KeyTokenValue
(
getTokenValueByLoginId
(
loginId
)));
}
/**
...
...
@@ -725,7 +725,7 @@ public class StpLogic {
* @return token剩余有效时间
*/
public
long
getSessionTimeoutByLoginId
(
Object
loginId
)
{
return
SaTokenManager
.
getSaTokenDao
().
getSessionTimeout
(
get
KeySession
(
loginId
));
return
SaTokenManager
.
getSaTokenDao
().
getSessionTimeout
(
splicing
KeySession
(
loginId
));
}
/**
...
...
@@ -742,7 +742,7 @@ public class StpLogic {
* @return token剩余有效时间
*/
public
long
getTokenSessionTimeoutByTokenValue
(
String
tokenValue
)
{
return
SaTokenManager
.
getSaTokenDao
().
getSessionTimeout
(
get
KeyTokenSession
(
tokenValue
));
return
SaTokenManager
.
getSaTokenDao
().
getSessionTimeout
(
splicing
KeyTokenSession
(
tokenValue
));
}
/**
...
...
@@ -769,7 +769,7 @@ public class StpLogic {
}
// ------ 开始查询
// 获取相关数据
String
keyLastActivityTime
=
get
KeyLastActivityTime
(
tokenValue
);
String
keyLastActivityTime
=
splicing
KeyLastActivityTime
(
tokenValue
);
String
lastActivityTimeString
=
SaTokenManager
.
getSaTokenDao
().
get
(
keyLastActivityTime
);
// 查不到,返回-2
if
(
lastActivityTimeString
==
null
)
{
...
...
@@ -1015,7 +1015,7 @@ public class StpLogic {
* @return token集合
*/
public
List
<
String
>
searchTokenValue
(
String
keyword
,
int
start
,
int
size
)
{
return
SaTokenManager
.
getSaTokenDao
().
searchData
(
get
KeyTokenValue
(
""
),
keyword
,
start
,
size
);
return
SaTokenManager
.
getSaTokenDao
().
searchData
(
splicing
KeyTokenValue
(
""
),
keyword
,
start
,
size
);
}
/**
...
...
@@ -1026,7 +1026,7 @@ public class StpLogic {
* @return sessionId集合
*/
public
List
<
String
>
searchSessionId
(
String
keyword
,
int
start
,
int
size
)
{
return
SaTokenManager
.
getSaTokenDao
().
searchData
(
get
KeySession
(
""
),
keyword
,
start
,
size
);
return
SaTokenManager
.
getSaTokenDao
().
searchData
(
splicing
KeySession
(
""
),
keyword
,
start
,
size
);
}
/**
...
...
@@ -1037,7 +1037,7 @@ public class StpLogic {
* @return sessionId集合
*/
public
List
<
String
>
searchTokenSessionId
(
String
keyword
,
int
start
,
int
size
)
{
return
SaTokenManager
.
getSaTokenDao
().
searchData
(
get
KeyTokenSession
(
""
),
keyword
,
start
,
size
);
return
SaTokenManager
.
getSaTokenDao
().
searchData
(
splicing
KeyTokenSession
(
""
),
keyword
,
start
,
size
);
}
...
...
@@ -1047,7 +1047,7 @@ public class StpLogic {
* 获取key:客户端 tokenName
* @return key
*/
public
String
get
KeyTokenName
()
{
public
String
splicing
KeyTokenName
()
{
return
getConfig
().
getTokenName
();
}
...
...
@@ -1056,7 +1056,7 @@ public class StpLogic {
* @param tokenValue token值
* @return key
*/
public
String
get
KeyTokenValue
(
String
tokenValue
)
{
public
String
splicing
KeyTokenValue
(
String
tokenValue
)
{
return
getConfig
().
getTokenName
()
+
":"
+
loginKey
+
":token:"
+
tokenValue
;
}
...
...
@@ -1065,7 +1065,7 @@ public class StpLogic {
* @param loginId 账号id
* @return key
*/
public
String
get
KeySession
(
Object
loginId
)
{
public
String
splicing
KeySession
(
Object
loginId
)
{
return
getConfig
().
getTokenName
()
+
":"
+
loginKey
+
":session:"
+
loginId
;
}
...
...
@@ -1074,7 +1074,7 @@ public class StpLogic {
* @param tokenValue token值
* @return key
*/
public
String
get
KeyTokenSession
(
String
tokenValue
)
{
public
String
splicing
KeyTokenSession
(
String
tokenValue
)
{
return
getConfig
().
getTokenName
()
+
":"
+
loginKey
+
":token-session:"
+
tokenValue
;
}
...
...
@@ -1083,7 +1083,7 @@ public class StpLogic {
* @param tokenValue token值
* @return key
*/
public
String
get
KeyLastActivityTime
(
String
tokenValue
)
{
public
String
splicing
KeyLastActivityTime
(
String
tokenValue
)
{
return
getConfig
().
getTokenName
()
+
":"
+
loginKey
+
":last-activity:"
+
tokenValue
;
}
...
...
@@ -1091,7 +1091,7 @@ public class StpLogic {
* 在进行身份切换时,使用的存储key
* @return key
*/
public
String
get
KeySwitch
()
{
public
String
splicing
KeySwitch
()
{
return
SaTokenConsts
.
SWITCH_TO_SAVE_KEY
+
getLoginKey
();
}
...
...
@@ -1099,7 +1099,7 @@ public class StpLogic {
* 如果token为本次请求新创建的,则以此字符串为key存储在当前request中
* @return key
*/
public
String
get
KeyJustCreatedSave
()
{
public
String
splicing
KeyJustCreatedSave
()
{
return
SaTokenConsts
.
JUST_CREATED_SAVE_KEY
+
getLoginKey
();
}
...
...
@@ -1184,14 +1184,14 @@ public class StpLogic {
* @param loginId 指定loginId
*/
public
void
switchTo
(
Object
loginId
)
{
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
setAttribute
(
get
KeySwitch
(),
loginId
);
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
setAttribute
(
splicing
KeySwitch
(),
loginId
);
}
/**
* 结束临时切换身份
*/
public
void
endSwitch
()
{
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
removeAttribute
(
get
KeySwitch
());
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
removeAttribute
(
splicing
KeySwitch
());
}
/**
...
...
@@ -1199,7 +1199,7 @@ public class StpLogic {
* @return 是否正处于[身份临时切换]中
*/
public
boolean
isSwitch
()
{
return
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
getAttribute
(
get
KeySwitch
())
!=
null
;
return
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
getAttribute
(
splicing
KeySwitch
())
!=
null
;
}
/**
...
...
@@ -1207,7 +1207,7 @@ public class StpLogic {
* @return 返回[身份临时切换]的loginId
*/
public
Object
getSwitchLoginId
()
{
return
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
getAttribute
(
get
KeySwitch
());
return
SaTokenManager
.
getSaTokenServlet
().
getRequest
().
getAttribute
(
splicing
KeySwitch
());
}
/**
...
...
sa-token-demo-jwt/src/main/java/com/pj/satoken/jwt/SaTokenJwtUtil.java
浏览文件 @
8c35d838
...
...
@@ -128,7 +128,7 @@ public class SaTokenJwtUtil {
SaTokenConfig
config
=
getConfig
();
// ------ 2、生成一个token
String
tokenValue
=
createTokenValue
(
loginId
);
request
.
setAttribute
(
get
KeyJustCreatedSave
(),
tokenValue
);
// 将token保存到本次request里
request
.
setAttribute
(
splicing
KeyJustCreatedSave
(),
tokenValue
);
// 将token保存到本次request里
if
(
config
.
getIsReadCookie
()
==
true
){
// cookie注入
SaTokenManager
.
getSaTokenCookie
().
addCookie
(
SaTokenManager
.
getSaTokenServlet
().
getResponse
(),
getTokenName
(),
tokenValue
,
"/"
,
config
.
getCookieDomain
(),
(
int
)
config
.
getTimeout
());
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录