public String generatePassword() { String retval = null; RandomStrg rs = newRandomStrg(); initRandomStrg(rs); int validationAttemptCount = 0; while (retval == null) { retval = rs.getRandom(); if (validator != null) { try { if (log.isDebugEnabled()) { log.debug("passing string " + retval + " to credential validator"); } validator.validate(retval); } catch (SecurityException sex) { if (validationAttemptCount >= maximumValidationAttempts) { log.warn("Unable to validate generated password after " + maximumValidationAttempts + " returning last generated password string unvalidated"); break; } retval = null; } } validationAttemptCount++; } if (log.isDebugEnabled()) { log.debug("returning string " + retval + " after " + validationAttemptCount + " validation attempts"); } return retval; }
/** * @return true is the password was invalid and update is required */ public boolean afterLoad(String userName, PasswordCredential credential, CredentialPasswordEncoder encoder, CredentialPasswordValidator validator) throws SecurityException { boolean updated = false; if (credential.getPassword() != null && !credential.isEncoded() && validator != null ) { try { validator.validate(credential.getPassword()); } catch (SecurityException e) { log.error("Loaded password for user "+userName+" is invalid. The user will be required to change it."); // persitent store contains an invalid password // allow login (assuming the user knows the invalid value) but enforce an update credential.setUpdateRequired(true); updated = true; } } return updated; } }
validator.validate(credential.getNewPassword());