Java 密码复杂度校验

您所在的位置:网站首页 密码8~20位,至少包含字母/数字/符号2种组合 Java 密码复杂度校验

Java 密码复杂度校验

#Java 密码复杂度校验| 来源: 网络整理| 查看: 265

1,需求

复杂性:用户的密码中必须包含的字符类型,默认为中

弱:必须包含小写字母中:必须包含小写字母、数字强:必须包含小写字母、数字、大写字母、特殊字符(鼠标移入的提示文字相同)

注:检查密码复杂度,仅新增账户、重置密码时生效,已有账户密码不检查; 当密码不符合复杂度时,根据强弱设置动态提示:“当前密码复杂度不符合,请包括大小写字母、数字、特殊字符”

2,代码

2.1 密码校验工具类:

import cn.hutool.core.util.StrUtil; import com.google.common.base.Preconditions; public class PwdCheckUtil { //定义特殊字符 public static String SPECIAL_CHAR = "!\"#$%&'()*+,-./:;?@[\\]^_`{|}~"; /** * @brief 检测密码中字符长度 * @param[in] password 密码字符串 * @return 符合长度要求 返回true */ public static boolean checkPasswordLength(String password, String minNum, String maxNum) { boolean flag =false; if (StrUtil.isBlank(maxNum)) { minNum = StrUtil.isBlank(minNum) ? "0":minNum; if (password.length() >= Integer.parseInt(minNum)) { flag = true; } } else { minNum = StrUtil.isBlank(minNum) ? "0":minNum; if (password.length() >= Integer.parseInt(minNum) && password.length() char[] chPass = password.toCharArray(); for (int i = 0; i return true; } } return false; } /** * @brief 检测密码中是否包含字母(不区分大小写) * @param[in] password 密码字符串 * @return 包含字母 返回true */ public static boolean checkContainCase(String password) { char[] chPass = password.toCharArray(); for (int i = 0; i return true; } } return false; } /** * @brief 检测密码中是否包含小写字母 * @param[in] password 密码字符串 * @return 包含小写字母 返回true */ public static boolean checkContainLowerCase(String password) { char[] chPass = password.toCharArray(); for (int i = 0; i return true; } } return false; } /** * @brief 检测密码中是否包含大写字母 * @param[in] password 密码字符串 * @return 包含大写字母 返回true */ public static boolean checkContainUpperCase(String password) { char[] chPass = password.toCharArray(); for (int i = 0; i return true; } } return false; } /** * @brief 检测密码中是否包含特殊符号 * @param[in] password 密码字符串 * @return 包含特殊符号 返回true */ public static boolean checkContainSpecialChar(String password) { char[] chPass = password.toCharArray(); for (int i = 0; i return true; } } return false; } }

2.2 强复杂度设置

//测试 public static void main(String[] args) { checkStrongPwd("FFsssssssssss>"); } /** * @brief 检测密码复杂度是否为 强 * @param[in] password 密码字符串 * @return 符合长度要求 返回true */ public static void checkStrongPwd(String pwd) { try { Preconditions.checkNotNull(pwd); if(!PwdCheckUtil.checkPasswordLength(pwd, "8", null) || !PwdCheckUtil.checkContainCase(pwd) || !PwdCheckUtil.checkContainDigit(pwd) || !PwdCheckUtil.checkContainSpecialChar(pwd) ){ throw new Exception("密码必须包含数字、字母、特殊符号且大于8位"); } }catch (Exception e){ e.printStackTrace(); } }

控制台输出:

在这里插入图片描述

3,Character类使用 public class CharacterDemo02 { public static void main(String[] args) { // public static boolean isUpperCase(char ch):判断给定的字符是否是大写字符 System.out.println("isUpperCase:" + Character.isUpperCase('A')); //isUpperCase:true System.out.println("isUpperCase:" + Character.isUpperCase('a')); //isUpperCase:false System.out.println("isUpperCase:" + Character.isUpperCase('0')); //isUpperCase:false System.out.println("-----------------------------------------"); // public static boolean isLowerCase(char ch):判断给定的字符是否是小写字符 System.out.println("isLowerCase:" + Character.isLowerCase('A')); //isLowerCase:false System.out.println("isLowerCase:" + Character.isLowerCase('a')); //isLowerCase:true System.out.println("isLowerCase:" + Character.isLowerCase('0')); //isLowerCase:false System.out.println("-----------------------------------------"); // public static boolean isDigit(char ch):判断给定的字符是否是数字字符 System.out.println("isDigit:" + Character.isDigit('A')); //isDigit:false System.out.println("isDigit:" + Character.isDigit('a')); //isDigit:false System.out.println("isDigit:" + Character.isDigit('0')); //isDigit:true System.out.println("-----------------------------------------"); // public static char toUpperCase(char ch):把给定的字符转换为大写字符 System.out.println("toUpperCase:" + Character.toUpperCase('A')); //toUpperCase:A System.out.println("toUpperCase:" + Character.toUpperCase('a')); //toUpperCase:A System.out.println("-----------------------------------------"); // public static char toLowerCase(char ch):把给定的字符转换为小写字符 System.out.println("toLowerCase:" + Character.toLowerCase('A')); //toLowerCase:a System.out.println("toLowerCase:" + Character.toLowerCase('a')); //toLowerCase:a } }

更多详情指路:http://www.51gjie.com/java/600.html



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3