[monitor] 自定义校验器注解@HostValid
This commit is contained in:
35
common/src/main/java/com/usthe/common/util/IpDomainUtil.java
Normal file
35
common/src/main/java/com/usthe/common/util/IpDomainUtil.java
Normal 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();
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user