mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-09-19 10:08:07 +08:00
新增 Dubbo 集成插件
This commit is contained in:
@@ -80,6 +80,8 @@ public class SaTokenConfig implements Serializable {
|
||||
/** 配置当前项目的网络访问地址 */
|
||||
private String currDomain;
|
||||
|
||||
/** 是否校验Id-Token(部分rpc插件有效) */
|
||||
private Boolean checkIdToken = false;
|
||||
|
||||
/**
|
||||
* Cookie配置对象
|
||||
@@ -399,6 +401,22 @@ public class SaTokenConfig implements Serializable {
|
||||
this.currDomain = currDomain;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return 是否校验Id-Token(部分rpc插件有效)
|
||||
*/
|
||||
public Boolean getCheckIdToken() {
|
||||
return checkIdToken;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param checkIdToken 是否校验Id-Token(部分rpc插件有效)
|
||||
* @return 对象自身
|
||||
*/
|
||||
public SaTokenConfig setCheckIdToken(Boolean checkIdToken) {
|
||||
this.checkIdToken = checkIdToken;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return SSO单点登录配置对象
|
||||
@@ -454,6 +472,7 @@ public class SaTokenConfig implements Serializable {
|
||||
+ ", idTokenTimeout=" + idTokenTimeout
|
||||
+ ", basic=" + basic
|
||||
+ ", currDomain=" + currDomain
|
||||
+ ", checkIdToken=" + checkIdToken
|
||||
+ ", sso=" + sso
|
||||
+ ", cookie=" + cookie
|
||||
+ "]";
|
||||
|
@@ -94,29 +94,52 @@ public class StpLogic {
|
||||
return SaStrategy.me.createToken.apply(loginId, loginType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在当前会话写入当前TokenValue
|
||||
* @param tokenValue token值
|
||||
*/
|
||||
public void setTokenValue(String tokenValue){
|
||||
setTokenValue(tokenValue, (int)SaManager.getConfig().getTimeout());
|
||||
}
|
||||
|
||||
/**
|
||||
* 在当前会话写入当前TokenValue
|
||||
* @param tokenValue token值
|
||||
* @param cookieTimeout Cookie存活时间(秒)
|
||||
*/
|
||||
public void setTokenValue(String tokenValue, int cookieTimeout){
|
||||
SaTokenConfig config = getConfig();
|
||||
|
||||
if(SaFoxUtil.isEmpty(tokenValue)) {
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 将token保存到[存储器]里
|
||||
setTokenValueToStorage(tokenValue);
|
||||
|
||||
// 2. 将 Token 保存到 [Cookie] 里
|
||||
if (getConfig().getIsReadCookie()) {
|
||||
setTokenValueToCookie(tokenValue, cookieTimeout);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 将 Token 保存到 [Storage] 里
|
||||
* @param tokenValue token值
|
||||
*/
|
||||
public void setTokenValueToStorage(String tokenValue){
|
||||
// 1. 将token保存到[存储器]里
|
||||
SaStorage storage = SaHolder.getStorage();
|
||||
|
||||
// 如果打开了token前缀模式,则拼接上前缀一起写入
|
||||
String tokenPrefix = config.getTokenPrefix();
|
||||
// 2. 如果打开了 Token 前缀模式,则拼接上前缀
|
||||
String tokenPrefix = getConfig().getTokenPrefix();
|
||||
if(SaFoxUtil.isEmpty(tokenPrefix) == false) {
|
||||
storage.set(splicingKeyJustCreatedSave(), tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT + tokenValue);
|
||||
} else {
|
||||
storage.set(splicingKeyJustCreatedSave(), tokenValue);
|
||||
}
|
||||
|
||||
// 2. 将 Token 保存到 [Cookie] 里
|
||||
if (config.getIsReadCookie()) {
|
||||
setTokenValueToCookie(tokenValue, cookieTimeout);
|
||||
}
|
||||
// 3. 写入 (无前缀)
|
||||
storage.set(SaTokenConsts.JUST_CREATED_NOT_PREFIX, tokenValue);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,6 +167,30 @@ public class StpLogic {
|
||||
* @return 当前tokenValue
|
||||
*/
|
||||
public String getTokenValue(){
|
||||
// 1. 获取
|
||||
String tokenValue = getTokenValueNotCut();
|
||||
|
||||
// 2. 如果打开了前缀模式,则裁剪掉
|
||||
String tokenPrefix = getConfig().getTokenPrefix();
|
||||
if(SaFoxUtil.isEmpty(tokenPrefix) == false) {
|
||||
// 如果token并没有按照指定的前缀开头,则视为未提供token
|
||||
if(SaFoxUtil.isEmpty(tokenValue) || tokenValue.startsWith(tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT) == false) {
|
||||
tokenValue = null;
|
||||
} else {
|
||||
// 则裁剪掉前缀
|
||||
tokenValue = tokenValue.substring(tokenPrefix.length() + SaTokenConsts.TOKEN_CONNECTOR_CHAT.length());
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 返回
|
||||
return tokenValue;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前TokenValue (不裁剪前缀)
|
||||
* @return /
|
||||
*/
|
||||
public String getTokenValueNotCut(){
|
||||
// 0. 获取相应对象
|
||||
SaStorage storage = SaHolder.getStorage();
|
||||
SaRequest request = SaHolder.getRequest();
|
||||
@@ -168,19 +215,7 @@ public class StpLogic {
|
||||
tokenValue = request.getCookieValue(keyTokenName);
|
||||
}
|
||||
|
||||
// 5. 如果打开了前缀模式
|
||||
String tokenPrefix = getConfig().getTokenPrefix();
|
||||
if(SaFoxUtil.isEmpty(tokenPrefix) == false) {
|
||||
// 如果token并没有按照指定的前缀开头,则视为未提供token
|
||||
if(SaFoxUtil.isEmpty(tokenValue) || tokenValue.startsWith(tokenPrefix + SaTokenConsts.TOKEN_CONNECTOR_CHAT) == false) {
|
||||
tokenValue = null;
|
||||
} else {
|
||||
// 则裁剪掉前缀
|
||||
tokenValue = tokenValue.substring(tokenPrefix.length() + SaTokenConsts.TOKEN_CONNECTOR_CHAT.length());
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 返回
|
||||
// 5. 返回
|
||||
return tokenValue;
|
||||
}
|
||||
|
||||
@@ -1599,7 +1634,8 @@ public class StpLogic {
|
||||
* @return key
|
||||
*/
|
||||
public String splicingKeyJustCreatedSave() {
|
||||
return SaTokenConsts.JUST_CREATED_SAVE_KEY + loginType;
|
||||
// return SaTokenConsts.JUST_CREATED_SAVE_KEY + loginType;
|
||||
return SaTokenConsts.JUST_CREATED;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -51,6 +51,14 @@ public class StpUtil {
|
||||
return stpLogic.getTokenName();
|
||||
}
|
||||
|
||||
/**
|
||||
* 在当前会话写入当前TokenValue
|
||||
* @param tokenValue token值
|
||||
*/
|
||||
public static void setTokenValue(String tokenValue){
|
||||
stpLogic.setTokenValue(tokenValue);
|
||||
}
|
||||
|
||||
/**
|
||||
* 在当前会话写入当前TokenValue
|
||||
* @param tokenValue token值
|
||||
@@ -68,6 +76,14 @@ public class StpUtil {
|
||||
return stpLogic.getTokenValue();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前TokenValue (不裁剪前缀)
|
||||
* @return /
|
||||
*/
|
||||
public static String getTokenValueNotCut(){
|
||||
return stpLogic.getTokenValueNotCut();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取当前会话的Token信息
|
||||
* @return token信息
|
||||
|
@@ -30,7 +30,12 @@ public class SaTokenConsts {
|
||||
/**
|
||||
* 常量key标记: 如果token为本次请求新创建的,则以此字符串为key存储在当前request中
|
||||
*/
|
||||
public static final String JUST_CREATED_SAVE_KEY = "JUST_CREATED_SAVE_KEY_";
|
||||
public static final String JUST_CREATED = "JUST_CREATED_";
|
||||
|
||||
/**
|
||||
* 常量key标记: 如果token为本次请求新创建的,则以此字符串为key存储在当前request中(不拼接前缀,纯Token)
|
||||
*/
|
||||
public static final String JUST_CREATED_NOT_PREFIX = "JUST_CREATED_NOT_PREFIX_";
|
||||
|
||||
/**
|
||||
* 常量key标记: 如果本次请求已经验证过[无操作过期], 则以此值存储在当前request中
|
||||
@@ -97,5 +102,14 @@ public class SaTokenConsts {
|
||||
* 切面、拦截器、过滤器等各种组件的注册优先级顺序
|
||||
*/
|
||||
public static final int ASSEMBLY_ORDER = -100;
|
||||
|
||||
|
||||
// =================== 废弃 ===================
|
||||
|
||||
/**
|
||||
* 请更换为 JUST_CREATED
|
||||
*/
|
||||
@Deprecated
|
||||
public static final String JUST_CREATED_SAVE_KEY = JUST_CREATED;
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user