Files
trident/src/main/java/checkURL.java

52 lines
1.3 KiB
Java
Raw Normal View History

2017-09-14 17:38:34 +08:00
/**
2017-09-14 17:43:12 +08:00
* date: 2017.09.05
* author: JoyChou (https://joychou.org)
2017-09-18 17:50:32 +08:00
* mail : joychou@joychou.org
2017-09-14 17:38:34 +08:00
*/
import java.net.URL;
import com.google.common.net.InternetDomainName;
2017-09-22 11:09:07 +08:00
public class CheckURL {
2017-09-14 17:38:34 +08:00
/**
* 检测传入的URL是否在白名单的域名里
* url需要检测的URL
* urlWList: 一级域名的域名列表比如String[] urlWList = {"joychou.com", "joychou.me"};
* 返回值合法URL返回true非法URL返回false
*/
public static Boolean checkUrlWlist(String url, String[] urlWList) {
try {
URL u = new URL(url);
// 只允许http和https的协议
if (!u.getProtocol().startsWith("http") && !u.getProtocol().startsWith("https")) {
return false;
}
// 获取域名,并转为小写
String host = u.getHost().toLowerCase();
// 获取一级域名
String rootDomain = InternetDomainName.from(host).topPrivateDomain().toString();
for (String whiteUrl: urlWList){
if (rootDomain.equals(whiteUrl)) {
return true;
}
}
return false;
} catch (Exception e) {
return false;
}
}
public static void main(String[] args) throws Exception {
}
}