From 8632d9a075f1bb5485c045d33de1839590bb339f Mon Sep 17 00:00:00 2001 From: wenc000 <450292408@qq.com> Date: Thu, 21 May 2020 23:47:37 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E7=94=A8=E6=88=B7=E5=90=8D?= =?UTF-8?q?=E6=A0=BC=E5=BC=8F=E6=A0=A1=E9=AA=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/cm/common/utils/RegexUtil.java | 14 ++++++++++++++ .../cm/common/utils/annotation/AnnotationUtil.java | 7 ++++++- 2 files changed, 20 insertions(+), 1 deletion(-) 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)); }