白名单使用正则匹配

This commit is contained in:
暮光:城中城
2019-02-01 18:37:55 +08:00
committed by zhanghongli
parent 620c7d02e8
commit 15cfa7e2d1
3 changed files with 25 additions and 4 deletions

View File

@@ -40,12 +40,20 @@ public class MgHttpRequestController {
if (whiteDomain == null || whiteDomain.isEmpty()) {
return DocResponseJson.warn("未设置代理请求白名单,不能代理请求");
}
long inWhiteList = whiteDomain.stream().filter(paramUrl::startsWith).count();
paramUrl = paramUrl.replace("http://", "").replace("https://", "");
String regexStr = paramUrl.substring(0, paramUrl.indexOf("/"));
long inWhiteList = whiteDomain.stream().filter(val -> regexStr.matches(val)).count();
if (inWhiteList <= 0) {
return DocResponseJson.warn("该域名不在白名单内,不能代理请求");
}
HttpRequest request = param.createRequest();
HttpResponse response = request.execute();
HttpResponse response;
try{
response = request.execute();
} catch (Exception e) {
e.printStackTrace();
return DocResponseJson.warn("请求失败,请检查域名是否正确");
}
HttpRequestVo httpRequestVo = new HttpRequestVo();
httpRequestVo.setData(response.body());
httpRequestVo.setStatus(response.getStatus());