使用带有Spring数据休息和MVC的Validators

时间:2015-07-11 16:22:11

标签: java validation rest spring-mvc spring-data-rest

我正在使用spring-MVC应用程序和spring-data实体。然后为了将实体暴露为休息服务,我添加了spring-data-rest模块。 我正在尝试使用spring-data-rest beforeSaveEventListener Validators来验证同一个实体。

这些实体也是MVC的一部分,UI正在访问此实体。 当请求来自MVC应用程序时,不会调用这些验证器。

我如何拥有一个可供MVC和spring-data-rest servlet使用的通用验证器?而且,我不想使用从MVC到Rest servlet的uri重定向。有没有更清洁的方法来避免两个验证器一个用于MVC而另一个验证器用于Rest实体?

由于

1 个答案:

答案 0 :(得分:0)

很简单,我们可以在Spring Data Rest事件中重用MVC验证器。例如,如果我想在保存之前验证用户密码,我会将以下代码添加到之前的保存事件中 -

@Autowired
PasswordValidator passwordValidator;

@HandleBeforeSave
public void beforeUsersave(User user) {

            PasswordDTO passwordDTO = new PasswordDTO(user.getPassword(), user.getMatchingPassword());
            Errors errors = new BeanPropertyBindingResult(passwordDTO, "user");

            passwordValidator.validate(passwordDTO, errors);

            if (errors.hasErrors()) {
                throw new RepositoryConstraintViolationException(errors);
            }
}

我在更改密码调用中使用相同的PasswordValidator作为MVC验证器。

这是我的PasswordValidator

package org.amnesty.cms.domain.validation;

import org.springframework.util.StringUtils;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PasswordValidator implements Validator {

    public boolean supports(Class clazz) {
        return PasswordDTO.class.equals(clazz);
    }

    private Pattern pattern;
    private Matcher matcher;
    private static final String passwordRegex = "^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=\\S+$).{8,50}$";

    public PasswordValidator() {
        pattern = Pattern.compile(passwordRegex);
    }

    public void validate(Object passwordObj, Errors errors) {

        PasswordDTO passwordDTO = (PasswordDTO) passwordObj;
        String password = passwordDTO.getPassword();

        ValidationUtils.rejectIfEmpty(errors, "password", "password", "Password can't be null or empty");

        if (!StringUtils.isEmpty(password)) {

            if (password.length() < 8) {
                errors.rejectValue("password", "password", "Password length should be at least 8 characters");
            }

            if (password.length() > 50) {
                errors.rejectValue("password", "password", "Password length can't be more than 50 characters");
            }
        }

        if (!errors.hasErrors()) {
            matcher = pattern.matcher(password);
            if (!matcher.matches()) {
                errors.rejectValue("password", "password", "Invalid password. A valid password contains at least " +
                        "one digit, one lowercase character, one uppercase character and no whitespace");
            }
        }

        if (!errors.hasErrors() && !password.equals(passwordDTO.getMatchingPassword())) {
            errors.rejectValue("matchingPassword", "matchingPassword", "Matching password is different from password");
        }
    }
}