当前位置: 首页 > news >正文

密码工具类-生成随机密码校验密码强度是否满足要求

生成随机密码

符合密码强度的密码要求:

  • 至少有一个大写字母
  • 至少有一个小写字母
  • 至少有一个数字
  • 至少有一个特殊字符
  • 长度满足要求(通常为8-16位)
    // 大写字母private static final String UPPERCASE = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";// 小写字母private static final String LOWERCASE = "abcdefghijklmnopqrstuvwxyz";// 数字private static final String DIGITS = "0123456789";// 特殊字符(支持空格-下发密码明文时需要关注空格)private static final String SPECIAL_CHARS = "`~!@#$%^&*()-_=+[]{}\\|;:'\",.<>/? ";// 最小长度private static final int MIN_LENGTH = 8;// 最大长度private static final int MAX_LENGTH = 16;private static final SecureRandom random = new SecureRandom();/*** 生成随机密码* 满足强度要求* 长度随机** @return 生成的密码*/public static String generatePassword() {int length = MIN_LENGTH + random.nextInt(MAX_LENGTH - MIN_LENGTH);return generatePassword(length);}/*** 生成符合强密码要求的随机密码** @param length 密码长度* @return 生成的密码* @throws IllegalArgumentException 长度不满足要求*/public static String generatePassword(int length) {if (length < MIN_LENGTH || length > MAX_LENGTH) {throw new IllegalArgumentException("invalid password length.");}List<Character> passwordChars = new ArrayList<>();// 确保每类字符至少出现一次addRandomChar(passwordChars, UPPERCASE);addRandomChar(passwordChars, LOWERCASE);addRandomChar(passwordChars, DIGITS);addRandomChar(passwordChars, SPECIAL_CHARS);// 填充剩余字符String allChars = UPPERCASE + LOWERCASE + DIGITS + SPECIAL_CHARS;for (int i = 4; i < length; i++) {addRandomChar(passwordChars, allChars);}// 打乱字符顺序Collections.shuffle(passwordChars, random);// 构建密码字符串StringBuilder password = new StringBuilder();for (char c : passwordChars) {password.append(c);}return password.toString();}/*** 从指定字符串中随机选取一个字符并添加到密码列表中*/private static void addRandomChar(List<Character> list, String charSet) {int index = random.nextInt(charSet.length());list.add(charSet.charAt(index));}

校验密码强度有效性

    // "^(?=.*[A-Z])(?=.*[a-z])(?=.*\\d)(?=.*[\\Q`~!@#$%^&*()-_=+[]{}\\|;:'\",.<>/? \\E]).{8,16}$"private static final Pattern PATTERN_VALID_PASSWORD = Pattern.compile("^" +"(?=.*[A-Z])" +"(?=.*[a-z])" +"(?=.*\\d)" +"(?=.*[" + Pattern.quote(SPECIAL_CHARS) + "])" +".{" + MIN_LENGTH + "," + MAX_LENGTH + "}"+ "$");/*** 判断密码强度是否满足要求* 长度为8-16位(可按照实际修改)* 必须包含大写字母,小写字母,数字,特殊字符** @param input 带验证的密码明文* @return 是否满足强度要求*/public static boolean isValid(String input) {if (input == null || input.isEmpty()) {return false;}return PATTERN_VALID_PASSWORD.matcher(input).matches();}

单元测试

package com.ysx.utils.security;import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;import java.util.stream.Stream;import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;/*** @author youngbear* @email youngbear@aliyun.com* @date 2025-05-06 18:41* @blog <a href="https://blog.csdn.net/next_second">...</a>* @github <a href="https://github.com/YoungBear">...</a>* @description test for {@link PasswordUtils}*/
class PasswordUtilsTest {private static final Logger LOGGER = LoggerFactory.getLogger(PasswordUtilsTest.class);@Testvoid generatePasswordTestForRandomLength() {String password = PasswordUtils.generatePassword();LOGGER.info("password: {}, length: {}.", password, password.length());assertTrue(password.length() >= 8);assertTrue(password.length() <= 16);}@Testvoid generatePasswordTestForSpecifiedLength() {int length = 10;String password = PasswordUtils.generatePassword(length);LOGGER.info("password: {}, length: {}.", password, password.length());assertEquals(length, password.length());}private static Stream<String> validPasswordProvider() {return Stream.of("Abc@2025","2025~xyZ","(2020#Abc"," Abc2025","~!@#$%Aa0","2020$123$abc$ABC","Aa0`2025@2026^Xy");}private static Stream<String> inValidPasswordProvider() {return Stream.of(null,"","2025~xZ","2025~xZ12ab34CD56","(2020#abc","(2020#ABC","2020Abcd");}@ParameterizedTest(name = "#{index} - Run test with isValid = {0}")@MethodSource("validPasswordProvider")void test_isValid_input_valid(String input) {assertTrue(PasswordUtils.isValid(input));}@ParameterizedTest(name = "#{index} - Run test with isValid = {0}")@MethodSource("inValidPasswordProvider")void test_isValid_input_inValid(String input) {assertFalse(PasswordUtils.isValid(input));}
}

源代码地址

  • github
  • gitee

相关文章:

  • GPS定位方案
  • 使用阿里AI的API接口实现图片内容提取功能
  • three.js通过GEO数据生成3D地图
  • 2025年5月HCIP题库(带解析)
  • 基于计算机视觉的试卷答题区表格识别与提取技术
  • js var a=如果ForRemove=true,是“normal“,否则为“bold“
  • 网页版部署MySQL + Qwen3-0.5B + Flask + Dify 工作流部署指南
  • 自定义SpringBoot Starter-笔记
  • 当K8S容器没有bash时高阶排查手段
  • Github上如何准确地搜索开源项目
  • (二)毛子整洁架构(CQRS/Dapper/DomianEvent Handler)
  • 8.软考高项(信息系统项目管理师)-沟通管理
  • 作为主动唤醒的节点,ECU上电如何请求通讯
  • String、StringBuilder、StringBuffer的区别
  • 翻转二叉树(简单)
  • 使用原生javascript手动实现一个可选链运算符
  • 牛客——暴力、技巧、字符与数组的使用(强强联合、字符数量)
  • 【工具】解析URL获取实际图片地址下载原始FFHQ图像
  • C++:实现线程池
  • VMware中虚拟机和主机的SSH远程连接
  • 联想发布超级智能体矩阵,杨元庆:美国关税影响反映在产品定价上,未来不确定性很大
  • 安徽六安原市长潘东旭,已任省市场监督管理局党组书记、局长
  • 两国战机均未侵入对方领空,巴方公布对印回击细节
  • 48岁黄世芳履新中国驻毛里求斯大使,曾在广西工作多年
  • 国产基因测序仪龙头华大智造业绩持续承压,今年有望迎来拐点?
  • 山大齐鲁医院回应护士论文现“男性确诊子宫肌瘤”:给予该护士记过处分、降级处理