diff --git a/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostParamValidator.java b/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostParamValidator.java index 1f5563d99be..f6984317af4 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostParamValidator.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostParamValidator.java @@ -28,10 +28,21 @@ * */ public class HostParamValidator implements ConstraintValidator { + public static final String HTTP = "http://"; + public static final String HTTPS = "https://"; + public static final String BLANK = ""; + public static final String PATTERN_HTTP = "(?i)http://"; + public static final String PATTERN_HTTPS = "(?i)https://"; @Override public boolean isValid(String value, ConstraintValidatorContext context) { // 判断value是否满足ipv4 ipv5 域名 格式 + if(value != null && value.toLowerCase().contains(HTTP)){ + value = value.replaceAll(PATTERN_HTTP, BLANK); + } + if(value != null && value.toLowerCase().contains(HTTPS)){ + value = value.replace(PATTERN_HTTPS, BLANK); + } return IpDomainUtil.validateIpDomain(value); } } diff --git a/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostValid.java b/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostValid.java index f4c322d3fbb..dfa4aba9a3d 100644 --- a/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostValid.java +++ b/common/src/main/java/org/dromara/hertzbeat/common/support/valid/HostValid.java @@ -38,7 +38,7 @@ @Constraint(validatedBy = HostParamValidator.class) public @interface HostValid { - String message() default "监控Host必须是ipv4,ipv6或域名"; + String message() default "监控Host必须是ipv4,ipv6或域名,
EX:127.0.0.1 或者 hertzbeat.com"; Class[] groups() default {}; diff --git a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java index 498001052d5..0075f0641a7 100644 --- a/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java +++ b/manager/src/main/java/org/dromara/hertzbeat/manager/service/impl/MonitorServiceImpl.java @@ -78,6 +78,12 @@ public class MonitorServiceImpl implements MonitorService { private static final Long MONITOR_ID_TMP = 1000000000L; + public static final String HTTP = "http://"; + public static final String HTTPS = "https://"; + public static final String BLANK = ""; + public static final String PATTERN_HTTP = "(?i)http://"; + public static final String PATTERN_HTTPS = "(?i)https://"; + private static final Gson GSON = new Gson(); @Autowired @@ -383,6 +389,12 @@ public void validate(MonitorDto monitorDto, Boolean isModify) throws IllegalArgu break; case "host": String hostValue = param.getValue(); + if(hostValue.toLowerCase().contains(HTTP)){ + hostValue = hostValue.replaceAll(PATTERN_HTTP, BLANK); + } + if(hostValue.toLowerCase().contains(HTTPS)){ + hostValue = hostValue.replace(PATTERN_HTTPS, BLANK); + } if (!IpDomainUtil.validateIpDomain(hostValue)) { throw new IllegalArgumentException("Params field " + field + " value " + hostValue + " is invalid host value.");