mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 18:22:15 +08:00
增加为 StpLogic 单独配置 SaTokenConfig 参数的能力
This commit is contained in:
@@ -101,6 +101,42 @@ public class StpLogic {
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private SaTokenConfig config;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 写入当前 StpLogic 单独使用的配置对象
|
||||||
|
*
|
||||||
|
* @param config 配置对象
|
||||||
|
* @return 对象自身
|
||||||
|
*/
|
||||||
|
public StpLogic setConfig(SaTokenConfig config) {
|
||||||
|
this.config = config;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回当前 StpLogic 使用的配置对象,如果当前 StpLogic 没有配置,则返回 null
|
||||||
|
*
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public SaTokenConfig getConfig() {
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 返回当前 StpLogic 使用的配置对象,如果当前 StpLogic 没有配置,则返回全局配置对象
|
||||||
|
*
|
||||||
|
* @return /
|
||||||
|
*/
|
||||||
|
public SaTokenConfig getConfigOrGlobal() {
|
||||||
|
SaTokenConfig cfg = getConfig();
|
||||||
|
if(cfg != null) {
|
||||||
|
return cfg;
|
||||||
|
}
|
||||||
|
return SaManager.getConfig();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
// ------------------- 获取 token 相关 -------------------
|
// ------------------- 获取 token 相关 -------------------
|
||||||
|
|
||||||
@@ -132,7 +168,7 @@ public class StpLogic {
|
|||||||
* @param tokenValue token 值
|
* @param tokenValue token 值
|
||||||
*/
|
*/
|
||||||
public void setTokenValue(String tokenValue){
|
public void setTokenValue(String tokenValue){
|
||||||
setTokenValue(tokenValue, new SaLoginModel().setTimeout(getConfig().getTimeout()));
|
setTokenValue(tokenValue, new SaLoginModel().setTimeout(getConfigOrGlobal().getTimeout()));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -162,7 +198,7 @@ public class StpLogic {
|
|||||||
setTokenValueToStorage(tokenValue);
|
setTokenValueToStorage(tokenValue);
|
||||||
|
|
||||||
// 2. 将 token 写入到当前会话的 Cookie 里
|
// 2. 将 token 写入到当前会话的 Cookie 里
|
||||||
if (getConfig().getIsReadCookie()) {
|
if (getConfigOrGlobal().getIsReadCookie()) {
|
||||||
setTokenValueToCookie(tokenValue, loginModel.getCookieTimeout());
|
setTokenValueToCookie(tokenValue, loginModel.getCookieTimeout());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -184,7 +220,7 @@ public class StpLogic {
|
|||||||
// 2、保存 token
|
// 2、保存 token
|
||||||
// - 如果没有配置前缀模式,直接保存
|
// - 如果没有配置前缀模式,直接保存
|
||||||
// - 如果配置了前缀模式,则拼接上前缀保存
|
// - 如果配置了前缀模式,则拼接上前缀保存
|
||||||
String tokenPrefix = getConfig().getTokenPrefix();
|
String tokenPrefix = getConfigOrGlobal().getTokenPrefix();
|
||||||
if( SaFoxUtil.isEmpty(tokenPrefix) ) {
|
if( SaFoxUtil.isEmpty(tokenPrefix) ) {
|
||||||
storage.set(splicingKeyJustCreatedSave(), tokenValue);
|
storage.set(splicingKeyJustCreatedSave(), tokenValue);
|
||||||
} else {
|
} else {
|
||||||
@@ -202,7 +238,7 @@ public class StpLogic {
|
|||||||
* @param cookieTimeout Cookie存活时间(单位:秒,填-1代表为内存Cookie,浏览器关闭后消失)
|
* @param cookieTimeout Cookie存活时间(单位:秒,填-1代表为内存Cookie,浏览器关闭后消失)
|
||||||
*/
|
*/
|
||||||
public void setTokenValueToCookie(String tokenValue, int cookieTimeout){
|
public void setTokenValueToCookie(String tokenValue, int cookieTimeout){
|
||||||
SaCookieConfig cfg = getConfig().getCookie();
|
SaCookieConfig cfg = getConfigOrGlobal().getCookie();
|
||||||
SaCookie cookie = new SaCookie()
|
SaCookie cookie = new SaCookie()
|
||||||
.setName(getTokenName())
|
.setName(getTokenName())
|
||||||
.setValue(tokenValue)
|
.setValue(tokenValue)
|
||||||
@@ -252,7 +288,7 @@ public class StpLogic {
|
|||||||
String tokenValue = getTokenValueNotCut();
|
String tokenValue = getTokenValueNotCut();
|
||||||
|
|
||||||
// 2、如果全局配置打开了前缀模式,则二次处理一下
|
// 2、如果全局配置打开了前缀模式,则二次处理一下
|
||||||
String tokenPrefix = getConfig().getTokenPrefix();
|
String tokenPrefix = getConfigOrGlobal().getTokenPrefix();
|
||||||
if(SaFoxUtil.isNotEmpty(tokenPrefix)) {
|
if(SaFoxUtil.isNotEmpty(tokenPrefix)) {
|
||||||
|
|
||||||
// 情况2.1:如果提交的 token 为空,则转为 null
|
// 情况2.1:如果提交的 token 为空,则转为 null
|
||||||
@@ -289,7 +325,7 @@ public class StpLogic {
|
|||||||
// 获取相应对象
|
// 获取相应对象
|
||||||
SaStorage storage = SaHolder.getStorage();
|
SaStorage storage = SaHolder.getStorage();
|
||||||
SaRequest request = SaHolder.getRequest();
|
SaRequest request = SaHolder.getRequest();
|
||||||
SaTokenConfig config = getConfig();
|
SaTokenConfig config = getConfigOrGlobal();
|
||||||
String keyTokenName = getTokenName();
|
String keyTokenName = getTokenName();
|
||||||
String tokenValue = null;
|
String tokenValue = null;
|
||||||
|
|
||||||
@@ -428,7 +464,7 @@ public class StpLogic {
|
|||||||
checkLoginArgs(id, loginModel);
|
checkLoginArgs(id, loginModel);
|
||||||
|
|
||||||
// 2、初始化 loginModel ,给一些参数补上默认值
|
// 2、初始化 loginModel ,给一些参数补上默认值
|
||||||
SaTokenConfig config = getConfig();
|
SaTokenConfig config = getConfigOrGlobal();
|
||||||
loginModel.build(config);
|
loginModel.build(config);
|
||||||
|
|
||||||
// 3、给这个账号分配一个可用的 token
|
// 3、给这个账号分配一个可用的 token
|
||||||
@@ -473,7 +509,7 @@ public class StpLogic {
|
|||||||
|
|
||||||
// 1、获取全局配置的 isConcurrent 参数
|
// 1、获取全局配置的 isConcurrent 参数
|
||||||
// 如果配置为:不允许一个账号多地同时登录,则需要先将这个账号的历史登录会话标记为:被顶下线
|
// 如果配置为:不允许一个账号多地同时登录,则需要先将这个账号的历史登录会话标记为:被顶下线
|
||||||
Boolean isConcurrent = getConfig().getIsConcurrent();
|
Boolean isConcurrent = getConfigOrGlobal().getIsConcurrent();
|
||||||
if( ! isConcurrent) {
|
if( ! isConcurrent) {
|
||||||
replaced(id, loginModel.getDevice());
|
replaced(id, loginModel.getDevice());
|
||||||
}
|
}
|
||||||
@@ -548,7 +584,7 @@ public class StpLogic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 5、如果全局配置未启动动态 activeTimeout 功能,但是此次登录却传入了 activeTimeout 参数,那么就打印警告信息
|
// 5、如果全局配置未启动动态 activeTimeout 功能,但是此次登录却传入了 activeTimeout 参数,那么就打印警告信息
|
||||||
if( ! getConfig().getDynamicActiveTimeout() && loginModel.getActiveTimeout() != null) {
|
if( ! getConfigOrGlobal().getDynamicActiveTimeout() && loginModel.getActiveTimeout() != null) {
|
||||||
SaManager.log.warn("当前全局配置未开启动态 activeTimeout 功能,传入的 activeTimeout 参数将被忽略");
|
SaManager.log.warn("当前全局配置未开启动态 activeTimeout 功能,传入的 activeTimeout 参数将被忽略");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -567,8 +603,8 @@ public class StpLogic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 2、如果打开了 Cookie 模式,则先把 Cookie 数据清除掉
|
// 2、如果打开了 Cookie 模式,则先把 Cookie 数据清除掉
|
||||||
if(getConfig().getIsReadCookie()){
|
if(getConfigOrGlobal().getIsReadCookie()){
|
||||||
SaCookieConfig cookie = getConfig().getCookie();
|
SaCookieConfig cookie = getConfigOrGlobal().getCookie();
|
||||||
SaHolder.getResponse().deleteCookie(getTokenName(), cookie.getPath(), cookie.getDomain());
|
SaHolder.getResponse().deleteCookie(getTokenName(), cookie.getPath(), cookie.getDomain());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -922,7 +958,7 @@ public class StpLogic {
|
|||||||
// ------ 至此,loginId 已经是一个合法的值,代表当前会话是一个正常的登录状态了
|
// ------ 至此,loginId 已经是一个合法的值,代表当前会话是一个正常的登录状态了
|
||||||
|
|
||||||
// 8、如果配置了自动续签功能, 则: 更新这个 token 的最后活跃时间 (注意此处的续签是在续 active-timeout,而非 timeout)
|
// 8、如果配置了自动续签功能, 则: 更新这个 token 的最后活跃时间 (注意此处的续签是在续 active-timeout,而非 timeout)
|
||||||
if(getConfig().getAutoRenew()) {
|
if(getConfigOrGlobal().getAutoRenew()) {
|
||||||
updateLastActiveToNow(tokenValue);
|
updateLastActiveToNow(tokenValue);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1139,7 +1175,7 @@ public class StpLogic {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 将这个 SaSession 入库
|
// 将这个 SaSession 入库
|
||||||
getSaTokenDao().setSession(session, getConfig().getTimeout());
|
getSaTokenDao().setSession(session, getConfigOrGlobal().getTimeout());
|
||||||
}
|
}
|
||||||
return session;
|
return session;
|
||||||
}
|
}
|
||||||
@@ -1239,7 +1275,7 @@ public class StpLogic {
|
|||||||
public SaSession getTokenSession(boolean isCreate) {
|
public SaSession getTokenSession(boolean isCreate) {
|
||||||
|
|
||||||
// 1、如果配置了:tokenSessionCheckLogin == true,则需要先校验当前是否登录,未登录情况下不允许拿到 Token-Session
|
// 1、如果配置了:tokenSessionCheckLogin == true,则需要先校验当前是否登录,未登录情况下不允许拿到 Token-Session
|
||||||
if(getConfig().getTokenSessionCheckLogin()) {
|
if(getConfigOrGlobal().getTokenSessionCheckLogin()) {
|
||||||
checkLogin();
|
checkLogin();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1307,7 +1343,7 @@ public class StpLogic {
|
|||||||
"token",
|
"token",
|
||||||
getConfigOfMaxTryTimes(),
|
getConfigOfMaxTryTimes(),
|
||||||
() -> {
|
() -> {
|
||||||
return createTokenValue(null, null, getConfig().getTimeout(), null);
|
return createTokenValue(null, null, getConfigOrGlobal().getTimeout(), null);
|
||||||
},
|
},
|
||||||
token -> {
|
token -> {
|
||||||
return getTokenSessionByToken(token, false) == null;
|
return getTokenSessionByToken(token, false) == null;
|
||||||
@@ -1361,7 +1397,7 @@ public class StpLogic {
|
|||||||
protected void setLastActiveToNow(String tokenValue, Long activeTimeout, Long timeout) {
|
protected void setLastActiveToNow(String tokenValue, Long activeTimeout, Long timeout) {
|
||||||
|
|
||||||
// 如果提供的 timeout 为null,则使用全局配置的 timeout 值
|
// 如果提供的 timeout 为null,则使用全局配置的 timeout 值
|
||||||
SaTokenConfig config = getConfig();
|
SaTokenConfig config = getConfigOrGlobal();
|
||||||
if(timeout == null) {
|
if(timeout == null) {
|
||||||
timeout = config.getTimeout();
|
timeout = config.getTimeout();
|
||||||
}
|
}
|
||||||
@@ -1451,7 +1487,7 @@ public class StpLogic {
|
|||||||
*/
|
*/
|
||||||
public Long getTokenUseActiveTimeout(String tokenValue) {
|
public Long getTokenUseActiveTimeout(String tokenValue) {
|
||||||
// 在未启用动态 activeTimeout 功能时,直接返回 null
|
// 在未启用动态 activeTimeout 功能时,直接返回 null
|
||||||
if( ! getConfig().getDynamicActiveTimeout()) {
|
if( ! getConfigOrGlobal().getDynamicActiveTimeout()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1473,7 +1509,7 @@ public class StpLogic {
|
|||||||
public long getTokenUseActiveTimeoutOrGlobalConfig(String tokenValue) {
|
public long getTokenUseActiveTimeoutOrGlobalConfig(String tokenValue) {
|
||||||
Long activeTimeout = getTokenUseActiveTimeout(tokenValue);
|
Long activeTimeout = getTokenUseActiveTimeout(tokenValue);
|
||||||
if(activeTimeout == null) {
|
if(activeTimeout == null) {
|
||||||
return getConfig().getActiveTimeout();
|
return getConfigOrGlobal().getActiveTimeout();
|
||||||
}
|
}
|
||||||
return activeTimeout;
|
return activeTimeout;
|
||||||
}
|
}
|
||||||
@@ -1618,7 +1654,7 @@ public class StpLogic {
|
|||||||
renewTimeout(tokenValue, timeout);
|
renewTimeout(tokenValue, timeout);
|
||||||
|
|
||||||
// 2、续期客户端 Cookie 有效期
|
// 2、续期客户端 Cookie 有效期
|
||||||
if(getConfig().getIsReadCookie()) {
|
if(getConfigOrGlobal().getIsReadCookie()) {
|
||||||
// 如果 timeout = -1,代表永久,但是一般浏览器不支持永久 Cookie,所以此处设置为 int 最大值
|
// 如果 timeout = -1,代表永久,但是一般浏览器不支持永久 Cookie,所以此处设置为 int 最大值
|
||||||
// 如果 timeout 大于 int 最大值,会造成数据溢出,所以也要将其设置为 int 最大值
|
// 如果 timeout 大于 int 最大值,会造成数据溢出,所以也要将其设置为 int 最大值
|
||||||
if(timeout == SaTokenDao.NEVER_EXPIRE || timeout > Integer.MAX_VALUE) {
|
if(timeout == SaTokenDao.NEVER_EXPIRE || timeout > Integer.MAX_VALUE) {
|
||||||
@@ -2634,7 +2670,7 @@ public class StpLogic {
|
|||||||
* @return key
|
* @return key
|
||||||
*/
|
*/
|
||||||
public String splicingKeyTokenName() {
|
public String splicingKeyTokenName() {
|
||||||
return getConfig().getTokenName();
|
return getConfigOrGlobal().getTokenName();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2644,7 +2680,7 @@ public class StpLogic {
|
|||||||
* @return key
|
* @return key
|
||||||
*/
|
*/
|
||||||
public String splicingKeyTokenValue(String tokenValue) {
|
public String splicingKeyTokenValue(String tokenValue) {
|
||||||
return getConfig().getTokenName() + ":" + loginType + ":token:" + tokenValue;
|
return getConfigOrGlobal().getTokenName() + ":" + loginType + ":token:" + tokenValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2654,7 +2690,7 @@ public class StpLogic {
|
|||||||
* @return key
|
* @return key
|
||||||
*/
|
*/
|
||||||
public String splicingKeySession(Object loginId) {
|
public String splicingKeySession(Object loginId) {
|
||||||
return getConfig().getTokenName() + ":" + loginType + ":session:" + loginId;
|
return getConfigOrGlobal().getTokenName() + ":" + loginType + ":session:" + loginId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2664,7 +2700,7 @@ public class StpLogic {
|
|||||||
* @return key
|
* @return key
|
||||||
*/
|
*/
|
||||||
public String splicingKeyTokenSession(String tokenValue) {
|
public String splicingKeyTokenSession(String tokenValue) {
|
||||||
return getConfig().getTokenName() + ":" + loginType + ":token-session:" + tokenValue;
|
return getConfigOrGlobal().getTokenName() + ":" + loginType + ":token-session:" + tokenValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2674,7 +2710,7 @@ public class StpLogic {
|
|||||||
* @return key
|
* @return key
|
||||||
*/
|
*/
|
||||||
public String splicingKeyLastActiveTime(String tokenValue) {
|
public String splicingKeyLastActiveTime(String tokenValue) {
|
||||||
return getConfig().getTokenName() + ":" + loginType + ":last-active:" + tokenValue;
|
return getConfigOrGlobal().getTokenName() + ":" + loginType + ":last-active:" + tokenValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2704,7 +2740,7 @@ public class StpLogic {
|
|||||||
* @return key
|
* @return key
|
||||||
*/
|
*/
|
||||||
public String splicingKeyDisable(Object loginId, String service) {
|
public String splicingKeyDisable(Object loginId, String service) {
|
||||||
return getConfig().getTokenName() + ":" + loginType + ":disable:" + service + ":" + loginId;
|
return getConfigOrGlobal().getTokenName() + ":" + loginType + ":disable:" + service + ":" + loginId;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2717,20 +2753,19 @@ public class StpLogic {
|
|||||||
public String splicingKeySafe(String tokenValue, String service) {
|
public String splicingKeySafe(String tokenValue, String service) {
|
||||||
// 格式:<Token名称>:<账号类型>:<safe>:<业务标识>:<Token值>
|
// 格式:<Token名称>:<账号类型>:<safe>:<业务标识>:<Token值>
|
||||||
// 形如:satoken:login:safe:important:gr_SwoIN0MC1ewxHX_vfCW3BothWDZMMtx__
|
// 形如:satoken:login:safe:important:gr_SwoIN0MC1ewxHX_vfCW3BothWDZMMtx__
|
||||||
return getConfig().getTokenName() + ":" + loginType + ":safe:" + service + ":" + tokenValue;
|
return getConfigOrGlobal().getTokenName() + ":" + loginType + ":safe:" + service + ":" + tokenValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// ------------------- Bean 对象、字段代理 -------------------
|
// ------------------- Bean 对象、字段代理 -------------------
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 返回全局配置对象
|
* 返回当前 StpLogic 使用的持久化对象
|
||||||
*
|
*
|
||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public SaTokenConfig getConfig() {
|
public SaTokenDao getSaTokenDao() {
|
||||||
// 为什么再次代理一层? 为某些业务场景下需要 [ 不同StpLogic不同配置 ] 提供便利
|
return SaManager.getSaTokenDao();
|
||||||
return SaManager.getConfig();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2739,7 +2774,7 @@ public class StpLogic {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public boolean getConfigOfIsShare() {
|
public boolean getConfigOfIsShare() {
|
||||||
return getConfig().getIsShare();
|
return getConfigOrGlobal().getIsShare();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2748,7 +2783,7 @@ public class StpLogic {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public boolean isOpenCheckActiveTimeout() {
|
public boolean isOpenCheckActiveTimeout() {
|
||||||
return getConfig().getActiveTimeout() != SaTokenDao.NEVER_EXPIRE;
|
return getConfigOrGlobal().getActiveTimeout() != SaTokenDao.NEVER_EXPIRE;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -2757,7 +2792,7 @@ public class StpLogic {
|
|||||||
* @return Cookie 应该保存的时长
|
* @return Cookie 应该保存的时长
|
||||||
*/
|
*/
|
||||||
public int getConfigOfCookieTimeout() {
|
public int getConfigOfCookieTimeout() {
|
||||||
long timeout = getConfig().getTimeout();
|
long timeout = getConfigOrGlobal().getTimeout();
|
||||||
if(timeout == SaTokenDao.NEVER_EXPIRE) {
|
if(timeout == SaTokenDao.NEVER_EXPIRE) {
|
||||||
return Integer.MAX_VALUE;
|
return Integer.MAX_VALUE;
|
||||||
}
|
}
|
||||||
@@ -2770,16 +2805,7 @@ public class StpLogic {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public int getConfigOfMaxTryTimes() {
|
public int getConfigOfMaxTryTimes() {
|
||||||
return getConfig().getMaxTryTimes();
|
return getConfigOrGlobal().getMaxTryTimes();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 返回持久化对象
|
|
||||||
*
|
|
||||||
* @return /
|
|
||||||
*/
|
|
||||||
public SaTokenDao getSaTokenDao() {
|
|
||||||
return SaManager.getSaTokenDao();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -19,7 +19,6 @@ import cn.dev33.satoken.SaManager;
|
|||||||
import cn.dev33.satoken.annotation.*;
|
import cn.dev33.satoken.annotation.*;
|
||||||
import cn.dev33.satoken.basic.SaBasicUtil;
|
import cn.dev33.satoken.basic.SaBasicUtil;
|
||||||
import cn.dev33.satoken.exception.SaTokenException;
|
import cn.dev33.satoken.exception.SaTokenException;
|
||||||
import cn.dev33.satoken.fun.strategy.SaGenerateUniqueTokenFunction;
|
|
||||||
import cn.dev33.satoken.fun.strategy.*;
|
import cn.dev33.satoken.fun.strategy.*;
|
||||||
import cn.dev33.satoken.session.SaSession;
|
import cn.dev33.satoken.session.SaSession;
|
||||||
import cn.dev33.satoken.stp.StpLogic;
|
import cn.dev33.satoken.stp.StpLogic;
|
||||||
@@ -64,7 +63,7 @@ public final class SaStrategy {
|
|||||||
*/
|
*/
|
||||||
public SaCreateTokenFunction createToken = (loginId, loginType) -> {
|
public SaCreateTokenFunction createToken = (loginId, loginType) -> {
|
||||||
// 根据配置的tokenStyle生成不同风格的token
|
// 根据配置的tokenStyle生成不同风格的token
|
||||||
String tokenStyle = SaManager.getConfig().getTokenStyle();
|
String tokenStyle = SaManager.getStpLogic(loginType).getConfigOrGlobal().getTokenStyle();
|
||||||
|
|
||||||
switch (tokenStyle) {
|
switch (tokenStyle) {
|
||||||
// uuid
|
// uuid
|
||||||
|
@@ -23,7 +23,7 @@ public class StpUserUtil {
|
|||||||
/**
|
/**
|
||||||
* 多账号体系下的类型标识
|
* 多账号体系下的类型标识
|
||||||
*/
|
*/
|
||||||
public static final String TYPE = "login";
|
public static final String TYPE = "user";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 底层使用的 StpLogic 对象
|
* 底层使用的 StpLogic 对象
|
||||||
|
@@ -1,16 +1,15 @@
|
|||||||
package com.pj.satoken;
|
package com.pj.satoken;
|
||||||
|
|
||||||
import org.springframework.context.annotation.Bean;
|
|
||||||
import org.springframework.context.annotation.Configuration;
|
|
||||||
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
|
||||||
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
|
||||||
|
|
||||||
import cn.dev33.satoken.context.SaHolder;
|
import cn.dev33.satoken.context.SaHolder;
|
||||||
import cn.dev33.satoken.filter.SaServletFilter;
|
import cn.dev33.satoken.filter.SaServletFilter;
|
||||||
import cn.dev33.satoken.interceptor.SaInterceptor;
|
import cn.dev33.satoken.interceptor.SaInterceptor;
|
||||||
import cn.dev33.satoken.router.SaHttpMethod;
|
import cn.dev33.satoken.router.SaHttpMethod;
|
||||||
import cn.dev33.satoken.router.SaRouter;
|
import cn.dev33.satoken.router.SaRouter;
|
||||||
import cn.dev33.satoken.util.SaResult;
|
import cn.dev33.satoken.util.SaResult;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
|
||||||
|
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@@ -23,7 +23,7 @@ public class StpUserUtil {
|
|||||||
/**
|
/**
|
||||||
* 多账号体系下的类型标识
|
* 多账号体系下的类型标识
|
||||||
*/
|
*/
|
||||||
public static final String TYPE = "login";
|
public static final String TYPE = "user";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 底层使用的 StpLogic 对象
|
* 底层使用的 StpLogic 对象
|
||||||
|
@@ -1,8 +1,10 @@
|
|||||||
package com.pj.test;
|
package com.pj.test;
|
||||||
|
|
||||||
|
import cn.dev33.satoken.stp.SaLoginConfig;
|
||||||
import cn.dev33.satoken.stp.StpUtil;
|
import cn.dev33.satoken.stp.StpUtil;
|
||||||
import cn.dev33.satoken.util.SaFoxUtil;
|
import cn.dev33.satoken.util.SaFoxUtil;
|
||||||
import cn.dev33.satoken.util.SaResult;
|
import cn.dev33.satoken.util.SaResult;
|
||||||
|
import com.pj.satoken.StpUserUtil;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.RestController;
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
@@ -21,7 +23,7 @@ public class TestController {
|
|||||||
// 测试登录 ---- http://localhost:8081/test/login
|
// 测试登录 ---- http://localhost:8081/test/login
|
||||||
@RequestMapping("login")
|
@RequestMapping("login")
|
||||||
public SaResult login(@RequestParam(defaultValue = "10001") long id) {
|
public SaResult login(@RequestParam(defaultValue = "10001") long id) {
|
||||||
StpUtil.login(id);
|
StpUtil.login(id, SaLoginConfig.setActiveTimeout(1000));
|
||||||
return SaResult.ok("登录成功");
|
return SaResult.ok("登录成功");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -29,7 +31,7 @@ public class TestController {
|
|||||||
@RequestMapping("test")
|
@RequestMapping("test")
|
||||||
public SaResult test() {
|
public SaResult test() {
|
||||||
System.out.println("------------进来了 " + SaFoxUtil.formatDate(new Date()));
|
System.out.println("------------进来了 " + SaFoxUtil.formatDate(new Date()));
|
||||||
StpUtil.checkLogin();
|
StpUserUtil.login(10001, SaLoginConfig.setActiveTimeout(2000));
|
||||||
// 返回
|
// 返回
|
||||||
return SaResult.data(null);
|
return SaResult.data(null);
|
||||||
}
|
}
|
||||||
|
@@ -164,28 +164,30 @@ public class StpUserUtil {
|
|||||||
如果自定义的 StpUserUtil 需要使用不同 SaTokenConfig 对象, 也很简单,参考示例如下:
|
如果自定义的 StpUserUtil 需要使用不同 SaTokenConfig 对象, 也很简单,参考示例如下:
|
||||||
|
|
||||||
``` java
|
``` java
|
||||||
public class StpUserUtil {
|
@Configuration
|
||||||
|
public class SaTokenConfigure {
|
||||||
|
|
||||||
// 使用匿名子类 重写`stpLogic对象`的一些方法
|
@Autowired
|
||||||
public static StpLogic stpLogic = new StpLogic("user") {
|
public void setSaTokenConfig() {
|
||||||
|
// 设定 StpUtil 使用的 SaTokenConfig 配置参数对象
|
||||||
|
SaTokenConfig config1 = new SaTokenConfig();
|
||||||
|
config1.setTokenName("satoken1");
|
||||||
|
config1.setTimeout(1000);
|
||||||
|
config1.setTokenStyle("random-64");
|
||||||
|
// 更多设置 ...
|
||||||
|
StpUtil.stpLogic.setConfig(config1);
|
||||||
|
|
||||||
// 首先自定义一个 Config 对象
|
// 设定 StpUserUtil 使用的 SaTokenConfig 配置参数对象
|
||||||
SaTokenConfig config = new SaTokenConfig()
|
SaTokenConfig config2 = new SaTokenConfig();
|
||||||
.setTokenName("satoken")
|
config2.setTokenName("satoken2");
|
||||||
.setTimeout(2592000)
|
config2.setTimeout(2000);
|
||||||
// ... 其它set
|
config2.setTokenStyle("tik");
|
||||||
;
|
// 更多设置 ...
|
||||||
|
StpUserUtil.stpLogic.setConfig(config2);
|
||||||
// 然后重写 stpLogic 配置获取方法
|
|
||||||
@Override
|
|
||||||
public SaTokenConfig getConfig() {
|
|
||||||
return config;
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// ...
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
### 9、多账号体系混合鉴权
|
### 9、多账号体系混合鉴权
|
||||||
|
@@ -55,7 +55,7 @@ public class StpLogicJwtForMixin extends StpLogic {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public String jwtSecretKey() {
|
public String jwtSecretKey() {
|
||||||
String keyt = getConfig().getJwtSecretKey();
|
String keyt = getConfigOrGlobal().getJwtSecretKey();
|
||||||
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
|
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
|
||||||
return keyt;
|
return keyt;
|
||||||
}
|
}
|
||||||
@@ -120,7 +120,7 @@ public class StpLogicJwtForMixin extends StpLogic {
|
|||||||
SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
|
SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
|
||||||
|
|
||||||
// 如果打开了Cookie模式,则把cookie清除掉
|
// 如果打开了Cookie模式,则把cookie清除掉
|
||||||
if(getConfig().getIsReadCookie()){
|
if(getConfigOrGlobal().getIsReadCookie()){
|
||||||
SaHolder.getResponse().deleteCookie(getTokenName());
|
SaHolder.getResponse().deleteCookie(getTokenName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@@ -50,7 +50,7 @@ public class StpLogicJwtForSimple extends StpLogic {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public String jwtSecretKey() {
|
public String jwtSecretKey() {
|
||||||
String keyt = getConfig().getJwtSecretKey();
|
String keyt = getConfigOrGlobal().getJwtSecretKey();
|
||||||
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
|
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
|
||||||
return keyt;
|
return keyt;
|
||||||
}
|
}
|
||||||
|
@@ -57,7 +57,7 @@ public class StpLogicJwtForStateless extends StpLogic {
|
|||||||
* @return /
|
* @return /
|
||||||
*/
|
*/
|
||||||
public String jwtSecretKey() {
|
public String jwtSecretKey() {
|
||||||
String keyt = getConfig().getJwtSecretKey();
|
String keyt = getConfigOrGlobal().getJwtSecretKey();
|
||||||
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
|
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
|
||||||
return keyt;
|
return keyt;
|
||||||
}
|
}
|
||||||
@@ -111,7 +111,7 @@ public class StpLogicJwtForStateless extends StpLogic {
|
|||||||
checkLoginArgs(id, loginModel);
|
checkLoginArgs(id, loginModel);
|
||||||
|
|
||||||
// 2、初始化 loginModel ,给一些参数补上默认值
|
// 2、初始化 loginModel ,给一些参数补上默认值
|
||||||
loginModel.build(getConfig());
|
loginModel.build(getConfigOrGlobal());
|
||||||
|
|
||||||
// 3、生成一个token
|
// 3、生成一个token
|
||||||
String tokenValue = createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getTimeout(), loginModel.getExtraData());
|
String tokenValue = createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getTimeout(), loginModel.getExtraData());
|
||||||
@@ -151,7 +151,7 @@ public class StpLogicJwtForStateless extends StpLogic {
|
|||||||
SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
|
SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
|
||||||
|
|
||||||
// 如果打开了Cookie模式,则把cookie清除掉
|
// 如果打开了Cookie模式,则把cookie清除掉
|
||||||
if(getConfig().getIsReadCookie()){
|
if(getConfigOrGlobal().getIsReadCookie()){
|
||||||
SaHolder.getResponse().deleteCookie(getTokenName());
|
SaHolder.getResponse().deleteCookie(getTokenName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user