具有passay库的自定义密码验证器-在部署的应用程序上工作时发生意外异常

时间:2019-04-25 18:57:50

标签: java spring spring-security

在将其部署在herroku上的spring boot应用程序中,使用passay库配置的自定义密码验证程序存在问题。在本地主机上运行该应用程序时不会发生此问题,注册过程进行顺利(自定义密码验证程序也可以正常运行,没有麻烦)。

POM:

<dependency>
    <groupId>org.passay</groupId>
    <artifactId>passay</artifactId>
    <version>1.5.0</version>
</dependency>

PasswordConstraintValidator:

public class PasswordConstraintValidator implements ConstraintValidator<ValidPassword, String> {

private DictionaryRule dictionaryRule;

@Override
public void initialize(ValidPassword constraintAnnotation) {
    try {
        String invalidPasswordList = this.getClass().getResource("/static/invalid-password-list.txt").getFile();
        dictionaryRule = new DictionaryRule(
                new WordListDictionary(WordLists.createFromReader(
                        new FileReader[]{
                                new FileReader(invalidPasswordList)
                        },
                        false,
                        new ArraysSort()
                )));
    } catch (IOException e) {
        dictionaryRule = null;
    }
}

@Override
public boolean isValid(String password, ConstraintValidatorContext context) {
    PasswordValidator validator = new PasswordValidator(Arrays.asList(

            new LengthRule(8, 30),
            new CharacterRule(EnglishCharacterData.UpperCase, 1),
            new CharacterRule(EnglishCharacterData.LowerCase, 1),
            new CharacterRule(EnglishCharacterData.Digit, 1),
            new CharacterRule(EnglishCharacterData.Special, 1),
            new WhitespaceRule(),
            dictionaryRule
    ));

    RuleResult result = validator.validate(new PasswordData(password));

    if (result.isValid()) {
        return true;
    }

    List<String> messages = validator.getMessages(result);
    String messageTemplate = messages.stream().collect(Collectors.joining(","));
    context.buildConstraintViolationWithTemplate(messageTemplate)
            .addConstraintViolation()
            .disableDefaultConstraintViolation();
    return false;
}
}

意外的异常:

There was an unexpected error (type=Internal Server Error, status=500).
HV000028: Unexpected exception during isValid call.

我发现,当不使用dictionaryRule时,不会出现异常。我无法找出导致已部署应用程序出现问题的原因,希望您能指出这一点,谢谢。

0 个答案:

没有答案