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

手机选择网站市场营销互联网营销

手机选择网站,市场营销互联网营销,网站中数据查询如何做,成都品牌营销策划有限公司生成随机密码 符合密码强度的密码要求: 至少有一个大写字母至少有一个小写字母至少有一个数字至少有一个特殊字符长度满足要求(通常为8-16位) // 大写字母private static final String UPPERCASE "ABCDEFGHIJKLMNOPQRSTUVWXYZ";…

生成随机密码

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

  • 至少有一个大写字母
  • 至少有一个小写字母
  • 至少有一个数字
  • 至少有一个特殊字符
  • 长度满足要求(通常为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
http://www.cadmedia.cn/news/1084.html

相关文章:

  • 山西省建设厅网站打不开生成关键词的软件
  • 学校网站设计论文二十条优化措施原文
  • 今日油价95号汽油价格湖南网站seo地址
  • 山东城市建设招生网站百度竞价被换着ip点击
  • 成都企业网站建设那家好排名首页服务热线
  • wordpress header.php在哪里seo网站优化收藏
  • 东莞品牌网站建设网页制作步骤
  • 济南将开展治堵十大行动seo优化服务商
  • 网站建设"淘宝网" 在颜色选取和搭配方面有哪些值得学习的地方.在线seo优化工具
  • 泗阳网站定制公司网站的推广方案
  • 网站做中秋专题怎么弄郑州关键词排名公司电话
  • 交友网站建设要多少钱百度代运营公司
  • 一建 建设网站首页seo诊断站长
  • 大悟网站设计免费产品推广软件
  • 望京网站建设公司营销活动推广方案
  • 口碑好的福州网站建设网站建设关键词排名
  • 开发一个网站需要的技术百度应用宝
  • 网络绿化网站建设哪家专业百度店铺免费入驻
  • 公司做网站要注意什么淘宝推广运营
  • 网站设计与建设公司百度服务中心
  • 装修公司网站该怎么做站长工具seo综合查询分析
  • 手机网站建设咨询网络推广外包联系方式
  • seo关键词怎么优化seo网络推广哪家专业
  • 北京网络营销的培训课程淄博seo
  • 深圳做棋牌网站建设哪家服务好百度上做推广怎么收费
  • 郑州建设企业网站找哪个公司关键词优化和seo
  • 怎样申请注册公司网站整合营销活动策划方案
  • 潭州教育网站开发株洲网站设计外包首选
  • 网站站点建设端口号的作用域名推荐
  • 软件工程师发展前景优化营商环境的措施建议