白名单使用正则匹配
This commit is contained in:
@@ -13,8 +13,8 @@ zyplayer:
|
|||||||
doc:
|
doc:
|
||||||
swagger:
|
swagger:
|
||||||
proxy-request:
|
proxy-request:
|
||||||
# 内部访问时可以代理请求的域名,使用 ; 分割,必须设置,防止访问到内部地址资源
|
# 允许代理请求的域名,正则表达式,多个使用 ; 分割,必须设置,防止通过代理接口访问到内部资源,实在觉得没必要可设置为:.+
|
||||||
white-domain: http://127.0.0.1/;http://www.baidu.com/;http://swagger-bootstrap-ui.xiaominfo.com/
|
white-domain: \S+\.zyplayer\.com;127.0.1:8080;\S+\.xiaominfo\.com;
|
||||||
# zyplayer_doc_manage管理端的数据库配置
|
# zyplayer_doc_manage管理端的数据库配置
|
||||||
manage:
|
manage:
|
||||||
datasource:
|
datasource:
|
||||||
|
|||||||
@@ -0,0 +1,13 @@
|
|||||||
|
package com.zyplayer.doc.test;
|
||||||
|
|
||||||
|
public class Test {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
String originRegex = "\\w+\\.zyplayer\\.com";
|
||||||
|
String origin = "http://doc.zyplayer.com/manage/";
|
||||||
|
origin = origin.replace("http://", "").replace("https://", "");
|
||||||
|
origin = origin.substring(0, origin.indexOf("/"));
|
||||||
|
System.out.println(origin.toLowerCase().matches(originRegex));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -40,12 +40,20 @@ public class MgHttpRequestController {
|
|||||||
if (whiteDomain == null || whiteDomain.isEmpty()) {
|
if (whiteDomain == null || whiteDomain.isEmpty()) {
|
||||||
return DocResponseJson.warn("未设置代理请求白名单,不能代理请求");
|
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) {
|
if (inWhiteList <= 0) {
|
||||||
return DocResponseJson.warn("该域名不在白名单内,不能代理请求");
|
return DocResponseJson.warn("该域名不在白名单内,不能代理请求");
|
||||||
}
|
}
|
||||||
HttpRequest request = param.createRequest();
|
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 httpRequestVo = new HttpRequestVo();
|
||||||
httpRequestVo.setData(response.body());
|
httpRequestVo.setData(response.body());
|
||||||
httpRequestVo.setStatus(response.getStatus());
|
httpRequestVo.setStatus(response.getStatus());
|
||||||
|
|||||||
Reference in New Issue
Block a user