mirror of
https://gitee.com/dromara/sa-token.git
synced 2024-12-02 03:47:50 +08:00
升级模糊匹配算法
This commit is contained in:
parent
f162834ded
commit
45a77612ef
@ -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];
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -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() {
|
||||
|
Loading…
Reference in New Issue
Block a user