Browse Source

系统密码策略设置

andy 1 year ago
parent
commit
bb694fafb8

+ 1 - 1
pom.xml

@@ -11,7 +11,7 @@
     </parent>
     <groupId>com.beifan</groupId>
     <artifactId>foxlibc</artifactId>
-    <version>1.0.1</version>
+    <version>1.0.2</version>
     <name>foxlibc</name>
     <description>foxlibc</description>
     <properties>

+ 37 - 2
src/main/java/com/beifan/foxlibc/modules/controller/AuthenticationController.java

@@ -332,12 +332,15 @@ public class AuthenticationController {
         String userid = userService.idsign(resetPasswdIn.getUserId(), resetPasswdIn.getOriginPassword());
         SystemSet systemSet = userService.systemSetting();
         String pwdMatch = systemSet.getPwdCons();
+        String pwdType = systemSet.getPwdType();
+        String pwdCase = systemSet.getPwdCase();
 
         /* 修改密码 */
         User user = userService.userQuery(userid);
         user.setUserPwd(resetPasswdIn.getNewPassword());
 
-        Assert.throwIfBool(Pattern.matches(pwdMatch, user.getUserPwd()), "密码不符合规则");
+        Assert.throwIfBool(Pattern.matches(generateRegexString(pwdMatch, pwdType, pwdCase
+        ), user.getUserPwd()), "密码不符合规则");
         String pwd = StringUtils.toLowerCase(DoubleCoder.MD5(resetPasswdIn.getNewPassword()));
 
         userService.updateUserPwd(userid, pwd);
@@ -357,7 +360,10 @@ public class AuthenticationController {
         user.setUserPwd(resetPasswdIn.getNewPassword());
         SystemSet systemSet = userService.systemSetting();
         String pwdMatch = systemSet.getPwdCons();
-        Assert.throwIfBool(Pattern.matches(pwdMatch, user.getUserPwd()), "密码不符合规则");
+        String pwdType = systemSet.getPwdType();
+        String pwdCase = systemSet.getPwdCase();
+        Assert.throwIfBool(Pattern.matches(generateRegexString(pwdMatch, pwdType, pwdCase
+        ), user.getUserPwd()), "密码不符合规则");
         String pwd = StringUtils.toLowerCase(DoubleCoder.MD5(resetPasswdIn.getNewPassword()));
 
         userService.updateUserSecPwd(userid, pwd);
@@ -367,4 +373,33 @@ public class AuthenticationController {
 
     // private String getSessionId() { return WebRequests.getHttpServletRequest().getSession().getId(); }
 
+
+    private String generateRegexString(String maxLength, String pwdType, String matchCase) {
+        switch (pwdType) {
+            case "1": {
+                return String.format("^(?=.*[0-9]).{6,%d}$", maxLength);
+            }
+            case "2": {
+                if ("1".equals(matchCase)) {
+                    return String.format("^(?=.*[a-z])(?=.*[A-Z]).{6,%d}$", maxLength);
+                } else {
+                    return String.format("^(?=.*[a-zA-Z]).{6,%d}$", maxLength);
+                }
+            }
+            case "3": {
+                if ("1".equals(matchCase)) {
+                    return String.format("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9]).{6,%d}$", maxLength);
+                } else {
+                    return String.format("^(?=.*[a-zA-Z])(?=.*[0-9]).{6,%d}$", maxLength);
+                }
+            }
+            default: {
+                if ("1".equals(matchCase)) {
+                    return String.format("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[^0-9a-zA-Z]).{6,%d}$", maxLength);
+                } else {
+                    return String.format("^(?=.*[a-zA-Z])(?=.*[0-9])(?=.*[^0-9a-zA-Z]).{6,%d}$", maxLength);
+                }
+            }
+        }
+    }
 }

+ 2 - 0
src/main/java/com/beifan/foxlibc/modules/pojo/model/SystemSet.java

@@ -21,6 +21,8 @@ public class SystemSet {
     private Integer userIdleDays;
     private Integer sysLockMinutes;
     private String pwdCons;
+    private String pwdType;
+    private String pwdCase;
     private Integer pwdValidTime;
     private Integer loginErrorCount;
     private Boolean openAuthData;