From a56a63ca781ae27ab6a9e6d81c6516b1b45dea3c Mon Sep 17 00:00:00 2001 From: OTTO <731554297@qq.com> Date: Wed, 22 Jan 2025 11:15:33 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E4=BA=A7=E5=93=81=E5=BA=93=E4=BC=98?= =?UTF-8?q?=E5=8C=96):=20=E4=BF=AE=E5=A4=8D=E4=BA=A7=E5=93=81=E5=BA=93?= =?UTF-8?q?=E7=A7=BB=E5=85=A5=E5=88=B6=E5=93=81=E6=97=B6=E8=8B=A5=E5=88=B6?= =?UTF-8?q?=E5=93=81=E4=B8=BA=E4=B8=AD=E6=96=87=E4=BC=9A=E8=A2=ABurlEncode?= =?UTF-8?q?=E4=B8=A4=E6=AC=A1=E5=AF=BC=E8=87=B4=E4=B8=AD=E6=96=87=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=AD=A3=E5=B8=B8=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复方案:urlEncode前判断字符串是否已经被urlEncode过 1. 使用URLDecoder.decode对字符串进行解码。 2. 再使用URLEncoder.encode对解码后的字符串重新编码。 3. 如果重新编码后的字符串与原始字符串相同,则说明原始字符串是经过URL编码的 Signed-off-by: OTTO <731554297@qq.com> --- .../common/core/utils/ServletUtils.java | 22 ++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/microservices-common/microservices-common-core/src/main/java/com/microservices/common/core/utils/ServletUtils.java b/microservices-common/microservices-common-core/src/main/java/com/microservices/common/core/utils/ServletUtils.java index 2ef6f6350..dbe4225ed 100644 --- a/microservices-common/microservices-common-core/src/main/java/com/microservices/common/core/utils/ServletUtils.java +++ b/microservices-common/microservices-common-core/src/main/java/com/microservices/common/core/utils/ServletUtils.java @@ -228,7 +228,11 @@ public class ServletUtils { StringBuilder stringBuffer = new StringBuilder(); String[] strings = str.split("/"); for (String string : strings) { - stringBuffer.append("/").append(URLEncoder.encode(string, Constants.UTF8)); + // 如果url已经进行过urlEncode 不重复进行Encode + if (!isUrlEncoded(string)) { + string = URLEncoder.encode(string, Constants.UTF8); + } + stringBuffer.append("/").append(string); } return stringBuffer.toString().replaceAll("\\+", "%20"); } catch (UnsupportedEncodingException e) { @@ -236,6 +240,22 @@ public class ServletUtils { } } + /** + * 判断字符串是否经过URLEncode + * 1. 使用URLDecoder.decode对字符串进行解码。 + * 2. 再使用URLEncoder.encode对解码后的字符串重新编码。 + * 3. 如果重新编码后的字符串与原始字符串相同,则说明原始字符串是经过URL编码的 + * + * @param encodedString 需encode的字符串 + * @return 是否经过URLEncode + * @throws UnsupportedEncodingException 编码异常 + */ + public static boolean isUrlEncoded(String encodedString) throws UnsupportedEncodingException { + String decodedString = URLDecoder.decode(encodedString, "UTF-8"); + String reencodedString = URLEncoder.encode(decodedString, "UTF-8"); + return encodedString.equalsIgnoreCase(reencodedString); + } + /** * 内容解码 *