mirror of
https://gitee.com/dromara/sa-token.git
synced 2025-05-04 20:57:56 +08:00
!196 StringBuffer替换为为StringBuilder
Merge pull request !196 from z.h.z/dev
This commit is contained in:
commit
6b8dcbc42f
@ -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));
|
||||
|
Loading…
Reference in New Issue
Block a user