[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();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.usthe.manager.pojo.entity;
|
||||
|
||||
import com.usthe.manager.support.valid.HostValid;
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.AllArgsConstructor;
|
||||
@@ -61,6 +62,7 @@ public class Monitor {
|
||||
*/
|
||||
@ApiModelProperty(value = "监控的对端host", example = "192.167.25.11", accessMode = READ_WRITE, position = 4)
|
||||
@Length(max = 100)
|
||||
@HostValid
|
||||
private String host;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.usthe.manager.support.valid;
|
||||
|
||||
import com.usthe.common.util.IpDomainUtil;
|
||||
|
||||
import javax.validation.ConstraintValidator;
|
||||
import javax.validation.ConstraintValidatorContext;
|
||||
|
||||
/**
|
||||
* host注解数据自定义校验器
|
||||
* @author tomsun28
|
||||
* @date 2021/11/17 19:44
|
||||
*/
|
||||
public class HostParamValidator implements ConstraintValidator<HostValid, String> {
|
||||
|
||||
@Override
|
||||
public boolean isValid(String value, ConstraintValidatorContext context) {
|
||||
// 判断value是否满足ipv4 ipv5 域名 格式
|
||||
return IpDomainUtil.validateIpDomain(value);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.usthe.manager.support.valid;
|
||||
|
||||
import javax.validation.Constraint;
|
||||
import javax.validation.Payload;
|
||||
import java.lang.annotation.Documented;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
import static java.lang.annotation.ElementType.FIELD;
|
||||
import static java.lang.annotation.ElementType.PARAMETER;
|
||||
import static java.lang.annotation.RetentionPolicy.RUNTIME;
|
||||
|
||||
/**
|
||||
* host注解数据自定义校验器注解
|
||||
* @author tomsun28
|
||||
* @date 2021/11/17 19:42
|
||||
*/
|
||||
@Target({ FIELD, PARAMETER })
|
||||
@Retention(RUNTIME)
|
||||
@Documented
|
||||
@Constraint(validatedBy = HostParamValidator.class)
|
||||
public @interface HostValid {
|
||||
|
||||
String message() default "Host value is invalid,must ipv4, ipv6 or domain";
|
||||
|
||||
Class<?>[] groups() default {};
|
||||
|
||||
Class<? extends Payload>[] payload() default {};
|
||||
}
|
||||
@@ -11,8 +11,6 @@ configmap:
|
||||
type: 1
|
||||
- key: password
|
||||
type: 2
|
||||
- key: param1
|
||||
type: 1
|
||||
# 指标组列表
|
||||
metrics:
|
||||
# 第一个监控指标组 cpu
|
||||
|
||||
@@ -29,5 +29,6 @@ param:
|
||||
- field: ssl
|
||||
name: 启动SSL
|
||||
type: radio
|
||||
required: false
|
||||
# 当type为radio单选框,checkbox复选框时,option表示可选项值列表
|
||||
option: Yes,No
|
||||
Reference in New Issue
Block a user