Try to run validator for password

308 views Asked by At

I have validator: com.name.spring.model

    package com.websystique.springmvc.model;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import com.websystique.springmvc.service.UserService;

public class PasswordValidator implements Validator {

    @Autowired
    UserService service;

    @Override
    public boolean supports(Class<?> clazz) {
        return User.class.equals(clazz);
    }

    @Override
    public void validate(Object target, Errors errors) {
        User user = (User) target;

        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password", "required.password");
        ValidationUtils.rejectIfEmptyOrWhitespace(errors, "retypePassword", "required.retypePassword");


        if (!user.getPassword().equals(user.getRetypePassword())) {
            errors.rejectValue("retypePassword", "nomatch.password");
        }

    }

}

And this is how I activate validator: com.name.spring.controller

    @InitBinder
protected void initBinder(WebDataBinder binder) {
    binder.setValidator(PasswordValidator);
}

@RequestMapping(value = { "/register" }, method = RequestMethod.GET)
public String newUser(ModelMap model) {
    User user = new User();
    model.addAttribute("user", user);
    return "register";
}

@RequestMapping(value = { "/register" }, method = RequestMethod.POST)
public String saveUser(@Validated User user, BindingResult result, ModelMap model) {
    if (result.hasErrors()) {
        return "register";
    }
    if (!service.isUserEmailUnique(user.getId(), user.getEmail())) {
        FieldError emailError = new FieldError("user", "email", messageSource.getMessage("non.unique.email",
                new String[] { user.getEmail() }, Locale.getDefault()));
        result.addError(emailError);
        return "register";
    }
    service.saveUser(user);
    model.addAttribute("success", " User " + user.getFirstName() + " register success");
    return "success";
}

But I have this: ERROR OR ( or when its work I wrote password:apple123 and conf password: legolego) there is no validator, just add new user for data base :((( with incorrect password. (confirm password is @Transient

    HTTP Status 500 - Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.websystique.springmvc.model.User] during persist time for groups [javax.validation.groups.Default, ]

type Exception report

message Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.websystique.springmvc.model.User] during persist time for groups [javax.validation.groups.Default, ]

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is javax.validation.ConstraintViolationException: Validation failed for classes [com.websystique.springmvc.model.User] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='size must be between 10 and 30', propertyPath=email, rootBeanClass=class com.websystique.springmvc.model.User, messageTemplate='{javax.validation.constraints.Size.message}'}
]
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:973)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:863)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
    org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:837)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause

javax.validation.ConstraintViolationException: Validation failed for classes [com.websystique.springmvc.model.User] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='size must be between 10 and 30', propertyPath=email, rootBeanClass=class com.websystique.springmvc.model.User, messageTemplate='{javax.validation.constraints.Size.message}'}
]

Question 2) How to encrypt password, for mysql ?

0

There are 0 answers