提交 36afaf74 编写于 作者: shengzhang_'s avatar shengzhang_

token前缀校验改为强制模式,如果配置了前缀,则前端提交token时必须带有前缀

上级 55b9c87d
......@@ -283,6 +283,16 @@ public class SaSession implements Serializable {
SaTokenManager.getSaTokenDao().updateSessionTimeout(this.id, minTimeout);
}
}
/**
* 修改此Session的最大剩余存活时间 (只有在Session的过期时间高于指定的maxTimeout时才会进行修改)
* @param maxTimeout 过期时间 (单位: 秒)
*/
public void updateMaxTimeout(long maxTimeout) {
if(getTimeout() > maxTimeout) {
SaTokenManager.getSaTokenDao().updateSessionTimeout(this.id, maxTimeout);
}
}
......
......@@ -92,11 +92,19 @@ public class StpLogic {
* @param tokenValue token值
*/
public void setTokenValue(String tokenValue, int cookieTimeout){
SaTokenConfig config = getConfig();
// 将token保存到本次request里
HttpServletRequest request = SaTokenManager.getSaTokenServlet().getRequest();
request.setAttribute(splicingKeyJustCreatedSave(), tokenValue);
// 判断是否配置了token前缀
String tokenPrefix = config.getTokenPrefix();
if(SaTokenInsideUtil.isEmpty(tokenPrefix)) {
request.setAttribute(splicingKeyJustCreatedSave(), tokenValue);
} else {
// 如果配置了token前缀,则拼接上前缀一起写入
request.setAttribute(splicingKeyJustCreatedSave(), tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT + tokenValue);
}
// 注入Cookie
SaTokenConfig config = getConfig();
if(config.getIsReadCookie() == true){
HttpServletResponse response = SaTokenManager.getSaTokenServlet().getResponse();
SaTokenManager.getSaTokenCookie().addCookie(response, getTokenName(), tokenValue,
......@@ -138,9 +146,11 @@ public class StpLogic {
// 5. 如果打开了前缀模式
String tokenPrefix = getConfig().getTokenPrefix();
if(SaTokenInsideUtil.isEmpty(tokenPrefix) == false && SaTokenInsideUtil.isEmpty(tokenValue) == false) {
// 如果token以指定的前缀开头, 则裁剪掉它
if(tokenValue.startsWith(tokenPrefix + " ")) {
tokenValue = tokenValue.substring(tokenPrefix.length() + 1);
// 如果token以指定的前缀开头, 则裁剪掉它, 否则视为未提供token
if(tokenValue.startsWith(tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT)) {
tokenValue = tokenValue.substring(tokenPrefix.length() + SaTokenConsts.TOKEN_CONNECTOR_CHAT.length());
} else {
tokenValue = null;
}
}
......
......@@ -42,8 +42,8 @@ public class SaTokenConsts {
* 常量key标记: 在进行临时身份切换时使用的key
*/
public static final String SWITCH_TO_SAVE_KEY = "SWITCH_TO_SAVE_KEY_";
// =================== token-style 相关 ===================
/**
......@@ -75,6 +75,13 @@ public class SaTokenConsts {
* token风格: tik风格 (2_14_16)
*/
public static final String TOKEN_STYLE_RANDOM_TIK = "tik";
// =================== 其它 ===================
/**
* 连接token前缀和token值的字符
*/
public static final String TOKEN_CONNECTOR_CHAT = " ";
}
......@@ -248,7 +248,6 @@ public class TestController {
// .setIsLastingCookie(true) // 是否为持久Cookie(临时Cookie在浏览器关闭时会自动删除,持久Cookie在重新打开后依然存在)
// .setTimeout(60 * 60 * 24 * 7) // 指定此次登录token的有效期, 单位:秒 (如未指定,自动取全局配置的timeout值)
// );
StpUtil.getTokenSession();
return AjaxJson.getSuccess("访问成功");
}
......
......@@ -19,7 +19,6 @@ spring:
token-style: uuid
# redis配置
redis:
# Redis数据库索引(默认为0)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册