Apache Shiro Form表单校验

a.直接跳转到login.jsp时可以使用GET方法,不携带参数

@RequestMapping(value = "/login", method = RequestMethod.GET)

b.form表单POST提交验证的时候会有验证,这时可以在POST中添加校验

@RequestMapping(value = "/login", method = RequestMethod.POST)
@RequestMapping(value = "/login", method = RequestMethod.GET)
public String loginForm(Model model) {
   return "/login";
}

@RequestMapping(value = "/login", method = RequestMethod.POST)
public String login(Model model, @Valid @ModelAttribute("validatorBean") ValidatorUser validatorBean, BindingResult result) {
    if (result.hasErrors()) {
        return loginForm(model);
    }
    String exceptionClassName = (String) request.getAttribute("shiroLoginFailure");

    logger.info(exceptionClassName);
    if (CaptchaException.class.getName().equals(exceptionClassName)) {
        result.rejectValue("error", "code.is.wrong");
        return loginForm(model);
    } else if (UnknownAccountException.class.getName().equals(exceptionClassName)) {
        result.rejectValue("error", "user.username.password.notmatch");
        return loginForm(model);
    } else if (IncorrectCredentialsException.class.getName().equals(exceptionClassName)) {
        result.rejectValue("error", "user.username.password.notmatch");
        return loginForm(model);
    } else if (ExcessiveAttemptsException.class.getName().equals(exceptionClassName)) {
        result.rejectValue("error", "user.locked");
        return loginForm(model);
    } else if (exceptionClassName != null) {
        result.rejectValue("error", "user.error.unknown");
        return loginForm(model);
    }

    return "redirect:/";
}