diff --git a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java index 925968df..a28a1072 100644 --- a/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java +++ b/sa-token-core/src/main/java/cn/dev33/satoken/util/SaFoxUtil.java @@ -251,8 +251,39 @@ public class SaFoxUtil { if( ! patt.contains("*")) { return patt.equals(str); } - // 正则匹配 - return Pattern.matches(patt.replace(".*", "\\..*"), str); + // 深入匹配 + return vagueMatchMethod(patt, str); + } + + /** + * 字符串模糊匹配 + * + * @param pattern / + * @param str / + * @return / + */ + private static boolean vagueMatchMethod( String pattern, String str) { + int m = str.length(); + int n = pattern.length(); + boolean[][] dp = new boolean[m + 1][n + 1]; + dp[0][0] = true; + for (int i = 1; i <= n; ++i) { + if (pattern.charAt(i - 1) == '*') { + dp[0][i] = true; + } else { + break; + } + } + for (int i = 1; i <= m; ++i) { + for (int j = 1; j <= n; ++j) { + if (pattern.charAt(j - 1) == '*') { + dp[i][j] = dp[i][j - 1] || dp[i - 1][j]; + } else if (str.charAt(i - 1) == pattern.charAt(j - 1)) { + dp[i][j] = dp[i - 1][j - 1]; + } + } + } + return dp[m][n]; } /** diff --git a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java index 45f742af..81551159 100644 --- a/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java +++ b/sa-token-test/sa-token-springboot-test/src/test/java/cn/dev33/satoken/core/util/SaFoxUtilTest.java @@ -113,16 +113,65 @@ public class SaFoxUtilTest { Assertions.assertEquals(list6.get(0), dataList.get(dataList.size() - 1)); } - @Test - public void vagueMatch() { - Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello")); - Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello world")); - Assertions.assertFalse(SaFoxUtil.vagueMatch("hello*", "he")); - Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello*")); - Assertions.assertTrue(SaFoxUtil.vagueMatch(null, null)); - Assertions.assertFalse(SaFoxUtil.vagueMatch(null, "hello")); - Assertions.assertFalse(SaFoxUtil.vagueMatch("hello*", null)); - } + @Test + public void vagueMatch() { + // 不模糊 + Assertions.assertTrue(SaFoxUtil.vagueMatch("hello", "hello")); + + // 正常模糊 + Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello world")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("hello*", "hello*")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("hello*", "he")); + + // 带 - + Assertions.assertTrue(SaFoxUtil.vagueMatch("user-*", "user-")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user-*", "user-add")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user-*", "user-*")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user-*", "user")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user-*-add-*", "user-xx-add-1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user-*-add-*", "user-add-1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user-*", "usermgt-list")); + + // 带 / + Assertions.assertTrue(SaFoxUtil.vagueMatch("user/*", "user/")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user/*", "user/add")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user/*", "user/*")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user/*", "user")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user/*/add/*", "user/xx/add/1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user/*/add/*", "user/add/1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user/*", "usermgt/list")); + + // 带 : + Assertions.assertTrue(SaFoxUtil.vagueMatch("user:*", "user:")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user:*", "user:add")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user:*", "user:*")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user:*", "user")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user:*:add:*", "user:xx:add:1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user:*:add:*", "user:add:1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user:*", "usermgt:list")); + + // 带 . + Assertions.assertTrue(SaFoxUtil.vagueMatch("user.*", "user.")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user.*", "user.add")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user.*", "user.*")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user.*", "user")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("user.*.add.*", "user.xx.add.1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user.*.add.*", "user.add.1")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("user.*", "usermgt.list")); + + // 极端情况 + Assertions.assertTrue(SaFoxUtil.vagueMatch(null, null)); + Assertions.assertFalse(SaFoxUtil.vagueMatch(null, "hello")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("hello*", null)); + + // url 匹配 + Assertions.assertTrue(SaFoxUtil.vagueMatch("*", "http://sa-sso-client1.com:9001/sso/login")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("http://sa-sso-client1.com:9001/*", "http://sa-sso-client1.com:9001/sso/login")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("http://sa-sso-client1.com:9001/*", "http://sa-sso-client1.com:9001/sso/login?name=1")); + Assertions.assertTrue(SaFoxUtil.vagueMatch("http://sa-sso-client1.com:9001/*", "http://sa-sso-client1.com:9001/sso/login?name=1&age=2")); + Assertions.assertFalse(SaFoxUtil.vagueMatch("http://sa-sso-client1.com:9001/*", "http://sa-sso-client1.com:9002")); + } @Test public void isWrapperType() {