校验——密码最少3中组合组成

本文介绍了一种后台密码强度验证的方法,确保密码由8到16位组成,并至少包含大写字母、小写字母、数字和符号中的三种组合。通过正则表达式匹配检查密码的安全性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

正常需求是密码要求8-16位,字符包含三种组合以上,前后端都可以校验,此处展示的是后台校验的相关代码

//传参中有个 BindingResult result
if (!isLowSafely(password)) {
		result.rejectValue("password", "password.fail","至少包含大写字母/小写字母/数字/符号3种组合");
	}


/**
	 * 判断密码安全级别
	 *
	 * @return boolean
	 */
	public boolean isLowSafely(String password) {
		int weight = 0;
		if (java.util.regex.Pattern.compile(".*\\d+.*").matcher(this.password).matches())
			weight ++;
		if (java.util.regex.Pattern.compile(".*[a-z]+.*").matcher(this.password).matches())
			weight ++;
		if (java.util.regex.Pattern.compile(".*[A-Z]+.*").matcher(this.password).matches())
			weight ++;
		if (java.util.regex.Pattern.compile(".*[!@#$%^&.]+.*").matcher(this.password).matches())
			weight ++;
		return weight >= 3;
	}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值