弱口令检查示例

在系统运维管理中,因用户账号口令比较简单而导致被盗用的情况比较常见。为防止用户使用较为简单的口令,可以在用户登录系统时进行弱口令检测,如判断为弱口令则可强制要求用户修改成强度较高的口令。


一、前端检查

使用Javascript对用户输入口令进行简单检查。代码可以放在IdP前端登录页面。

检查标准如下:

(1)口令长度大于8位

(2)至少包括:小写字母、大写字母、数字、特殊字符4类中的2类字符


示例代码:inputCheck.html


二、后端检查

代码可放置在IdP认证系统后端。

检查标准如下:

(1)口令长度大于8位

(2)口令不与登录用户名相同,或口令不包含用户名

(3)口令不包含超过3位的相同字符,例如:1111,aaaa

(4)不含连续数字,例如:1234, 2345

(5)不能全是数字、小写字母、大写字母


示例代码(JAVA):checkSimplePWD.java

checkSimplePWD.java
/**
* @version: V1.0
* @author: CARSI
* @className: checkSimplePWD
* @packageName: checkSimplePWD
* @description: Check Simple password
* @data: 2020-06-29
* @param: 
*	userLoginPWD  String
*	userLoginID  String
* @return:
* 	true : simple password
* 	false : not simple password
*
*
* Simple password verify rule: 
* 1. password == userLoginID  OR password include userLoginID
* 2. password length < 8
* 3. password same numbers more than 3 , Like: 1111,aaaa
* 4. serial char more than 4, Like: 1234,4567
* 5. password is all numbers
* 6. password is all low char
* 7. password is all large char
*
*/
import java.util.regex.*;
public class checkSimplePWD {

public static boolean isSimple(String userLoginPWD, String userLoginID)
{ 
	/*  check password == userLoginID   OR  check password include userLoginID 	*/
	if( userLoginPWD.equals(userLoginID) || userLoginPWD.indexOf(userLoginID)>-1 )
	{
		return true;
	}
	if(userLoginPWD.length()<8)
	{
		return true;
	}
	Pattern pat = Pattern.compile("(.)\\1{3}"); // \\1 means match 1 group,{2} means repeat 2+1 times
	Matcher pmt = pat.matcher(userLoginPWD);
	if( pmt.find() )
	{
		return true;
	}
	Pattern patternN = Pattern.compile("[0-9]*");
	if(patternN.matcher(userLoginPWD).matches()==true)
	{
		return true;
	}
	Pattern patternC1 = Pattern.compile("[a-z]*");
	if(patternC1.matcher(userLoginPWD).matches()==true)
	{
		return true;
	}
	Pattern patternC2 = Pattern.compile("[A-Z]*");
	if(patternC2.matcher(userLoginPWD).matches()==true)
	{
		return true;
	}

	String strPat[] = {"0123","1234","2345","3456","4567","5678","6789","7890"};
	for(int p1=0;p1<strPat.length;p1++)
	{
		if(LoginUserPwd.indexOf(strPat[p1])>-1)
		{
			return true;
		}
	}
	return false;
}
public static void main(String args[])
{
	checkSimplePWD csp = new checkSimplePWD();
	System.out.println(csp.isSimple(args[0]));
}
}