mirror of
https://gitee.com/dromara/sa-token.git
synced 2024-12-02 20:08:08 +08:00
Merge branch 'dev' of https://gitee.com/dromara/sa-token into dev
This commit is contained in:
commit
81c1ddacde
@ -236,30 +236,30 @@ public class SaCookie {
|
||||
if(SaFoxUtil.isEmpty(name)) {
|
||||
throw new SaTokenException("name不能为空").setCode(SaErrorCode.CODE_12002);
|
||||
}
|
||||
if(value != null && value.indexOf(";") > -1) {
|
||||
if(value != null && value.contains(";")) {
|
||||
throw new SaTokenException("无效Value:" + value).setCode(SaErrorCode.CODE_12003);
|
||||
}
|
||||
|
||||
// Set-Cookie: name=value; Max-Age=100000; Expires=Tue, 05-Oct-2021 20:28:17 GMT; Domain=localhost; Path=/; Secure; HttpOnly; SameSite=Lax
|
||||
|
||||
StringBuffer sb = new StringBuffer();
|
||||
sb.append(name + "=" + value);
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(name).append("=").append(value);
|
||||
|
||||
if(maxAge >= 0) {
|
||||
sb.append("; Max-Age=" + maxAge);
|
||||
sb.append("; Max-Age=").append(maxAge);
|
||||
String expires;
|
||||
if(maxAge == 0) {
|
||||
expires = Instant.EPOCH.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.RFC_1123_DATE_TIME);
|
||||
} else {
|
||||
expires = OffsetDateTime.now().plusSeconds(maxAge).format(DateTimeFormatter.RFC_1123_DATE_TIME);
|
||||
}
|
||||
sb.append("; Expires=" + expires);
|
||||
sb.append("; Expires=").append(expires);
|
||||
}
|
||||
if(!SaFoxUtil.isEmpty(domain)) {
|
||||
sb.append("; Domain=" + domain);
|
||||
sb.append("; Domain=").append(domain);
|
||||
}
|
||||
if(!SaFoxUtil.isEmpty(path)) {
|
||||
sb.append("; Path=" + path);
|
||||
sb.append("; Path=").append(path);
|
||||
}
|
||||
if(secure) {
|
||||
sb.append("; Secure");
|
||||
@ -268,7 +268,7 @@ public class SaCookie {
|
||||
sb.append("; HttpOnly");
|
||||
}
|
||||
if(!SaFoxUtil.isEmpty(sameSite)) {
|
||||
sb.append("; SameSite=" + sameSite);
|
||||
sb.append("; SameSite=").append(sameSite);
|
||||
}
|
||||
|
||||
return sb.toString();
|
||||
|
@ -1,5 +1,6 @@
|
||||
package cn.dev33.satoken.secure;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.KeyFactory;
|
||||
import java.security.KeyPair;
|
||||
import java.security.KeyPairGenerator;
|
||||
@ -112,7 +113,7 @@ public class SaSecureUtil {
|
||||
try {
|
||||
str = (str == null ? "" : str);
|
||||
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
|
||||
messageDigest.update(str.getBytes("UTF-8"));
|
||||
messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
byte[] bytes = messageDigest.digest();
|
||||
StringBuilder builder = new StringBuilder();
|
||||
@ -159,7 +160,7 @@ public class SaSecureUtil {
|
||||
public static String aesEncrypt(String key, String text) {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
|
||||
byte[] byteContent = text.getBytes("utf-8");
|
||||
byte[] byteContent = text.getBytes(StandardCharsets.UTF_8);
|
||||
cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
|
||||
byte[] result = cipher.doFinal(byteContent);
|
||||
return encoder.encodeToString(result);
|
||||
@ -179,7 +180,7 @@ public class SaSecureUtil {
|
||||
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
|
||||
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
|
||||
byte[] result = cipher.doFinal(decoder.decode(text));
|
||||
return new String(result, "utf-8");
|
||||
return new String(result, StandardCharsets.UTF_8);
|
||||
} catch (Exception e) {
|
||||
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12115);
|
||||
}
|
||||
@ -249,11 +250,11 @@ public class SaSecureUtil {
|
||||
// 该密钥能够加密的最大字节长度
|
||||
int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8 - 11;
|
||||
byte[][] arrays = splitBytes(content.getBytes(), splitLength);
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (byte[] array : arrays) {
|
||||
stringBuffer.append(bytesToHexString(cipher.doFinal(array)));
|
||||
stringBuilder.append(bytesToHexString(cipher.doFinal(array)));
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
return stringBuilder.toString();
|
||||
} catch (Exception e) {
|
||||
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12116);
|
||||
}
|
||||
@ -274,11 +275,11 @@ public class SaSecureUtil {
|
||||
// 该密钥能够加密的最大字节长度
|
||||
int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8 - 11;
|
||||
byte[][] arrays = splitBytes(content.getBytes(), splitLength);
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (byte[] array : arrays) {
|
||||
stringBuffer.append(bytesToHexString(cipher.doFinal(array)));
|
||||
stringBuilder.append(bytesToHexString(cipher.doFinal(array)));
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
return stringBuilder.toString();
|
||||
} catch (Exception e) {
|
||||
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12117);
|
||||
}
|
||||
@ -301,11 +302,11 @@ public class SaSecureUtil {
|
||||
int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8;
|
||||
byte[] contentBytes = hexStringToBytes(content);
|
||||
byte[][] arrays = splitBytes(contentBytes, splitLength);
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (byte[] array : arrays) {
|
||||
stringBuffer.append(new String(cipher.doFinal(array)));
|
||||
stringBuilder.append(new String(cipher.doFinal(array)));
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
return stringBuilder.toString();
|
||||
} catch (Exception e) {
|
||||
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12118);
|
||||
}
|
||||
@ -327,11 +328,11 @@ public class SaSecureUtil {
|
||||
int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8;
|
||||
byte[] contentBytes = hexStringToBytes(content);
|
||||
byte[][] arrays = splitBytes(contentBytes, splitLength);
|
||||
StringBuffer stringBuffer = new StringBuffer();
|
||||
StringBuilder stringBuilder = new StringBuilder();
|
||||
for (byte[] array : arrays) {
|
||||
stringBuffer.append(new String(cipher.doFinal(array)));
|
||||
stringBuilder.append(new String(cipher.doFinal(array)));
|
||||
}
|
||||
return stringBuffer.toString();
|
||||
return stringBuilder.toString();
|
||||
} catch (Exception e) {
|
||||
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12119);
|
||||
}
|
||||
@ -352,9 +353,7 @@ public class SaSecureUtil {
|
||||
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
|
||||
|
||||
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
|
||||
|
||||
return publicKey;
|
||||
return keyFactory.generatePublic(x509KeySpec);
|
||||
}
|
||||
|
||||
/** 根据私钥字符串获取 私钥对象 */
|
||||
@ -369,9 +368,7 @@ public class SaSecureUtil {
|
||||
|
||||
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
|
||||
|
||||
PrivateKey privateKey = keyFactory.generatePrivate(x509KeySpec);
|
||||
|
||||
return privateKey;
|
||||
return keyFactory.generatePrivate(x509KeySpec);
|
||||
}
|
||||
|
||||
|
||||
|
@ -58,7 +58,7 @@ public class SaFoxUtil {
|
||||
*/
|
||||
public static String getRandomString(int length) {
|
||||
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
|
||||
StringBuffer sb = new StringBuffer();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < length; i++) {
|
||||
int number = ThreadLocalRandom.current().nextInt(62);
|
||||
sb.append(str.charAt(number));
|
||||
@ -113,7 +113,6 @@ public class SaFoxUtil {
|
||||
public static boolean notEquals(Object a, Object b) {
|
||||
return !equals(a, b);
|
||||
}
|
||||
|
||||
/**
|
||||
* 以当前时间戳和随机int数字拼接一个随机字符串
|
||||
*
|
||||
|
@ -21,7 +21,7 @@
|
||||
<jackson-datatype-jsr310.version>2.11.2</jackson-datatype-jsr310.version>
|
||||
<servlet-api.version>3.1.0</servlet-api.version>
|
||||
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
|
||||
<solon.version>1.10.13</solon.version>
|
||||
<solon.version>1.12.0</solon.version>
|
||||
<noear-redisx.version>1.4.4</noear-redisx.version>
|
||||
<jfinal.version>4.9.17</jfinal.version>
|
||||
<jboot.version>3.14.4</jboot.version>
|
||||
|
@ -6,7 +6,7 @@ SaSession-会话对象,专业数据缓存组件。
|
||||
|
||||
### 1、常量
|
||||
``` java
|
||||
SaSession.ROLE_LIST = "USER"; // 在 Session 上存储用户对象时建议使用的key
|
||||
SaSession.USER= "USER"; // 在 Session 上存储用户对象时建议使用的key
|
||||
SaSession.ROLE_LIST = "ROLE_LIST"; // 在 Session 上存储角色时建议使用的key
|
||||
SaSession.PERMISSION_LIST = "PERMISSION_LIST"; // 在 Session 上存储权限时建议使用的key
|
||||
```
|
||||
|
@ -26,7 +26,7 @@ dao.updateTimeout(key, timeout); // 修改Value的剩余存活时间 (单位:
|
||||
``` java
|
||||
dao.getObject(key); // 获取Object,如无返空
|
||||
dao.setObject(key, value, timeout); // 写入Object,并设定存活时间 (单位: 秒)
|
||||
dao.setObject(key, value); // 更新Object (过期时间不变)
|
||||
dao.updateObject(key, value); // 更新Object (过期时间不变)
|
||||
dao.deleteObject(key); // 删除Object
|
||||
dao.getObjectTimeout(key); // 获取Object的剩余存活时间 (单位: 秒)
|
||||
dao.updateObjectTimeout(key, timeout); // 修改Object的剩余存活时间 (单位: 秒)
|
||||
|
@ -13,7 +13,7 @@ Sa-Token 中的基础异常类是 `SaTokenException`,在此基础上,又针
|
||||
|
||||
``` java
|
||||
if(SaFoxUtil.isUrl(url) == false) {
|
||||
throw new SaSsoException("无效redirect:" + url).setCode(SaSsoExceptionCode.CODE_20001);
|
||||
throw new SaSsoException("无效redirect:" + url).setCode(SaSsoErrorCode.CODE_30001);
|
||||
}
|
||||
```
|
||||
|
||||
@ -28,13 +28,13 @@ public class GlobalExceptionHandler {
|
||||
public SaResult handlerSaTokenException(SaTokenException e) {
|
||||
|
||||
// 根据不同异常细分状态码返回不同的提示
|
||||
if(e.getCode() == 20001) {
|
||||
if(e.getCode() == 30001) {
|
||||
return SaResult.error("redirect 重定向 url 是一个无效地址");
|
||||
}
|
||||
if(e.getCode() == 20002) {
|
||||
if(e.getCode() == 30002) {
|
||||
return SaResult.error("redirect 重定向 url 不在 allowUrl 允许的范围内");
|
||||
}
|
||||
if(e.getCode() == 20004) {
|
||||
if(e.getCode() == 30004) {
|
||||
return SaResult.error("提供的 ticket 是无效的");
|
||||
}
|
||||
// 更多 code 码判断 ...
|
||||
|
@ -90,7 +90,6 @@ public class SaTokenConfigure {
|
||||
// 此配置会与 application.yml 中的配置合并 (代码配置优先)
|
||||
@Autowired
|
||||
public void configSaToken(SaTokenConfig config) {
|
||||
SaTokenConfig config = new SaTokenConfig();
|
||||
config.setTokenName("satoken"); // token名称 (同时也是cookie名称)
|
||||
config.setTimeout(30 * 24 * 60 * 60); // token有效期,单位s 默认30天
|
||||
config.setActivityTimeout(-1); // token临时有效期 (指定时间内无操作就视为token过期) 单位: 秒
|
||||
|
@ -69,8 +69,8 @@ public class XPluginImp implements Plugin {
|
||||
SaManager.setSaTokenSecondContext(bean.create());
|
||||
});
|
||||
|
||||
// 注入侦听器 Bean
|
||||
context.subBean(SaTokenListener.class, sl -> {
|
||||
// 注入侦听器 Bean (可以有多个)
|
||||
context.subBeansOfType(SaTokenListener.class, sl -> {
|
||||
SaTokenEventCenter.registerListener(sl);
|
||||
});
|
||||
|
||||
@ -115,8 +115,8 @@ public class XPluginImp implements Plugin {
|
||||
SaManager.setSaSignTemplate(bean);
|
||||
});
|
||||
|
||||
// 自定义 StpLogic 对象
|
||||
context.getBeanAsync(StpLogic.class, bean -> {
|
||||
// 自定义 StpLogic 对象(可以有多个)
|
||||
context.subBeansOfType(StpLogic.class, bean -> {
|
||||
StpUtil.setStpLogic(bean);
|
||||
});
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user