[monitor] 自定义校验器注解@HostValid

This commit is contained in:
tomsun28
2021-11-17 20:21:28 +08:00
parent 1f4259364c
commit 119d05c8ab
6 changed files with 87 additions and 2 deletions

View File

@@ -0,0 +1,35 @@
package com.usthe.common.util;
import sun.net.util.IPAddressUtil;
import java.util.regex.Pattern;
/**
* ipv4 ipv6 domain 工具类
* @author tomsun28
* @date 2021/11/17 19:56
*/
public class IpDomainUtil {
/**
* 域名校验正则
*/
private static final Pattern DOMAIN_PATTERN =
Pattern.compile("^([hH][tT]{2}[pP]://|[hH][tT]{2}[pP][sS]://)?(([A-Za-z0-9-~]+).)+([A-Za-z0-9-~\\/])+$");
/**
* 校验判断是否是 ip或者domain
* @param ipDomain ip domain string
* @return true-yes false-no
*/
public static boolean validateIpDomain(String ipDomain) {
if (IPAddressUtil.isIPv4LiteralAddress(ipDomain)) {
return true;
}
if (IPAddressUtil.isIPv6LiteralAddress(ipDomain)) {
return true;
}
return DOMAIN_PATTERN.matcher(ipDomain).matches();
}
}