diff --git a/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java b/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java index 4bbba71..c26dd2f 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/RegexUtil.java @@ -51,6 +51,10 @@ public class RegexUtil { * 身份证 */ private static final Pattern PATTERN_IDENTITY = Pattern.compile("(^\\d{15}$)|(^\\d{17}(x|X|\\d)$)"); + /** + * 用户名 + */ + private static final Pattern PATTERN_USERNAME = Pattern.compile("^[a-zA-Z0-9_\\s]+$"); /** * 字母 */ @@ -166,6 +170,16 @@ public class RegexUtil { return PATTERN_IDENTITY.matcher(input).matches(); } + /** + * 判断用户名 + * + * @param input + * @return + */ + public static boolean isUsername(String input) { + return PATTERN_USERNAME.matcher(input).matches(); + } + /** * 判断是字母 * diff --git a/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java b/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java index fdc668e..123fd7c 100644 --- a/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java +++ b/cloud-common/src/main/java/com/cm/common/utils/annotation/AnnotationUtil.java @@ -136,7 +136,12 @@ public class AnnotationUtil { if (StringUtils.isBlank(verifyType)) { return; } - if (StringUtils.equals("phone", verifyType)) { + if (StringUtils.equals("username", verifyType)) { + if (!RegexUtil.isUsername(value)) { + throw new ParamsException(String.format("%s格式只能是字母、数字和下划线", name)); + } + return; + } else if (StringUtils.equals("phone", verifyType)) { if (!RegexUtil.isPhone(value)) { throw new ParamsException(String.format("%s格式非手机格式", name)); }