Browse Source

add ConstsRegex

shimingxy 6 months ago
parent
commit
18f6f86b24

+ 36 - 0
maxkey-core/src/main/java/org/dromara/maxkey/constants/ConstsRegex.java

@@ -0,0 +1,36 @@
+/*
+ * Copyright [2020] [MaxKey of copyright http://www.maxkey.top]
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+ 
+
+package org.dromara.maxkey.constants;
+
+import java.util.regex.Pattern;
+
+/**
+ *  Regex for email , mobile and etc.
+ */
+public class ConstsRegex {
+	
+	 public static final Pattern 	EMAIL_PATTERN 		= Pattern.compile("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
+
+	 public static final Pattern 	MOBILE_PATTERN 		= Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$");
+	 
+	 public static final Pattern  	IPADDRESS_REGEX 	= Pattern.compile("\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}");
+	 
+	 public static final Pattern  	WHITESPACE_REGEX 	= Pattern.compile("\\s");
+	 
+	 public static final Pattern 	CHINESE_REGEX 		= Pattern.compile("[\\u4e00-\\u9fa5]");
+}

+ 4 - 9
maxkey-webs/maxkey-web-maxkey/src/main/java/org/dromara/maxkey/web/contorller/ForgotPasswordContorller.java

@@ -17,12 +17,11 @@
 
 package org.dromara.maxkey.web.contorller;
 
-import java.util.regex.Pattern;
-
 import org.apache.commons.lang3.StringUtils;
 import org.dromara.maxkey.authn.jwt.AuthTokenService;
 import org.dromara.maxkey.configuration.EmailConfig;
 import org.dromara.maxkey.constants.ConstsEntryType;
+import org.dromara.maxkey.constants.ConstsRegex;
 import org.dromara.maxkey.constants.ConstsAct;
 import org.dromara.maxkey.constants.ConstsActResult;
 import org.dromara.maxkey.entity.ChangePassword;
@@ -47,10 +46,6 @@ import org.springframework.web.bind.annotation.*;
 public class ForgotPasswordContorller {
     private static Logger logger = LoggerFactory.getLogger(ForgotPasswordContorller.class);
 
-    static final Pattern emailRegex = Pattern.compile("^\\s*\\w+(?:\\.{0,1}[\\w-]+)*@[a-zA-Z0-9]+(?:[-.][a-zA-Z0-9]+)*\\.[a-zA-Z]+\\s*$");
-    
-    static final Pattern mobileRegex = Pattern.compile("^[1][3,4,5,6,7,8,9][0-9]{9}$");
-    
     @Autowired
     EmailConfig emailConfig;
     
@@ -125,8 +120,8 @@ public class ForgotPasswordContorller {
         }
         
     	ChangePassword change = null;
-    	logger.debug("Mobile Regex matches {}",mobileRegex.matcher(mobile).matches());
-    	if(StringUtils.isNotBlank(mobile) && mobileRegex.matcher(mobile).matches()) {
+    	logger.debug("Mobile Regex matches {}",ConstsRegex.MOBILE_PATTERN.matcher(mobile).matches());
+    	if(StringUtils.isNotBlank(mobile) && ConstsRegex.MOBILE_PATTERN.matcher(mobile).matches()) {
     		UserInfo userInfo = userInfoService.findByEmailMobile(mobile);
     		if(userInfo != null) {
 	    		change = new ChangePassword(userInfo);
@@ -152,7 +147,7 @@ public class ForgotPasswordContorller {
         }
         
     	ChangePassword change = null;
-    	if(StringUtils.isNotBlank(email) && emailRegex.matcher(email).matches()) {
+    	if(StringUtils.isNotBlank(email) && ConstsRegex.EMAIL_PATTERN.matcher(email).matches()) {
     		UserInfo userInfo = userInfoService.findByEmailMobile(email);
     		if(userInfo != null) {
 	    		change = new ChangePassword(userInfo);