|
| 1 | +package cn.wildfirechat.app.tools; |
| 2 | + |
| 3 | +import org.springframework.beans.factory.annotation.Value; |
| 4 | +import org.springframework.stereotype.Component; |
| 5 | + |
| 6 | +import java.util.Arrays; |
| 7 | +import java.util.HashSet; |
| 8 | +import java.util.Set; |
| 9 | + |
| 10 | +/** |
| 11 | + * 弱口令检验组件 |
| 12 | + * <p> |
| 13 | + * 通过配置文件控制检验规则: |
| 14 | + * <ul> |
| 15 | + * <li>password.weak_check.enable — 是否开启弱口令校验(默认 true)</li> |
| 16 | + * <li>password.weak_check.min_length — 最小密码长度(默认 8)</li> |
| 17 | + * <li>password.weak_check.require_uppercase — 是否要求大写字母(默认 false)</li> |
| 18 | + * <li>password.weak_check.require_lowercase — 是否要求小写字母(默认 false)</li> |
| 19 | + * <li>password.weak_check.require_digit — 是否要求数字(默认 false)</li> |
| 20 | + * <li>password.weak_check.require_special_char — 是否要求特殊字符(默认 false)</li> |
| 21 | + * <li>password.weak_check.forbidden_list — 禁止使用的常见弱口令,逗号分隔</li> |
| 22 | + * </ul> |
| 23 | + */ |
| 24 | +@Component |
| 25 | +public class WeakPasswordChecker { |
| 26 | + |
| 27 | + @Value("${password.weak_check.enable:true}") |
| 28 | + private boolean enable; |
| 29 | + |
| 30 | + @Value("${password.weak_check.min_length:8}") |
| 31 | + private int minLength; |
| 32 | + |
| 33 | + @Value("${password.weak_check.require_uppercase:false}") |
| 34 | + private boolean requireUppercase; |
| 35 | + |
| 36 | + @Value("${password.weak_check.require_lowercase:false}") |
| 37 | + private boolean requireLowercase; |
| 38 | + |
| 39 | + @Value("${password.weak_check.require_digit:false}") |
| 40 | + private boolean requireDigit; |
| 41 | + |
| 42 | + @Value("${password.weak_check.require_special_char:false}") |
| 43 | + private boolean requireSpecialChar; |
| 44 | + |
| 45 | + /** |
| 46 | + * 逗号分隔的禁止密码列表,例如: |
| 47 | + * password.weak_check.forbidden_list=123456,password,111111,qwerty,abc123 |
| 48 | + */ |
| 49 | + @Value("${password.weak_check.forbidden_list:123456,password,123456789,12345678,111111,qwerty,abc123,123123,000000,iloveyou,admin,letmein,welcome,monkey,dragon}") |
| 50 | + private String forbiddenListStr; |
| 51 | + |
| 52 | + private Set<String> forbiddenSet; |
| 53 | + |
| 54 | + @javax.annotation.PostConstruct |
| 55 | + private void init() { |
| 56 | + forbiddenSet = new HashSet<>(); |
| 57 | + if (forbiddenListStr != null && !forbiddenListStr.isEmpty()) { |
| 58 | + Arrays.stream(forbiddenListStr.split(",")) |
| 59 | + .map(String::trim) |
| 60 | + .filter(s -> !s.isEmpty()) |
| 61 | + .forEach(forbiddenSet::add); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * 检查密码是否为弱口令。 |
| 67 | + * |
| 68 | + * @param password 待检查的明文密码 |
| 69 | + * @return {@code true} 表示是弱口令(不符合要求),{@code false} 表示密码合格 |
| 70 | + */ |
| 71 | + public boolean isWeakPassword(String password) { |
| 72 | + if (!enable) { |
| 73 | + return false; |
| 74 | + } |
| 75 | + |
| 76 | + if (password == null || password.length() < minLength) { |
| 77 | + return true; |
| 78 | + } |
| 79 | + |
| 80 | + if (requireUppercase && password.chars().noneMatch(Character::isUpperCase)) { |
| 81 | + return true; |
| 82 | + } |
| 83 | + |
| 84 | + if (requireLowercase && password.chars().noneMatch(Character::isLowerCase)) { |
| 85 | + return true; |
| 86 | + } |
| 87 | + |
| 88 | + if (requireDigit && password.chars().noneMatch(Character::isDigit)) { |
| 89 | + return true; |
| 90 | + } |
| 91 | + |
| 92 | + if (requireSpecialChar && password.chars().allMatch(c -> Character.isLetterOrDigit(c) || Character.isWhitespace(c))) { |
| 93 | + return true; |
| 94 | + } |
| 95 | + |
| 96 | + if (forbiddenSet != null && forbiddenSet.contains(password)) { |
| 97 | + return true; |
| 98 | + } |
| 99 | + |
| 100 | + return false; |
| 101 | + } |
| 102 | +} |
0 commit comments