Advertisement
Guest User

Password Validator

a guest
Mar 7th, 2016
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.88 KB | None | 0 0
  1. package ch.hasselba.xpages.registration;
  2.  
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. import javax.faces.application.FacesMessage;
  7. import javax.faces.component.UIComponent;
  8. import javax.faces.component.UIInput;
  9. import javax.faces.context.FacesContext;
  10. import javax.faces.validator.Validator;
  11. import javax.faces.validator.ValidatorException;
  12.  
  13. import ch.hasselba.xpages.JSFUtils;
  14.  
  15. public class PasswordValidator implements Validator {
  16.  
  17.     private final static String VAR_NAME_PASSWORDRETYPE = "httpPasswordRetype";
  18.     private final static String ERR_MSG_PASSWORDS_NOMATCH = "Passwords are not equal.";
  19.     private final static String ERR_MSG_PASSWORDS_INSECURE = "Password must have a length of six characters and contain at least a digit, a lower and an upper character.";
  20.     private final static Pattern passwordPattern =  Pattern.compile("(?=.{6,})"   +     // "" followed by 6+ symbols
  21.             "(?=.*[a-z])" +     // --- ' ' --- at least 1 lower
  22.             "(?=.*[A-Z])" +     // --- ' ' --- at least 1 upper
  23.             "(?=.*[0-9])" +     // --- ' ' --- at least 1 digit
  24.             ".*");              // the actual characters
  25.     public void validate(FacesContext context, UIComponent component,
  26.             Object value) throws ValidatorException {
  27.  
  28.    
  29.         String password = (String) value;
  30.         UIInput confirmComponent = (UIInput) JSFUtils.resolveVariable(VAR_NAME_PASSWORDRETYPE);
  31.  
  32.         String confirm = (String) confirmComponent.getSubmittedValue();
  33.  
  34.         Matcher matcher = passwordPattern.matcher( password );
  35.         if( matcher.matches() == false ){
  36.             throw new ValidatorException(new FacesMessage(
  37.                     ERR_MSG_PASSWORDS_INSECURE ));
  38.         }
  39.        
  40.         // Compare the password with the confirm password.
  41.         if (!password.equals(confirm)) {
  42.             confirmComponent.setValid(false);
  43.             throw new ValidatorException(new FacesMessage(
  44.                     ERR_MSG_PASSWORDS_NOMATCH));
  45.         }  
  46.        
  47.  
  48.          
  49.  
  50.     }
  51.  
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement