mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
ServletPathMatcher 增加包含*的模式匹配 pattern: /druid/*/index.html
source:/druid/admin/index.html
This commit is contained in:
@@ -17,45 +17,56 @@ package com.alibaba.druid.util;
|
||||
|
||||
public class ServletPathMatcher implements PatternMatcher {
|
||||
private final static ServletPathMatcher instance = new ServletPathMatcher();
|
||||
|
||||
|
||||
public static ServletPathMatcher getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* <p>
|
||||
* three type: endsWithMatch(eg. /xxx*=/xxx/xyz), startsWithMatch(eg. *.xxx=abc.xxx), equals(eg. /xxx=/xxx).
|
||||
* </p>
|
||||
* <b>Notice</b>: *xxx* will match *xxxyyyy. endsWithMatch first.
|
||||
*/
|
||||
@Override
|
||||
public boolean matches(String pattern, String source) {
|
||||
if (pattern == null || source == null) {
|
||||
return false;
|
||||
}
|
||||
pattern = pattern.trim();
|
||||
source = source.trim();
|
||||
if (pattern.endsWith("*")) {
|
||||
// pattern: /druid* source:/druid/index.html
|
||||
int length = pattern.length() - 1;
|
||||
if (source.length() >= length) {
|
||||
if (pattern.substring(0, length).equals(source.substring(0, length))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else if (pattern.startsWith("*")) {
|
||||
// pattern: *.html source:/xx/xx.html
|
||||
int length = pattern.length() - 1;
|
||||
if (source.length() >= length && source.endsWith(pattern.substring(1))) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// pattern: /druid/index.html source:/druid/index.html
|
||||
if (pattern.equals(source)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
/**
|
||||
* <p>
|
||||
* three type: endsWithMatch(eg. /xxx*=/xxx/xyz), startsWithMatch(eg.
|
||||
* *.xxx=abc.xxx), equals(eg. /xxx=/xxx).
|
||||
* </p>
|
||||
* <b>Notice</b>: *xxx* will match *xxxyyyy. endsWithMatch first.
|
||||
*/
|
||||
@Override
|
||||
public boolean matches(String pattern, String source) {
|
||||
if (pattern == null || source == null) {
|
||||
return false;
|
||||
}
|
||||
pattern = pattern.trim();
|
||||
source = source.trim();
|
||||
if (pattern.endsWith("*")) {
|
||||
// pattern: /druid* source:/druid/index.html
|
||||
int length = pattern.length() - 1;
|
||||
if (source.length() >= length) {
|
||||
if (pattern.substring(0, length).equals(
|
||||
source.substring(0, length))) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
} else if (pattern.startsWith("*")) {
|
||||
// pattern: *.html source:/xx/xx.html
|
||||
int length = pattern.length() - 1;
|
||||
if (source.length() >= length
|
||||
&& source.endsWith(pattern.substring(1))) {
|
||||
return true;
|
||||
}
|
||||
} else if (pattern.contains("*")) {
|
||||
// pattern: /druid/*/index.html source:/druid/admin/index.html
|
||||
int start = pattern.indexOf("*");
|
||||
int end = pattern.lastIndexOf("*");
|
||||
if (source.startsWith(pattern.substring(0, start))
|
||||
&& source.endsWith(pattern.substring(end + 1))) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
// pattern: /druid/index.html source:/druid/index.html
|
||||
if (pattern.equals(source)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user