浏览代码

class\xml加密支持正则路径匹配

(cherry picked from commit 7e62f9e9a61bb42abded713e390e49707c01e8ab)
wang 2 年之前
父节点
当前提交
cd4b570cc6

+ 5 - 5
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/executor/EncryptExecutor.java

@@ -344,12 +344,12 @@ public class EncryptExecutor {
                 })
                 .filter(zipEntryName -> {
                     if (excludeXmlPrefixSet != null) {
-                        boolean shouldExclude = excludeXmlPrefixSet.stream().anyMatch(zipEntryName::startsWith);
+                        boolean shouldExclude = excludeXmlPrefixSet.stream().anyMatch(s -> StrUtil.regMatched(s, zipEntryName));
                         if (shouldExclude) {
                             return false;
                         }
                     }
-                    return includeXmlPrefixSet.stream().anyMatch(zipEntryName::startsWith);
+                    return includeXmlPrefixSet.stream().anyMatch(s -> StrUtil.regMatched(s, zipEntryName));
                 })
                 .collect(Collectors.toSet());
         
@@ -943,7 +943,7 @@ public class EncryptExecutor {
                     String classLongName = JavassistUtil.resolveClassName(file.getAbsolutePath(), true);
                     if (excludePrefixSet != null) {
                         for (String excludePrefix : excludePrefixSet) {
-                            if (classLongName.startsWith(excludePrefix)) {
+                            if (StrUtil.regMatched(excludePrefix, classLongName)) {
                                 // 排除
                                 return null;
                             }
@@ -955,8 +955,8 @@ public class EncryptExecutor {
                             Pair<String, Map<String, String>> pair = extraPrefixAndParam(includePrefix);
                             String pureIncludePrefix = pair.getLeft();
                             Map<String, String> params = pair.getRight();
-                            
-                            if (classLongName.startsWith(pureIncludePrefix)) {
+
+                            if (StrUtil.regMatched(pureIncludePrefix, classLongName)) {
                                 // 包含
                                 return EncryptClassArgs.create(file,
                                         StrUtil.isTrueDefault(params.get(Constant.CLEAN_CLASS_ANNOTATION_PARAM_NAME), false),

+ 27 - 0
class-winter-core/src/main/java/winter/com/ideaaedi/classwinter/util/StrUtil.java

@@ -8,6 +8,7 @@ import java.nio.charset.StandardCharsets;
 import java.util.Arrays;
 import java.util.HashSet;
 import java.util.Set;
+import java.util.regex.Pattern;
 
 /**
  * 字符串工具类
@@ -178,4 +179,30 @@ public final class StrUtil {
         Arrays.fill(byteBuffer.array(), (byte) 0);
         return bytes;
     }
+
+    /**
+     * 获取正则表达式匹配次数
+     *
+     * @param patternStr
+     *              正则表达式字符串
+     * @param text
+     *              待匹配字符串
+     * @return 匹配次数
+     */
+    public static boolean regMatched(String patternStr, String text) {
+        return regMatched(Pattern.compile(patternStr), text);
+    }
+
+    /**
+     * 获取正则表达式匹配次数
+     *
+     * @param pattern
+     *            正则表达式匹配器
+     * @param text
+     *          待匹配字符串
+     * @return 匹配次数
+     */
+    public static boolean regMatched(Pattern pattern, String text) {
+        return pattern.matcher(text).find();
+    }
 }