正则表达式--密码复杂度验证--必须包含大写、小写、数字、特殊字符中的至少三项

举报
红目香薰 发表于 2022/01/23 00:19:33 2022/01/23
【摘要】 密码复杂度要求: 大写字母、小写字母、数字、特殊字符,四项中至少包含三项。 import org.junit.Test;import org.springframework.util.StringUtils; import java.util.ArrayList;import java.util.Arrays;import java...

密码复杂度要求:

大写字母、小写字母、数字、特殊字符,四项中至少包含三项。


  
  1. import org.junit.Test;
  2. import org.springframework.util.StringUtils;
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.List;
  6. /**
  7. * @Author TeacherFu
  8. * @Version 1.0
  9. */
  10. public class PasswordTest {
  11. /**
  12. * 1.全部包含:大写、小写、数字、特殊字符;
  13. * 2.无大写:小写、数字、特殊字符;
  14. * 3.无小写:大写、数字、特殊字符;
  15. * 4.无数字:大写、小写、特殊字符;
  16. * 5.无特殊字符:大写、小写、数字;
  17. */
  18. @Test
  19. public void complexTest(){
  20. List<String> list = Arrays.asList(new String[]{
  21. //全包含
  22. "aBcd12@",
  23. "aB2@dfgh",
  24. "aB2_dfgh",
  25. "123Abc_",
  26. "_123Abc",
  27. "~123Abc",
  28. //无大写
  29. "abcd12@",
  30. "!abcd12",
  31. "(abcd12",
  32. ")abcd12",
  33. "{abcd12",
  34. "}abcd12",
  35. "[abcd12",
  36. "]abcd12",
  37. "|abcd12",
  38. "\\abcd12",
  39. "\"abcd12",
  40. "'abcd12",
  41. ":abcd12",
  42. "?abcd12",
  43. "<abcd12",
  44. ">abcd12",
  45. "~abcd12",
  46. "3~bcd12",
  47. "F~bcd12",
  48. //无小写
  49. "ABCD12@",
  50. "!ABCD12",
  51. "!AB12333",
  52. //无数字
  53. "aBcd_@",
  54. "!Abcd",
  55. //无特殊字符
  56. "abCd12",
  57. "abcD12",
  58. "12345678",
  59. "abcdefgh",
  60. "ABCDEFGH",
  61. "abcd1234",
  62. "ABCD1234",
  63. //无特殊字符
  64. "aBcd1234",
  65. //无数字
  66. "abcdEfgh!",
  67. "~!@#$%^&*()_+{}:'?<>",
  68. "abcd!@#$",
  69. "1234!@#$",
  70. "",
  71. null
  72. });
  73. List<String> matches = new ArrayList<String>();
  74. for(String word : list){
  75. //
  76. if(isComplexityMatches(word)) {
  77. matches.add(word);
  78. }
  79. }
  80. System.out.println(Arrays.toString(matches.toArray()));
  81. List<String> matches2 = new ArrayList<String>();
  82. for(String word : list){
  83. //
  84. if(isComplexityMatches2(word)) {
  85. matches2.add(word);
  86. }
  87. }
  88. System.out.println(Arrays.toString(matches2.toArray()));
  89. List<String> matches3 = new ArrayList<String>();
  90. for(String word : list){
  91. //
  92. if(isComplexityMatches3(word,5,6)) {
  93. matches3.add(word);
  94. }
  95. }
  96. System.out.println(Arrays.toString(matches3.toArray()));
  97. }
  98. /**
  99. * 复杂度要求:
  100. * 大写、小写、数字、特殊字符,需要包含其中至少三项
  101. *
  102. * @param content
  103. * @return
  104. */
  105. private boolean isComplexityMatches(String content){
  106. if(!StringUtils.hasLength(content)){
  107. return false;
  108. }
  109. //1.全部包含:大写、小写、数字、特殊字符;
  110. String regex1 = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[\\W_])^.*$";
  111. //2.无大写:小写、数字、特殊字符;
  112. String regex2 = "(?=.*[a-z])(?=.*[0-9])(?=.*[\\W_])^.*$";
  113. //3.无小写:大写、数字、特殊字符;
  114. String regex3 = "(?=.*[A-Z])(?=.*[0-9])(?=.*[\\W_])^.*$";
  115. //4.无数字:大写、小写、特殊字符;
  116. String regex4 = "(?=.*[A-Z])(?=.*[a-z])(?=.*[\\W_])^.*$";
  117. //5.无特殊字符:大写、小写、数字;
  118. String regex5 = "(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])^.*$";
  119. String regex = "(" + regex1 + ")|(" + regex2 + ")|(" + regex3 + ")|(" + regex4 + ")|(" + regex5 + ")";
  120. return content.matches(regex);
  121. }
  122. private boolean isComplexityMatches2(String content){
  123. if(!StringUtils.hasLength(content)){
  124. return false;
  125. }
  126. //String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_!@#$%^&*`~()-+=]+$)(?![a-z0-9]+$)(?![a-z\\W_!@#$%^&*`~()-+=]+$)(?![0-9\\W_!@#$%^&*`~()-+=]+$)[a-zA-Z0-9\\W_!@#$%^&*`~()-+=]{8,30}$";
  127. //String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{8,30}$";
  128. //String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{5,30}$";
  129. //String regex = "^(?![a-zA-Z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[a-zA-Z0-9\\W_]{5,}$";
  130. String regex = "^(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[A-Za-z0-9\\W_]{5,}$";//ok
  131. //String regex = "(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)^[A-Za-z0-9\\W_]{5,}$";//ok
  132. //String regex = "^[A-Za-z0-9\\W_]{5,}$(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)";
  133. //错误的模式,测试结果不正确(此模式匹配的是:大写、小写、数字、特殊字符等四项必须全部包含)
  134. String regex2 = "^(?![A-Za-z0-9]+$)(?![a-z0-9\\W]+$)(?![A-Za-z\\W]+$)(?![A-Z0-9\\W]+$)[a-zA-Z0-9\\W]{5,30}$";
  135. return content.matches(regex);
  136. //return content.matches(regex2);
  137. }
  138. private boolean isComplexityMatches3(String content,Integer minLength,Integer maxLength){
  139. if(!StringUtils.hasLength(content)){
  140. return false;
  141. }
  142. if(minLength != null && maxLength != null && minLength > maxLength){
  143. System.out.println("参数非法:最小长度不能大于最大长度。");
  144. return false;
  145. }
  146. if(minLength == null && maxLength != null && maxLength < 0){
  147. System.out.println("参数非法:最大长度不能小于0。");
  148. return false;
  149. }
  150. String length = "";
  151. if(minLength == null || minLength < 1){
  152. length = "{0,";
  153. }else{
  154. length = "{" + minLength + ",";
  155. }
  156. if(maxLength == null){
  157. length = length + "}";
  158. }else {
  159. length = length + maxLength + "}";
  160. }
  161. //String regex = "^(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[A-Za-z0-9\\W_]{5,}$";//ok
  162. String regex = "^(?![A-Za-z]+$)(?![A-Z0-9]+$)(?![A-Z\\W_]+$)(?![a-z0-9]+$)(?![a-z\\W_]+$)(?![0-9\\W_]+$)[A-Za-z0-9\\W_]" + length + "$";
  163. return content.matches(regex);
  164. }
  165. }

文章来源: laoshifu.blog.csdn.net,作者:红目香薰,版权归原作者所有,如需转载,请联系作者。

原文链接:laoshifu.blog.csdn.net/article/details/121064278

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。