This commit is contained in:
@@ -8,6 +8,10 @@ package org.redkale.net.http;
|
|||||||
import org.redkale.net.Filter;
|
import org.redkale.net.Filter;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* HTTP 过滤器 <br>
|
||||||
|
*
|
||||||
|
* <p>
|
||||||
|
* 详情见: https://redkale.org
|
||||||
*
|
*
|
||||||
* @author zhangjx
|
* @author zhangjx
|
||||||
*/
|
*/
|
||||||
|
|||||||
@@ -40,15 +40,15 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
|
|
||||||
private final Object excludeLock = new Object();
|
private final Object excludeLock = new Object();
|
||||||
|
|
||||||
private Map<String, BiPredicate<String, String>> excludeUrlMaps; //禁用的URL的正则表达式, 必须与 excludeUrlPredicates 保持一致
|
private Map<String, BiPredicate<String, String>> forbidURIMaps; //禁用的URL的正则表达式, 必须与 forbidURIPredicates 保持一致
|
||||||
|
|
||||||
private BiPredicate<String, String>[] excludeUrlPredicates; //禁用的URL的Predicate, 必须与 excludeUrlMaps 保持一致
|
private BiPredicate<String, String>[] forbidURIPredicates; //禁用的URL的Predicate, 必须与 forbidURIMaps 保持一致
|
||||||
|
|
||||||
public void addExcludeUrlReg(final String urlreg) {
|
public boolean addForbidURIReg(final String urlreg) {
|
||||||
if (urlreg == null || urlreg.isEmpty()) return;
|
if (urlreg == null || urlreg.isEmpty()) return false;
|
||||||
synchronized (excludeLock) {
|
synchronized (excludeLock) {
|
||||||
if (excludeUrlMaps != null && excludeUrlMaps.containsKey(urlreg)) return;
|
if (forbidURIMaps != null && forbidURIMaps.containsKey(urlreg)) return false;
|
||||||
if (excludeUrlMaps == null) excludeUrlMaps = new HashMap<>();
|
if (forbidURIMaps == null) forbidURIMaps = new HashMap<>();
|
||||||
String mapping = urlreg;
|
String mapping = urlreg;
|
||||||
if (Utility.contains(mapping, '.', '*', '{', '[', '(', '|', '^', '$', '+', '?', '\\')) { //是否是正则表达式))
|
if (Utility.contains(mapping, '.', '*', '{', '[', '(', '|', '^', '$', '+', '?', '\\')) { //是否是正则表达式))
|
||||||
if (mapping.endsWith("/*")) {
|
if (mapping.endsWith("/*")) {
|
||||||
@@ -63,35 +63,37 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
BiPredicate<String, String> predicate = (prefix, uri) -> {
|
BiPredicate<String, String> predicate = (prefix, uri) -> {
|
||||||
return begin || prefix.isEmpty() ? regPredicate.test(uri) : uri.matches(prefix + reg);
|
return begin || prefix.isEmpty() ? regPredicate.test(uri) : uri.matches(prefix + reg);
|
||||||
};
|
};
|
||||||
excludeUrlMaps.put(urlreg, predicate);
|
forbidURIMaps.put(urlreg, predicate);
|
||||||
excludeUrlPredicates = Utility.append(excludeUrlPredicates, predicate);
|
forbidURIPredicates = Utility.append(forbidURIPredicates, predicate);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void removeExcludeUrlReg(final String urlreg) {
|
public boolean removeForbidURIReg(final String urlreg) {
|
||||||
if (urlreg == null || urlreg.isEmpty()) return;
|
if (urlreg == null || urlreg.isEmpty()) return false;
|
||||||
synchronized (excludeLock) {
|
synchronized (excludeLock) {
|
||||||
if (excludeUrlMaps == null || excludeUrlPredicates == null || !excludeUrlMaps.containsKey(urlreg)) return;
|
if (forbidURIMaps == null || forbidURIPredicates == null || !forbidURIMaps.containsKey(urlreg)) return false;
|
||||||
BiPredicate<String, String> predicate = excludeUrlMaps.get(urlreg);
|
BiPredicate<String, String> predicate = forbidURIMaps.get(urlreg);
|
||||||
excludeUrlMaps.remove(urlreg);
|
forbidURIMaps.remove(urlreg);
|
||||||
int index = -1;
|
int index = -1;
|
||||||
for (int i = 0; i < excludeUrlPredicates.length; i++) {
|
for (int i = 0; i < forbidURIPredicates.length; i++) {
|
||||||
if (excludeUrlPredicates[i] == predicate) {
|
if (forbidURIPredicates[i] == predicate) {
|
||||||
index = i;
|
index = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
if (excludeUrlPredicates.length == 1) {
|
if (forbidURIPredicates.length == 1) {
|
||||||
excludeUrlPredicates = null;
|
forbidURIPredicates = null;
|
||||||
} else {
|
} else {
|
||||||
int newlen = excludeUrlPredicates.length - 1;
|
int newlen = forbidURIPredicates.length - 1;
|
||||||
BiPredicate[] news = new BiPredicate[newlen];
|
BiPredicate[] news = new BiPredicate[newlen];
|
||||||
System.arraycopy(excludeUrlPredicates, 0, news, 0, index);
|
System.arraycopy(forbidURIPredicates, 0, news, 0, index);
|
||||||
System.arraycopy(excludeUrlPredicates, index + 1, news, index, newlen - index);
|
System.arraycopy(forbidURIPredicates, index + 1, news, index, newlen - index);
|
||||||
excludeUrlPredicates = news;
|
forbidURIPredicates = news;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -134,7 +136,7 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
public void execute(HttpRequest request, HttpResponse response) throws IOException {
|
public void execute(HttpRequest request, HttpResponse response) throws IOException {
|
||||||
try {
|
try {
|
||||||
final String uri = request.getRequestURI();
|
final String uri = request.getRequestURI();
|
||||||
HttpServlet servlet = null;
|
HttpServlet servlet;
|
||||||
if (request.isWebSocket()) {
|
if (request.isWebSocket()) {
|
||||||
servlet = wsmappings.get(uri);
|
servlet = wsmappings.get(uri);
|
||||||
if (servlet == null && this.regWsArray != null) {
|
if (servlet == null && this.regWsArray != null) {
|
||||||
@@ -163,8 +165,9 @@ public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, Http
|
|||||||
if (servlet == null) servlet = this.resourceHttpServlet;
|
if (servlet == null) servlet = this.resourceHttpServlet;
|
||||||
}
|
}
|
||||||
boolean forbid = false;
|
boolean forbid = false;
|
||||||
if (excludeUrlPredicates != null && excludeUrlPredicates.length > 0) {
|
BiPredicate<String, String>[] forbidUrlPredicates = this.forbidURIPredicates;
|
||||||
for (BiPredicate<String, String> predicate : excludeUrlPredicates) {
|
if (forbidUrlPredicates != null && forbidUrlPredicates.length > 0) {
|
||||||
|
for (BiPredicate<String, String> predicate : forbidUrlPredicates) {
|
||||||
if (predicate != null && predicate.test(servlet._prefix, uri)) {
|
if (predicate != null && predicate.test(servlet._prefix, uri)) {
|
||||||
forbid = true;
|
forbid = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
@@ -57,6 +57,28 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
|||||||
return (HttpFilter) this.prepare.removeFilter(filterName);
|
return (HttpFilter) this.prepare.removeFilter(filterName);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 屏蔽请求URL的正则表达式
|
||||||
|
*
|
||||||
|
* @param urlreg 正则表达式
|
||||||
|
*
|
||||||
|
* @return 是否成功
|
||||||
|
*/
|
||||||
|
public boolean addForbidURIReg(final String urlreg) {
|
||||||
|
return ((HttpPrepareServlet) this.prepare).addForbidURIReg(urlreg);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 删除屏蔽请求URL的正则表达式
|
||||||
|
*
|
||||||
|
* @param urlreg 正则表达式
|
||||||
|
*
|
||||||
|
* @return 是否成功
|
||||||
|
*/
|
||||||
|
public boolean removeForbidURIReg(final String urlreg) {
|
||||||
|
return ((HttpPrepareServlet) this.prepare).removeForbidURIReg(urlreg);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 删除HttpFilter
|
* 删除HttpFilter
|
||||||
*
|
*
|
||||||
@@ -313,21 +335,4 @@ public class HttpServer extends Server<String, HttpContext, HttpRequest, HttpRes
|
|||||||
return httpcontext;
|
return httpcontext;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 增加可以屏蔽的URL正则表达式
|
|
||||||
*
|
|
||||||
* @param urlreg URL正则表达式
|
|
||||||
*/
|
|
||||||
public void addExcludeUrlReg(final String urlreg) {
|
|
||||||
((HttpPrepareServlet) this.prepare).addExcludeUrlReg(urlreg);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 删除可以屏蔽的URL正则表达式
|
|
||||||
*
|
|
||||||
* @param urlreg URL正则表达式
|
|
||||||
*/
|
|
||||||
public void removeExcludeUrlReg(final String urlreg) {
|
|
||||||
((HttpPrepareServlet) this.prepare).removeExcludeUrlReg(urlreg);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user