下方代码可以把url中的域名用正则表达式匹配找出来。
然后可以做白名单、黑名单之类的。
//也可以这样写
@Value("#{'${yml.blackList}'.split(';')}")
ArrayList<String> blackList;
//yml.blackList=www.a.com;www.b.com
public boolean whiteList(String exampleUrl){
ArrayList<String> whiteList = new ArrayList<String>();
whiteList.add("www.baidu.com");
whiteList.add("editor.csdn.net");
//例子
String url="http://www.baidu.com/abc/def/index.html";
url=URLDecoder.decode(url);
//匹配://与/之间的,域名,?是非贪婪匹配
//忽略大小写
Pattern p = Pattern.compile("://(.*?)/", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(url);
m.find();
//得到:www.baidu.com
url=m.group(1);
if(whiteList.contains(url){
return true;
}else{
return false;
}
}