阅读文本大概需要3分钟。
虽然现在第三方登录微信、QQ、GitHub等大行其道,但是账密登录依然是一个系统最核心、最主要的功能之一。登录功能的好坏直接影响到系统的安全,而登录的流程、错误提示有直接关系到用户使用的体验效果。
账密登录有如下一些问题值得思考
-
账号的规则。纯数字,纯字母,还是数字和字母组合
-
密码的规则。纯数字,纯字母、数字和字母组合,数字、字母和特殊字符组合
-
密码使用什么加密方式传输。对称加密aes?还是非对称加密rsa。
-
存储密码时使用什么方式。md5?还是sha?还是多种方式混淆后,再加盐值。
今天和大家说下密码的校验规则:
0x01:纯数字
纯数字的加密规则大家想想在什么场景最常见?当然是银行卡的密码啦,无论是借记卡,还是借贷卡,都是千篇一律的是数字。
-
简单判断是否是数字
public static boolean testNumber(String pwd){ // Pattern pattern = Pattern.compile("[0-9]*"); Pattern pattern = Pattern.compile("^\\d+$"); Matcher isNum = pattern.matcher(pwd); if( !isNum.matches() ){ return false; } return true; }
-
判断是否是数字并且长度大于6
public static boolean testNumber(String pwd){ Pattern pattern = Pattern.compile("[0-9]{6,}"); // Pattern pattern = Pattern.compile("^\\d{6,}$"); Matcher isNum = pattern.matcher(pwd); if( !isNum.matches() ){ return false; } return true; }
-
判断是否是数字,长度大于6,并且不能存在大于等于3的连续字符。像A123879,这样的123连续了。
要找出连续字符,就要找两个字符串的最大子串,即查找目标密码字符串与123456789这个字符串的最大子串,最大子串的长度不能大于等于3
public static String getMaxSubStr(String s1, String s2) { String max = ""; String min = ""; max = (s1.length() > s2.length()) ? s1 : s2; min = (max == s1) ? s2 : s1; for (int x = 0; x < min.length(); x++) { for (int y = 0, z = min.length() - x; z != min.length() + 1; y++, z++) { String temp = min.substring(y, z); if (max.contains(temp)) return temp; } } return ""; }
判断是否是数字,长度大于6,并且不能存在大于等于3的连续字符
public static boolean testNumber(String pwd) { Pattern pattern = Pattern.compile("[0-9]{6,}"); // Pattern pattern = Pattern.compile("^\\d{6,}$"); Matcher isNum = pattern.matcher(pwd); String maxsub = getMaxSubStr("0123456789", pwd); if(maxsub.length()>=3){ return false; } if (!isNum.matches()) { return false; } return true; }
0x02:数字、大小写字母和特殊字符组合
对于纯字母、数字和字母这种密码就不在深入开展。下面说下目前互联网中使用最大的密码规则:数字、大小写字母和特殊字符组合
-
数字、大写字母、小写字母、特殊字符必须同时存在
public static boolean testPWD(String pwd) { Pattern pnum = Pattern.compile(".*[0-9]+.*"); Matcher isNum = pnum.matcher(pwd); if (!isNum.matches()) { return false; } Pattern pstr = Pattern.compile(".*[a-zA-Z]+.*"); Matcher isstr = pstr.matcher(pwd); if (!isstr.matches()) { return false; } Pattern sstr = Pattern.compile(".*[@!#%&]+.*"); Matcher sistr = sstr.matcher(pwd); if (!sistr.matches()) { return false; } return pwd.length()>=6; }
-
判断是否是数字、大写字母、小写字母、特殊字符组合,长度大于6,并且不能存在大于等于3的连续字符。像Abc879,这样的Abc连续了。
public static boolean testPWD(String pwd) { Pattern pnum = Pattern.compile(".*[0-9]+.*"); Matcher isNum = pnum.matcher(pwd); if (!isNum.matches()) { return false; } Pattern pstr = Pattern.compile(".*[a-zA-Z]+.*"); Matcher isstr = pstr.matcher(pwd); if (!isstr.matches()) { return false; } Pattern sstr = Pattern.compile(".*[@!#%&]+.*"); Matcher sistr = sstr.matcher(pwd); if (!sistr.matches()) { return false; } String number = "123456789"; String submax = getMaxSubStr(pwd, number); if(submax.length()>=3){ return false; } String strtpl = "abcdefghijklmnopqrstuvwsyz"; submax = getMaxSubStr(pwd, strtpl); if(submax.toLowerCase().length()>=3){ return false; } return pwd.length()>=6; }
0x03:总结
密码的校验规则可以很复杂,例如还是校验不能是一些常用的密码。例如admin12、user12等等,如果要做这些校验,就需要有一个常用密码库,然后与这个库的密码进行校验。如果存在就不能使用,否则就可以使用。
☆
往期精彩
☆
01 漫谈发版哪些事,好课程推荐
02 Linux的常用最危险的命令
03 精讲Spring Boot—入门+进阶+实例
04 优秀的Java程序员必须了解的GC哪些
05 互联网支付系统整体架构详解
关注我
每天进步一点点
喜欢!在看☟