增加为 StpLogic 单独配置 SaTokenConfig 参数的能力

This commit is contained in:
click33
2023-06-08 10:30:58 +08:00
parent f59b1b3885
commit 3626c490b8
10 changed files with 113 additions and 85 deletions

View File

@@ -101,6 +101,42 @@ public class StpLogic {
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 相关 -------------------
@@ -132,7 +168,7 @@ public class StpLogic {
* @param tokenValue token 值
*/
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);
// 2. 将 token 写入到当前会话的 Cookie 里
if (getConfig().getIsReadCookie()) {
if (getConfigOrGlobal().getIsReadCookie()) {
setTokenValueToCookie(tokenValue, loginModel.getCookieTimeout());
}
@@ -184,7 +220,7 @@ public class StpLogic {
// 2、保存 token
// - 如果没有配置前缀模式,直接保存
// - 如果配置了前缀模式,则拼接上前缀保存
String tokenPrefix = getConfig().getTokenPrefix();
String tokenPrefix = getConfigOrGlobal().getTokenPrefix();
if( SaFoxUtil.isEmpty(tokenPrefix) ) {
storage.set(splicingKeyJustCreatedSave(), tokenValue);
} else {
@@ -202,7 +238,7 @@ public class StpLogic {
* @param cookieTimeout Cookie存活时间单位填-1代表为内存Cookie浏览器关闭后消失
*/
public void setTokenValueToCookie(String tokenValue, int cookieTimeout){
SaCookieConfig cfg = getConfig().getCookie();
SaCookieConfig cfg = getConfigOrGlobal().getCookie();
SaCookie cookie = new SaCookie()
.setName(getTokenName())
.setValue(tokenValue)
@@ -252,7 +288,7 @@ public class StpLogic {
String tokenValue = getTokenValueNotCut();
// 2、如果全局配置打开了前缀模式则二次处理一下
String tokenPrefix = getConfig().getTokenPrefix();
String tokenPrefix = getConfigOrGlobal().getTokenPrefix();
if(SaFoxUtil.isNotEmpty(tokenPrefix)) {
// 情况2.1:如果提交的 token 为空,则转为 null
@@ -289,7 +325,7 @@ public class StpLogic {
// 获取相应对象
SaStorage storage = SaHolder.getStorage();
SaRequest request = SaHolder.getRequest();
SaTokenConfig config = getConfig();
SaTokenConfig config = getConfigOrGlobal();
String keyTokenName = getTokenName();
String tokenValue = null;
@@ -428,7 +464,7 @@ public class StpLogic {
checkLoginArgs(id, loginModel);
// 2、初始化 loginModel ,给一些参数补上默认值
SaTokenConfig config = getConfig();
SaTokenConfig config = getConfigOrGlobal();
loginModel.build(config);
// 3、给这个账号分配一个可用的 token
@@ -473,7 +509,7 @@ public class StpLogic {
// 1、获取全局配置的 isConcurrent 参数
// 如果配置为:不允许一个账号多地同时登录,则需要先将这个账号的历史登录会话标记为:被顶下线
Boolean isConcurrent = getConfig().getIsConcurrent();
Boolean isConcurrent = getConfigOrGlobal().getIsConcurrent();
if( ! isConcurrent) {
replaced(id, loginModel.getDevice());
}
@@ -548,7 +584,7 @@ public class StpLogic {
}
// 5、如果全局配置未启动动态 activeTimeout 功能,但是此次登录却传入了 activeTimeout 参数,那么就打印警告信息
if( ! getConfig().getDynamicActiveTimeout() && loginModel.getActiveTimeout() != null) {
if( ! getConfigOrGlobal().getDynamicActiveTimeout() && loginModel.getActiveTimeout() != null) {
SaManager.log.warn("当前全局配置未开启动态 activeTimeout 功能,传入的 activeTimeout 参数将被忽略");
}
@@ -567,8 +603,8 @@ public class StpLogic {
}
// 2、如果打开了 Cookie 模式,则先把 Cookie 数据清除掉
if(getConfig().getIsReadCookie()){
SaCookieConfig cookie = getConfig().getCookie();
if(getConfigOrGlobal().getIsReadCookie()){
SaCookieConfig cookie = getConfigOrGlobal().getCookie();
SaHolder.getResponse().deleteCookie(getTokenName(), cookie.getPath(), cookie.getDomain());
}
@@ -922,7 +958,7 @@ public class StpLogic {
// ------ 至此loginId 已经是一个合法的值,代表当前会话是一个正常的登录状态了
// 8、如果配置了自动续签功能, 则: 更新这个 token 的最后活跃时间 (注意此处的续签是在续 active-timeout而非 timeout
if(getConfig().getAutoRenew()) {
if(getConfigOrGlobal().getAutoRenew()) {
updateLastActiveToNow(tokenValue);
}
}
@@ -1139,7 +1175,7 @@ public class StpLogic {
}
// 将这个 SaSession 入库
getSaTokenDao().setSession(session, getConfig().getTimeout());
getSaTokenDao().setSession(session, getConfigOrGlobal().getTimeout());
}
return session;
}
@@ -1239,7 +1275,7 @@ public class StpLogic {
public SaSession getTokenSession(boolean isCreate) {
// 1、如果配置了tokenSessionCheckLogin == true则需要先校验当前是否登录未登录情况下不允许拿到 Token-Session
if(getConfig().getTokenSessionCheckLogin()) {
if(getConfigOrGlobal().getTokenSessionCheckLogin()) {
checkLogin();
}
@@ -1307,7 +1343,7 @@ public class StpLogic {
"token",
getConfigOfMaxTryTimes(),
() -> {
return createTokenValue(null, null, getConfig().getTimeout(), null);
return createTokenValue(null, null, getConfigOrGlobal().getTimeout(), null);
},
token -> {
return getTokenSessionByToken(token, false) == null;
@@ -1361,7 +1397,7 @@ public class StpLogic {
protected void setLastActiveToNow(String tokenValue, Long activeTimeout, Long timeout) {
// 如果提供的 timeout 为null则使用全局配置的 timeout 值
SaTokenConfig config = getConfig();
SaTokenConfig config = getConfigOrGlobal();
if(timeout == null) {
timeout = config.getTimeout();
}
@@ -1451,7 +1487,7 @@ public class StpLogic {
*/
public Long getTokenUseActiveTimeout(String tokenValue) {
// 在未启用动态 activeTimeout 功能时,直接返回 null
if( ! getConfig().getDynamicActiveTimeout()) {
if( ! getConfigOrGlobal().getDynamicActiveTimeout()) {
return null;
}
@@ -1473,7 +1509,7 @@ public class StpLogic {
public long getTokenUseActiveTimeoutOrGlobalConfig(String tokenValue) {
Long activeTimeout = getTokenUseActiveTimeout(tokenValue);
if(activeTimeout == null) {
return getConfig().getActiveTimeout();
return getConfigOrGlobal().getActiveTimeout();
}
return activeTimeout;
}
@@ -1618,7 +1654,7 @@ public class StpLogic {
renewTimeout(tokenValue, timeout);
// 2、续期客户端 Cookie 有效期
if(getConfig().getIsReadCookie()) {
if(getConfigOrGlobal().getIsReadCookie()) {
// 如果 timeout = -1代表永久但是一般浏览器不支持永久 Cookie所以此处设置为 int 最大值
// 如果 timeout 大于 int 最大值,会造成数据溢出,所以也要将其设置为 int 最大值
if(timeout == SaTokenDao.NEVER_EXPIRE || timeout > Integer.MAX_VALUE) {
@@ -2634,7 +2670,7 @@ public class StpLogic {
* @return key
*/
public String splicingKeyTokenName() {
return getConfig().getTokenName();
return getConfigOrGlobal().getTokenName();
}
/**
@@ -2644,7 +2680,7 @@ public class StpLogic {
* @return key
*/
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
*/
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
*/
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
*/
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
*/
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) {
// 格式:<Token名称>:<账号类型>:<safe>:<业务标识>:<Token值>
// 形如satoken:login:safe:important:gr_SwoIN0MC1ewxHX_vfCW3BothWDZMMtx__
return getConfig().getTokenName() + ":" + loginType + ":safe:" + service + ":" + tokenValue;
return getConfigOrGlobal().getTokenName() + ":" + loginType + ":safe:" + service + ":" + tokenValue;
}
// ------------------- Bean 对象、字段代理 -------------------
/**
* 返回全局配置对象
* 返回当前 StpLogic 使用的持久化对象
*
* @return /
*/
public SaTokenConfig getConfig() {
// 为什么再次代理一层? 为某些业务场景下需要 [ 不同StpLogic不同配置 ] 提供便利
return SaManager.getConfig();
public SaTokenDao getSaTokenDao() {
return SaManager.getSaTokenDao();
}
/**
@@ -2739,7 +2774,7 @@ public class StpLogic {
* @return /
*/
public boolean getConfigOfIsShare() {
return getConfig().getIsShare();
return getConfigOrGlobal().getIsShare();
}
/**
@@ -2748,7 +2783,7 @@ public class StpLogic {
* @return /
*/
public boolean isOpenCheckActiveTimeout() {
return getConfig().getActiveTimeout() != SaTokenDao.NEVER_EXPIRE;
return getConfigOrGlobal().getActiveTimeout() != SaTokenDao.NEVER_EXPIRE;
}
/**
@@ -2757,7 +2792,7 @@ public class StpLogic {
* @return Cookie 应该保存的时长
*/
public int getConfigOfCookieTimeout() {
long timeout = getConfig().getTimeout();
long timeout = getConfigOrGlobal().getTimeout();
if(timeout == SaTokenDao.NEVER_EXPIRE) {
return Integer.MAX_VALUE;
}
@@ -2770,16 +2805,7 @@ public class StpLogic {
* @return /
*/
public int getConfigOfMaxTryTimes() {
return getConfig().getMaxTryTimes();
}
/**
* 返回持久化对象
*
* @return /
*/
public SaTokenDao getSaTokenDao() {
return SaManager.getSaTokenDao();
return getConfigOrGlobal().getMaxTryTimes();
}
/**

View File

@@ -19,7 +19,6 @@ import cn.dev33.satoken.SaManager;
import cn.dev33.satoken.annotation.*;
import cn.dev33.satoken.basic.SaBasicUtil;
import cn.dev33.satoken.exception.SaTokenException;
import cn.dev33.satoken.fun.strategy.SaGenerateUniqueTokenFunction;
import cn.dev33.satoken.fun.strategy.*;
import cn.dev33.satoken.session.SaSession;
import cn.dev33.satoken.stp.StpLogic;
@@ -64,7 +63,7 @@ public final class SaStrategy {
*/
public SaCreateTokenFunction createToken = (loginId, loginType) -> {
// 根据配置的tokenStyle生成不同风格的token
String tokenStyle = SaManager.getConfig().getTokenStyle();
String tokenStyle = SaManager.getStpLogic(loginType).getConfigOrGlobal().getTokenStyle();
switch (tokenStyle) {
// uuid

View File

@@ -23,7 +23,7 @@ public class StpUserUtil {
/**
* 多账号体系下的类型标识
*/
public static final String TYPE = "login";
public static final String TYPE = "user";
/**
* 底层使用的 StpLogic 对象

View File

@@ -1,16 +1,15 @@
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.filter.SaServletFilter;
import cn.dev33.satoken.interceptor.SaInterceptor;
import cn.dev33.satoken.router.SaHttpMethod;
import cn.dev33.satoken.router.SaRouter;
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;
/**

View File

@@ -23,7 +23,7 @@ public class StpUserUtil {
/**
* 多账号体系下的类型标识
*/
public static final String TYPE = "login";
public static final String TYPE = "user";
/**
* 底层使用的 StpLogic 对象

View File

@@ -1,8 +1,10 @@
package com.pj.test;
import cn.dev33.satoken.stp.SaLoginConfig;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaFoxUtil;
import cn.dev33.satoken.util.SaResult;
import com.pj.satoken.StpUserUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@@ -21,7 +23,7 @@ public class TestController {
// 测试登录 ---- http://localhost:8081/test/login
@RequestMapping("login")
public SaResult login(@RequestParam(defaultValue = "10001") long id) {
StpUtil.login(id);
StpUtil.login(id, SaLoginConfig.setActiveTimeout(1000));
return SaResult.ok("登录成功");
}
@@ -29,7 +31,7 @@ public class TestController {
@RequestMapping("test")
public SaResult test() {
System.out.println("------------进来了 " + SaFoxUtil.formatDate(new Date()));
StpUtil.checkLogin();
StpUserUtil.login(10001, SaLoginConfig.setActiveTimeout(2000));
// 返回
return SaResult.data(null);
}

View File

@@ -164,28 +164,30 @@ public class StpUserUtil {
如果自定义的 StpUserUtil 需要使用不同 SaTokenConfig 对象, 也很简单,参考示例如下:
``` java
public class StpUserUtil {
@Configuration
public class SaTokenConfigure {
// 使用匿名子类 重写`stpLogic对象`的一些方法
public static StpLogic stpLogic = new StpLogic("user") {
@Autowired
public void setSaTokenConfig() {
// 设定 StpUtil 使用的 SaTokenConfig 配置参数对象
SaTokenConfig config1 = new SaTokenConfig();
config1.setTokenName("satoken1");
config1.setTimeout(1000);
config1.setTokenStyle("random-64");
// 更多设置 ...
StpUtil.stpLogic.setConfig(config1);
// 首先自定义一个 Config 对象
SaTokenConfig config = new SaTokenConfig()
.setTokenName("satoken")
.setTimeout(2592000)
// ... 其它set
;
// 然后重写 stpLogic 配置获取方法
@Override
public SaTokenConfig getConfig() {
return config;
// 设定 StpUserUtil 使用的 SaTokenConfig 配置参数对象
SaTokenConfig config2 = new SaTokenConfig();
config2.setTokenName("satoken2");
config2.setTimeout(2000);
config2.setTokenStyle("tik");
// 更多设置 ...
StpUserUtil.stpLogic.setConfig(config2);
}
};
// ...
}
```
### 9、多账号体系混合鉴权

View File

@@ -55,7 +55,7 @@ public class StpLogicJwtForMixin extends StpLogic {
* @return /
*/
public String jwtSecretKey() {
String keyt = getConfig().getJwtSecretKey();
String keyt = getConfigOrGlobal().getJwtSecretKey();
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
return keyt;
}
@@ -120,7 +120,7 @@ public class StpLogicJwtForMixin extends StpLogic {
SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
// 如果打开了Cookie模式则把cookie清除掉
if(getConfig().getIsReadCookie()){
if(getConfigOrGlobal().getIsReadCookie()){
SaHolder.getResponse().deleteCookie(getTokenName());
}
}

View File

@@ -50,7 +50,7 @@ public class StpLogicJwtForSimple extends StpLogic {
* @return /
*/
public String jwtSecretKey() {
String keyt = getConfig().getJwtSecretKey();
String keyt = getConfigOrGlobal().getJwtSecretKey();
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
return keyt;
}

View File

@@ -57,7 +57,7 @@ public class StpLogicJwtForStateless extends StpLogic {
* @return /
*/
public String jwtSecretKey() {
String keyt = getConfig().getJwtSecretKey();
String keyt = getConfigOrGlobal().getJwtSecretKey();
SaJwtException.throwByNull(keyt, "请配置jwt秘钥", SaJwtErrorCode.CODE_30205);
return keyt;
}
@@ -111,7 +111,7 @@ public class StpLogicJwtForStateless extends StpLogic {
checkLoginArgs(id, loginModel);
// 2、初始化 loginModel ,给一些参数补上默认值
loginModel.build(getConfig());
loginModel.build(getConfigOrGlobal());
// 3、生成一个token
String tokenValue = createTokenValue(id, loginModel.getDeviceOrDefault(), loginModel.getTimeout(), loginModel.getExtraData());
@@ -151,7 +151,7 @@ public class StpLogicJwtForStateless extends StpLogic {
SaHolder.getStorage().delete(splicingKeyJustCreatedSave());
// 如果打开了Cookie模式则把cookie清除掉
if(getConfig().getIsReadCookie()){
if(getConfigOrGlobal().getIsReadCookie()){
SaHolder.getResponse().deleteCookie(getTokenName());
}
}