From e40d6fcfe259bacd8cdb549f0f3b302bb1852e8c Mon Sep 17 00:00:00 2001 From: dongzeliang <2746733890@qq.com> Date: Wed, 26 Feb 2025 22:08:29 +0800 Subject: [PATCH] =?UTF-8?q?fix():=E6=A0=BC=E5=BC=8F=E8=A7=84=E8=8C=83?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/muyu/common/core/text/Convert.java | 6 +- .../com/muyu/common/core/utils/JwtUtils.java | 4 +- .../muyu/common/core/utils/ip/IpUtils.java | 45 ++++++------ .../muyu/common/core/utils/sign/Base64.java | 68 +++++++++---------- 4 files changed, 61 insertions(+), 62 deletions(-) diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/text/Convert.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/text/Convert.java index 5b4378d..21b5310 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/text/Convert.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/text/Convert.java @@ -798,8 +798,8 @@ public class Convert { * * @return 全角字符串. */ - public static String toSBC (String input) { - return toSBC(input, null); + public static String toSbc(String input) { + return toSbc(input, null); } /** @@ -810,7 +810,7 @@ public class Convert { * * @return 全角字符串. */ - public static String toSBC (String input, Set notConvertSet) { + public static String toSbc(String input, Set notConvertSet) { char[] c = input.toCharArray(); for (int i = 0 ; i < c.length ; i++) { if (null != notConvertSet && notConvertSet.contains(c[i])) { diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/JwtUtils.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/JwtUtils.java index 62816cb..a0b3430 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/JwtUtils.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/JwtUtils.java @@ -28,11 +28,11 @@ public class JwtUtils { * 一旦客户端得知这个secret, 那就意味着客户端是可以自我签发jwt了。 * 应该大于等于 256位(长度32及以上的字符串),并且是随机的字符串 */ - private final static String secret = TokenConstants.SECRET; + private final static String SECRET = TokenConstants.SECRET; /** * 秘钥实例 */ - public static final SecretKey KEY = Keys.hmacShaKeyFor(secret.getBytes()); + public static final SecretKey KEY = Keys.hmacShaKeyFor(SECRET.getBytes()); /** * jwt签发者 */ diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/ip/IpUtils.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/ip/IpUtils.java index 774ec54..f17cb71 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/ip/IpUtils.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/ip/IpUtils.java @@ -17,7 +17,7 @@ public class IpUtils { // 匹配 ip public final static String REGX_IP = "((" + REGX_0_255 + "\\.){3}" + REGX_0_255 + ")"; // 匹配网段 - public final static String REGX_IP_SEG = "(" + REGX_IP + "\\-" + REGX_IP + ")"; + public final static String REGX_IP_SEG = "(" + REGX_IP + "-" + REGX_IP + ")"; public final static String REGX_IP_WILDCARD = "(((\\*\\.){3}\\*)|(" + REGX_0_255 + "(\\.\\*){3})|(" + REGX_0_255 + "\\." + REGX_0_255 + ")(\\.\\*){2}" + "|((" + REGX_0_255 + "\\.){3}\\*))"; /** @@ -41,20 +41,20 @@ public class IpUtils { return "unknown"; } String ip = request.getHeader("x-forwarded-for"); - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("Proxy-Client-IP"); } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Forwarded-For"); } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("WL-Proxy-Client-IP"); } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getHeader("X-Real-IP"); } - if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) { + if (ip == null || ip.isEmpty() || "unknown".equalsIgnoreCase(ip)) { ip = request.getRemoteAddr(); } @@ -87,25 +87,24 @@ public class IpUtils { final byte b0 = addr[0]; final byte b1 = addr[1]; // 10.x.x.x/8 - final byte SECTION_1 = 0x0A; + final byte section1 = 0x0A; // 172.16.x.x/12 - final byte SECTION_2 = (byte) 0xAC; - final byte SECTION_3 = (byte) 0x10; - final byte SECTION_4 = (byte) 0x1F; + final byte section2 = (byte) 0xAC; + final byte section3 = (byte) 0x10; + final byte section4 = (byte) 0x1F; // 192.168.x.x/16 - final byte SECTION_5 = (byte) 0xC0; - final byte SECTION_6 = (byte) 0xA8; + final byte section5 = (byte) 0xC0; + final byte section6 = (byte) 0xA8; switch (b0) { - case SECTION_1: + case section1: return true; - case SECTION_2: - if (b1 >= SECTION_3 && b1 <= SECTION_4) { + case section2: + if (b1 >= section3 && b1 <= section4) { return true; } - case SECTION_5: - switch (b1) { - case SECTION_6: - return true; + case section5: + if (b1 == section6) { + return true; } default: return false; @@ -120,7 +119,7 @@ public class IpUtils { * @return byte 字节 */ public static byte[] textToNumericFormatV4 (String text) { - if (text.length() == 0) { + if (text.isEmpty()) { return null; } @@ -195,7 +194,7 @@ public class IpUtils { public static String getHostIp () { try { return InetAddress.getLocalHost().getHostAddress(); - } catch (UnknownHostException e) { + } catch (UnknownHostException ignored) { } return "127.0.0.1"; } @@ -208,7 +207,7 @@ public class IpUtils { public static String getHostName () { try { return InetAddress.getLocalHost().getHostName(); - } catch (UnknownHostException e) { + } catch (UnknownHostException ignored) { } return "未知"; } @@ -225,7 +224,7 @@ public class IpUtils { if (ip != null && ip.indexOf(",") > 0) { final String[] ips = ip.trim().split(","); for (String subIp : ips) { - if (false == isUnknown(subIp)) { + if (!isUnknown(subIp)) { ip = subIp; break; } diff --git a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/sign/Base64.java b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/sign/Base64.java index ebc0599..f12cbd6 100644 --- a/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/sign/Base64.java +++ b/cloud-common/cloud-common-core/src/main/java/com/muyu/common/core/utils/sign/Base64.java @@ -1,5 +1,7 @@ package com.muyu.common.core.utils.sign; +import java.util.Arrays; + /** * Base64工具类 * @@ -14,40 +16,38 @@ public final class Base64 { static private final int FOURBYTE = 4; static private final int SIGN = -128; static private final char PAD = '='; - static final private byte[] base64Alphabet = new byte[BASELENGTH]; - static final private char[] lookUpBase64Alphabet = new char[LOOKUPLENGTH]; + static final private byte[] BASE_64_ALPHABET = new byte[BASELENGTH]; + static final private char[] LOOK_UP_BASE_64_ALPHABET = new char[LOOKUPLENGTH]; static { - for (int i = 0 ; i < BASELENGTH ; ++i) { - base64Alphabet[i] = -1; - } + Arrays.fill(BASE_64_ALPHABET, (byte) -1); for (int i = 'Z' ; i >= 'A' ; i--) { - base64Alphabet[i] = (byte) (i - 'A'); + BASE_64_ALPHABET[i] = (byte) (i - 'A'); } for (int i = 'z' ; i >= 'a' ; i--) { - base64Alphabet[i] = (byte) (i - 'a' + 26); + BASE_64_ALPHABET[i] = (byte) (i - 'a' + 26); } for (int i = '9' ; i >= '0' ; i--) { - base64Alphabet[i] = (byte) (i - '0' + 52); + BASE_64_ALPHABET[i] = (byte) (i - '0' + 52); } - base64Alphabet['+'] = 62; - base64Alphabet['/'] = 63; + BASE_64_ALPHABET['+'] = 62; + BASE_64_ALPHABET['/'] = 63; for (int i = 0 ; i <= 25 ; i++) { - lookUpBase64Alphabet[i] = (char) ('A' + i); + LOOK_UP_BASE_64_ALPHABET[i] = (char) ('A' + i); } for (int i = 26, j = 0 ; i <= 51 ; i++, j++) { - lookUpBase64Alphabet[i] = (char) ('a' + j); + LOOK_UP_BASE_64_ALPHABET[i] = (char) ('a' + j); } for (int i = 52, j = 0 ; i <= 61 ; i++, j++) { - lookUpBase64Alphabet[i] = (char) ('0' + j); + LOOK_UP_BASE_64_ALPHABET[i] = (char) ('0' + j); } - lookUpBase64Alphabet[62] = (char) '+'; - lookUpBase64Alphabet[63] = (char) '/'; + LOOK_UP_BASE_64_ALPHABET[62] = '+'; + LOOK_UP_BASE_64_ALPHABET[63] = '/'; } private static boolean isWhiteSpace (char octect) { @@ -59,7 +59,7 @@ public final class Base64 { } private static boolean isData (char octect) { - return (octect < BASELENGTH && base64Alphabet[octect] != -1); + return (octect < BASELENGTH && BASE_64_ALPHABET[octect] != -1); } /** @@ -101,10 +101,10 @@ public final class Base64 { byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc); - encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; - encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; - encodedData[encodedIndex++] = lookUpBase64Alphabet[(l << 2) | val3]; - encodedData[encodedIndex++] = lookUpBase64Alphabet[b3 & 0x3f]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[val1]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[val2 | (k << 4)]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[(l << 2) | val3]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[b3 & 0x3f]; } // form integral number of 6-bit groups @@ -112,8 +112,8 @@ public final class Base64 { b1 = binaryData[dataIndex]; k = (byte) (b1 & 0x03); byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); - encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; - encodedData[encodedIndex++] = lookUpBase64Alphabet[k << 4]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[val1]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[k << 4]; encodedData[encodedIndex++] = PAD; encodedData[encodedIndex++] = PAD; } else if (fewerThan24bits == SIXTEENBIT) { @@ -125,9 +125,9 @@ public final class Base64 { byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); - encodedData[encodedIndex++] = lookUpBase64Alphabet[val1]; - encodedData[encodedIndex++] = lookUpBase64Alphabet[val2 | (k << 4)]; - encodedData[encodedIndex++] = lookUpBase64Alphabet[l << 2]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[val1]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[val2 | (k << 4)]; + encodedData[encodedIndex++] = LOOK_UP_BASE_64_ALPHABET[l << 2]; encodedData[encodedIndex++] = PAD; } return new String(encodedData); @@ -176,10 +176,10 @@ public final class Base64 { return null; } // if found "no data" just return null - b1 = base64Alphabet[d1]; - b2 = base64Alphabet[d2]; - b3 = base64Alphabet[d3]; - b4 = base64Alphabet[d4]; + b1 = BASE_64_ALPHABET[d1]; + b2 = BASE_64_ALPHABET[d2]; + b3 = BASE_64_ALPHABET[d3]; + b4 = BASE_64_ALPHABET[d4]; decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); @@ -191,8 +191,8 @@ public final class Base64 { return null; } - b1 = base64Alphabet[d1]; - b2 = base64Alphabet[d2]; + b1 = BASE_64_ALPHABET[d1]; + b2 = BASE_64_ALPHABET[d2]; d3 = base64Data[dataIndex++]; d4 = base64Data[dataIndex++]; @@ -209,7 +209,7 @@ public final class Base64 { tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); return tmp; } else if (!isPad(d3) && isPad(d4)) { - b3 = base64Alphabet[d3]; + b3 = BASE_64_ALPHABET[d3]; // 最后2位应为零 if ((b3 & 0x3) != 0) { @@ -225,8 +225,8 @@ public final class Base64 { } } else { // 无衬垫 e.g 3cQl - b3 = base64Alphabet[d3]; - b4 = base64Alphabet[d4]; + b3 = BASE_64_ALPHABET[d3]; + b4 = BASE_64_ALPHABET[d4]; decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); decodedData[encodedIndex++] = (byte) (b3 << 6 | b4);