ToolsOh-密码生成器(KT:JAVA、zxcvbn、passay)

ToolsOh-密码生成器(KT:JAVA、zxcvbn、passay),第1张

ToolsOh-密码生成器(KT:JAVA、zxcvbn、passay) 功能描述

随机生成可靠、随机且唯一的密码



模块类图



页面地址

http://www.toolsoh.com/p/t/life/mimashengchengqi-passwordgenerator.html



界面展示
关键代码

maven引用

            
                org.passay
                passay
                1.6.1
            

java代码

import org.apache.commons.lang3.StringUtils;
import org.passay.*;
import org.springframework.stereotype.Component;
import java.util.ArrayList;import java.util.List;
import static org.passay.IllegalCharacterRule.ERROR_CODE;

@Component
public class PasswordGeneratorHelper {

    
    public String generatePassayPassword(int length, boolean lowerCaseFlag, boolean upperCaseFlag, boolean digitFlag, String special) {

        PasswordGenerator passwordGenerator = new PasswordGenerator();
        List rules = new ArrayList<>();

        if (lowerCaseFlag) {
            CharacterData lowerCaseChars = EnglishCharacterData.LowerCase;
            CharacterRule lowerCaseRule = new CharacterRule(lowerCaseChars);
            // 至少有1个小写字母
            lowerCaseRule.setNumberOfCharacters(1);
            rules.add(lowerCaseRule);
        }
        if (upperCaseFlag) {
            CharacterData upperCaseChars = EnglishCharacterData.UpperCase;
            CharacterRule upperCaseRule = new CharacterRule(upperCaseChars);
            upperCaseRule.setNumberOfCharacters(1);
            rules.add(upperCaseRule);
        }

        if (digitFlag) {
            CharacterData digitChars = EnglishCharacterData.Digit;
            CharacterRule digitRule = new CharacterRule(digitChars);
            digitRule.setNumberOfCharacters(1);
            rules.add(digitRule);
        }
        if (StringUtils.isNotBlank(special)) {
            CharacterData specialChars = new CharacterData() {
                public String getErrorCode() {
                    return ERROR_CODE;
                }

                public String getCharacters() {
                    return special;
                }
            };
            CharacterRule splCharRule = new CharacterRule(specialChars);
            splCharRule.setNumberOfCharacters(1);
            rules.add(splCharRule);
        }

        String password = passwordGenerator.generatePassword(length, rules);
        return password;
    }

    public static void main(String[] args) {
        String password = new PasswordGeneratorHelper().generatePassayPassword(16,true,true,true,"!@#$%^&");
        System.out.println(password);
    }
}

致谢开源
  • 开源项目作者: vt-middleware
  • 开源项目名称: passay
  • 开源项目地址: https://github.com/vt-middleware/passay 去star
  • 开源项目描述: Java的密码策略实施。Password policy enforcement for Java.

  • 开源项目作者: nulab
  • 开源项目名称: zxcvbn4j
  • 开源项目地址: https://github.com/nulab/zxcvbn4j 去star
  • 开源项目描述: 这是zxcvbn的一个java端口,是一个Javascript密码强度生成器。This is a java port of zxcvbn, which is a Javascript password strength generator.

END

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5437660.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论

登录后才能评论

评论列表(0条)

保存