首页 > 其他分享 >密码规则校验

密码规则校验

时间:2023-09-26 11:58:52浏览次数:44  
标签:String ++ 校验 密码 int static 规则 password REG

package com.infosec.ztpdp.policycenter.common.util;

import org.apache.commons.lang3.StringUtils;

/**
 * 
 * <p>
 * 规则: 1、长度大于8,且小于32 2、不能包含用户名 3、不能包含连续3位及以上相同字母或数字 4、不能包含3个及以上字典连续字符
 * 4、不能包含3个及以上键盘连续字符 4、数字、小写字母、大写字母、特殊字符,至少包含三种
 * </p>
 *
 * <p>
 * 版权所有:北京信安世纪科技股份有限公司(c) 2020
 * </p>
 *
 * @author: jlcui
 * @date: 2021年10月19日上午10:41:44
 *
 */
public class CheckPassword {
	/**
	 * 数字
	 */
	private static final String REG_NUMBER = ".*\\d+.*";
	/**
	 * 小写字母
	 */
	private static final String REG_UPPERCASE = ".*[A-Z]+.*";
	/**
	 * 大写字母
	 */
	private static final String REG_LOWERCASE = ".*[a-z]+.*";
	/**
	 * 特殊符号(~!@#$%^&*()_+|<>,.?/:;'[]{}\)
	 */
	private static final String REG_SYMBOL = ".*[~!@#$%^&*()_+|<>,.?/:;'\\[\\]{}\"]+.*";
	/**
	 * 键盘字符表(小写) 非shift键盘字符表
	 */
	private static final char[][] CHAR_TABLE1 = new char[][] {
			{ '1', '2', '3', '4', '5', '6', '7', '8', '9', '0', '-', '=', '\0' },
			{ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '[', ']', '\\' },
			{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', '\'', '\0', '\0' },
			{ 'z', 'x', 'c', 'v', 'b', 'n', 'm', ',', '.', '/', '\0', '\0', '\0' } };
	/**
	 * shift键盘的字符表
	 */
	private static final char[][] CHAR_TABLE2 = new char[][] {
			{ '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '\0' },
			{ 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', 'o', 'p', '{', '}', '|' },
			{ 'a', 's', 'd', 'f', 'g', 'h', 'j', 'k', 'l', ':', '"', '\0', '\0' },
			{ 'z', 'x', 'c', 'v', 'b', 'n', 'm', '<', '>', '?', '\0', '\0', '\0' } };


	/**
	 * 是否包含3个及以上连续相同的字母或数字
	 */
	public static boolean isContinuousSameThreeOrMoreNumbersOrLetters(String password) {
		if (StringUtils.isEmpty(password) || password.length() < 3) {
			return false;
		}
		char[] chars = password.toCharArray();
		for (int i = 0; i < chars.length - 2; i++) {
			String n1 = StringUtil.charToString(chars[i]);
			String n2 = StringUtil.charToString(chars[i+1]);
			String n3 = StringUtil.charToString(chars[i+2]);
			// 判断连续重复的字母或数字
			if (!n1.matches(REG_SYMBOL) && n1.equals(n2) && n1.equals(n3)) {
				return true;
			}
		}
		return false;
	}
	/**
	 * 密码是否至少包含三种字符以上
	 */
	public static boolean isContainThreeTypesChar(String password) {
		int i = 0;
		if (password.matches(REG_NUMBER))
			i++;
		if (password.matches(REG_LOWERCASE))
			i++;
		if (password.matches(REG_UPPERCASE))
			i++;
		if (password.matches(REG_SYMBOL))
			i++;
		if (i < 3) {
			return false;
		}
		return true;
	}
	 /**
     * 密码是否至少包含两种字符以上
     */
    public static boolean isContainTwoTypesChar(String password) {
        int i = 0;
        if (password.matches(REG_NUMBER))
            i++;
        if (password.matches(REG_LOWERCASE))
            i++;
        if (password.matches(REG_UPPERCASE))
            i++;
        if (password.matches(REG_SYMBOL))
            i++;
        if (i < 2) {
            return false;
        }
        return true;
    }
	/**
	 * 是否包含3个及以上键盘连续字符
	 *
	 * @param password 待匹配的字符串
	 */
	@SuppressWarnings("unused")
	private static boolean isKeyBoardContinuousChar(String password) {
		if (StringUtils.isEmpty(password)) {
			return false;
		}
		// 考虑大小写,都转换成小写字母
		char[] lpStrChars = password.toLowerCase().toCharArray();

		// 获取字符串长度
		int nStrLen = lpStrChars.length;
		// 定义位置数组:row - 行,col - column 列
		int[] pRowCharPos = new int[nStrLen];
		int[] pColCharPos = new int[nStrLen];
		for (int i = 0; i < nStrLen; i++) {
			char chLower = lpStrChars[i];
			pColCharPos[i] = -1;
			// 检索在表1中的位置,构建位置数组
			for (int nRowTable1Idx = 0; nRowTable1Idx < 4; nRowTable1Idx++) {
				for (int nColTable1Idx = 0; nColTable1Idx < 13; nColTable1Idx++) {
					if (chLower == CHAR_TABLE1[nRowTable1Idx][nColTable1Idx]) {
						pRowCharPos[i] = nRowTable1Idx;
						pColCharPos[i] = nColTable1Idx;
					}
				}
			}
			// 在表1中没找到,到表二中去找,找到则continue
			if (pColCharPos[i] >= 0) {
				continue;
			}
			// 检索在表2中的位置,构建位置数组
			for (int nRowTable2Idx = 0; nRowTable2Idx < 4; nRowTable2Idx++) {
				for (int nColTable2Idx = 0; nColTable2Idx < 13; nColTable2Idx++) {
					if (chLower == CHAR_TABLE2[nRowTable2Idx][nColTable2Idx]) {
						pRowCharPos[i] = nRowTable2Idx;
						pColCharPos[i] = nColTable2Idx;
					}
				}
			}
		}

		// 匹配坐标连线
		for (int j = 1; j <= nStrLen - 2; j++) {
			// 同一行
			if (pRowCharPos[j - 1] == pRowCharPos[j] && pRowCharPos[j] == pRowCharPos[j + 1]) {
				// 键盘行正向连续(asd)或者键盘行反向连续(dsa)
				if ((pColCharPos[j - 1] + 1 == pColCharPos[j] && pColCharPos[j] + 1 == pColCharPos[j + 1])
						|| (pColCharPos[j + 1] + 1 == pColCharPos[j] && pColCharPos[j] + 1 == pColCharPos[j - 1])) {
					return true;
				}
			}
			// 同一列
			if (pColCharPos[j - 1] == pColCharPos[j] && pColCharPos[j] == pColCharPos[j + 1]) {
				// 键盘列连续(qaz)或者键盘列反向连续(zaq)
				if ((pRowCharPos[j - 1] + 1 == pRowCharPos[j] && pRowCharPos[j] + 1 == pRowCharPos[j + 1])
						|| (pRowCharPos[j - 1] - 1 == pRowCharPos[j] && pRowCharPos[j] - 1 == pRowCharPos[j + 1])) {
					return true;
				}
			}
		}
		return false;
	}
}

  

标签:String,++,校验,密码,int,static,规则,password,REG
From: https://www.cnblogs.com/cuijinlong/p/17729748.html

相关文章

  • RHEL更改和重置根密码
    如果需要更改现有的根密码,可以以root用户或一个非root用户重置它。1、作为root用户更改root密码:您可以使用passwd命令以root用户身份更改root密码。先决条件:'root'访问权限流程:要更改'root'密码,使用:#passwd在修改前,会提示您输入您当前的密码。2、以非root......
  • centos scp传输不需密码
    注意事项:1、authorized_keys文件权限必须是600的,必须在家目录不一定要用root帐号,通过cat/etc/passwd可查看家目录位置2、authorized_keys文件内容格式多个用户追加新行起在Linux环境下,两台主机之间传输文件一般使用scp命令,通常用scp命令通过ssh获取对方linux主机文件的时候都......
  • 提交校验字段是否为数值(包含小数点)
    应用场景:如果一些已有数据的varchar字段,在后续想要更改为数据类型。除了在数据库备份alter的方法外,也可以在前端控制用户的输入。JS代码:<script>functionvalidateNum(value){//获取对应的值//编写正则表达式varreg=/^\d+(?=\.{0,1......
  • 蓝天采集器 采集规则设置
    1.创建任务2.采集规则设置点击任务后面的规则进入设置规则页面请求头建议开启,这样会伪装成蜘蛛访问3.起始页网址可以设置栏目页也可以设置列表页4.内容页网址内容页网址获取-选择正则(这种比较方便简单)操作完成记得保存,测试一下。  点击保存就好了,可以测试一下。......
  • 直播软件开发,随机密码生成器
    直播软件开发,随机密码生成器方法调用 publicstaticvoidmain(String[]args){    //排除字符0OoB81lI,包含大写字母,包含小写字母,包含数字,包含特殊字符,长度8,生成10000个,特殊字符集    generatePassword("0OoB81lI",true,true,true,true,8,10000,"~!@^*%");......
  • C#中实现校验是否包含中文与http接口地址中解析ip和端口号
    场景Winform/CSharp中实现对Http接口地址、IP地址字符串格式/合法性校验:https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/129616161在上面的基础上对某http接口地址(ip加端口号,示例http://127.0.0.1:9092)进行校验是否包含中文以及解析该地址获取ip和端口号博客:h......
  • JVS规则引擎,打造智能自动化决策的利器
    在日常的项目中,实时数据处理和自动化决策是智能化业务、灵活化配置的关键能力。为了满足这一需求,JVS规则引擎应运而生,它是一种高效的低代码/零代码平台,能够帮助企业快速构建各种应用场景,实现自动化、智能化决策的利器。一、JVS规则引擎简介JVS规则引擎是一种基于规则的自动化决策系......
  • 密码生成器
    //长度不低于8位。//包括大小写字母(A-Z和a-z)。//包括特殊符号(!@#$%^&*()-_=+[]{};:,.<>?/)functiongeneratePassword(){constchars='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_=+[]{};:,.<>?/';letpassword=&......
  • 2023-2024-1 20211306 密码系统设计与实现课程学习笔记3
    20211306密码系统设计与实现课程学习笔记3学习任务详情自学教材第10章,提交学习笔记大家学习过Python,C,Java等语言,总结一下一门程序设计语言有哪些必备的要素和技能?这些要素和技能在shell脚本中是如果呈现出来的?知识点归纳以及自己最有收获的内容,选择至少2个知识点利用......
  • OpenLDAP:使用Self Service Password管理用户密码
    安装dockeryum-yinstalldocker拉取镜像dockerpullgrams/ltb-self-service-password编辑配置文件<?php#==============================================================================#LTBSelfServicePassword##Copyright(C)2009ClementOUDOT#Co......