!196 StringBuffer替换为为StringBuilder

Merge pull request !196 from z.h.z/dev
This commit is contained in:
孔明 2022-11-19 13:57:33 +00:00 committed by Gitee
commit 6b8dcbc42f
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
3 changed files with 226 additions and 229 deletions

View File

@ -236,30 +236,30 @@ public class SaCookie {
if(SaFoxUtil.isEmpty(name)) { if(SaFoxUtil.isEmpty(name)) {
throw new SaTokenException("name不能为空").setCode(SaErrorCode.CODE_12002); 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); 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 // 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(); StringBuilder sb = new StringBuilder();
sb.append(name + "=" + value); sb.append(name).append("=").append(value);
if(maxAge >= 0) { if(maxAge >= 0) {
sb.append("; Max-Age=" + maxAge); sb.append("; Max-Age=").append(maxAge);
String expires; String expires;
if(maxAge == 0) { if(maxAge == 0) {
expires = Instant.EPOCH.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.RFC_1123_DATE_TIME); expires = Instant.EPOCH.atOffset(ZoneOffset.UTC).format(DateTimeFormatter.RFC_1123_DATE_TIME);
} else { } else {
expires = OffsetDateTime.now().plusSeconds(maxAge).format(DateTimeFormatter.RFC_1123_DATE_TIME); expires = OffsetDateTime.now().plusSeconds(maxAge).format(DateTimeFormatter.RFC_1123_DATE_TIME);
} }
sb.append("; Expires=" + expires); sb.append("; Expires=").append(expires);
} }
if(!SaFoxUtil.isEmpty(domain)) { if(!SaFoxUtil.isEmpty(domain)) {
sb.append("; Domain=" + domain); sb.append("; Domain=").append(domain);
} }
if(!SaFoxUtil.isEmpty(path)) { if(!SaFoxUtil.isEmpty(path)) {
sb.append("; Path=" + path); sb.append("; Path=").append(path);
} }
if(secure) { if(secure) {
sb.append("; Secure"); sb.append("; Secure");
@ -268,7 +268,7 @@ public class SaCookie {
sb.append("; HttpOnly"); sb.append("; HttpOnly");
} }
if(!SaFoxUtil.isEmpty(sameSite)) { if(!SaFoxUtil.isEmpty(sameSite)) {
sb.append("; SameSite=" + sameSite); sb.append("; SameSite=").append(sameSite);
} }
return sb.toString(); return sb.toString();

View File

@ -1,5 +1,6 @@
package cn.dev33.satoken.secure; package cn.dev33.satoken.secure;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory; import java.security.KeyFactory;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.KeyPairGenerator; import java.security.KeyPairGenerator;
@ -112,7 +113,7 @@ public class SaSecureUtil {
try { try {
str = (str == null ? "" : str); str = (str == null ? "" : str);
MessageDigest messageDigest = MessageDigest.getInstance("SHA-256"); MessageDigest messageDigest = MessageDigest.getInstance("SHA-256");
messageDigest.update(str.getBytes("UTF-8")); messageDigest.update(str.getBytes(StandardCharsets.UTF_8));
byte[] bytes = messageDigest.digest(); byte[] bytes = messageDigest.digest();
StringBuilder builder = new StringBuilder(); StringBuilder builder = new StringBuilder();
@ -159,7 +160,7 @@ public class SaSecureUtil {
public static String aesEncrypt(String key, String text) { public static String aesEncrypt(String key, String text) {
try { try {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); 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)); cipher.init(Cipher.ENCRYPT_MODE, getSecretKey(key));
byte[] result = cipher.doFinal(byteContent); byte[] result = cipher.doFinal(byteContent);
return encoder.encodeToString(result); return encoder.encodeToString(result);
@ -179,7 +180,7 @@ public class SaSecureUtil {
Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM); Cipher cipher = Cipher.getInstance(DEFAULT_CIPHER_ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key)); cipher.init(Cipher.DECRYPT_MODE, getSecretKey(key));
byte[] result = cipher.doFinal(decoder.decode(text)); byte[] result = cipher.doFinal(decoder.decode(text));
return new String(result, "utf-8"); return new String(result, StandardCharsets.UTF_8);
} catch (Exception e) { } catch (Exception e) {
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12115); throw new SaTokenException(e).setCode(SaErrorCode.CODE_12115);
} }
@ -249,11 +250,11 @@ public class SaSecureUtil {
// 该密钥能够加密的最大字节长度 // 该密钥能够加密的最大字节长度
int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8 - 11; int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8 - 11;
byte[][] arrays = splitBytes(content.getBytes(), splitLength); byte[][] arrays = splitBytes(content.getBytes(), splitLength);
StringBuffer stringBuffer = new StringBuffer(); StringBuilder stringBuilder = new StringBuilder();
for (byte[] array : arrays) { 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) { } catch (Exception e) {
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12116); throw new SaTokenException(e).setCode(SaErrorCode.CODE_12116);
} }
@ -274,11 +275,11 @@ public class SaSecureUtil {
// 该密钥能够加密的最大字节长度 // 该密钥能够加密的最大字节长度
int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8 - 11; int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8 - 11;
byte[][] arrays = splitBytes(content.getBytes(), splitLength); byte[][] arrays = splitBytes(content.getBytes(), splitLength);
StringBuffer stringBuffer = new StringBuffer(); StringBuilder stringBuilder = new StringBuilder();
for (byte[] array : arrays) { 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) { } catch (Exception e) {
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12117); throw new SaTokenException(e).setCode(SaErrorCode.CODE_12117);
} }
@ -301,11 +302,11 @@ public class SaSecureUtil {
int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8; int splitLength = ((RSAPublicKey) publicKey).getModulus().bitLength() / 8;
byte[] contentBytes = hexStringToBytes(content); byte[] contentBytes = hexStringToBytes(content);
byte[][] arrays = splitBytes(contentBytes, splitLength); byte[][] arrays = splitBytes(contentBytes, splitLength);
StringBuffer stringBuffer = new StringBuffer(); StringBuilder stringBuilder = new StringBuilder();
for (byte[] array : arrays) { 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) { } catch (Exception e) {
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12118); throw new SaTokenException(e).setCode(SaErrorCode.CODE_12118);
} }
@ -327,11 +328,11 @@ public class SaSecureUtil {
int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8; int splitLength = ((RSAPrivateKey) privateKey).getModulus().bitLength() / 8;
byte[] contentBytes = hexStringToBytes(content); byte[] contentBytes = hexStringToBytes(content);
byte[][] arrays = splitBytes(contentBytes, splitLength); byte[][] arrays = splitBytes(contentBytes, splitLength);
StringBuffer stringBuffer = new StringBuffer(); StringBuilder stringBuilder = new StringBuilder();
for (byte[] array : arrays) { 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) { } catch (Exception e) {
throw new SaTokenException(e).setCode(SaErrorCode.CODE_12119); throw new SaTokenException(e).setCode(SaErrorCode.CODE_12119);
} }
@ -352,9 +353,7 @@ public class SaSecureUtil {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PublicKey publicKey = keyFactory.generatePublic(x509KeySpec); return keyFactory.generatePublic(x509KeySpec);
return publicKey;
} }
/** 根据私钥字符串获取 私钥对象 */ /** 根据私钥字符串获取 私钥对象 */
@ -369,9 +368,7 @@ public class SaSecureUtil {
KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM); KeyFactory keyFactory = KeyFactory.getInstance(ALGORITHM);
PrivateKey privateKey = keyFactory.generatePrivate(x509KeySpec); return keyFactory.generatePrivate(x509KeySpec);
return privateKey;
} }

View File

@ -58,7 +58,7 @@ public class SaFoxUtil {
*/ */
public static String getRandomString(int length) { public static String getRandomString(int length) {
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
StringBuffer sb = new StringBuffer(); StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) { for (int i = 0; i < length; i++) {
int number = ThreadLocalRandom.current().nextInt(62); int number = ThreadLocalRandom.current().nextInt(62);
sb.append(str.charAt(number)); sb.append(str.charAt(number));