Spring security对自定义登录表单执行验证
在调用authenticationManager进行身份验证之前,我需要对登录表单进行一些验证。在一个现有职位的帮助下能够实现这一目标: 有人能告诉我我是遵循正确的方法还是遗漏了什么吗?特别是,我不太清楚如何显示错误消息。 在过滤器中,我使用validator对登录字段执行验证,如果出现错误,我会抛出一个异常(扩展AuthenticationException)并封装errors对象。向exception类提供getErrors()方法来检索错误 因为在任何身份验证异常的情况下,故障处理程序将异常存储在会话中,所以在我的控制器中,我检查会话中存储的异常,如果存在异常,则使用从我的自定义异常检索的errors对象填充绑定结果(检查AuthenticationException的运行时实例后) 以下是我的代码快照- LoginFilter类Spring security对自定义登录表单执行验证,spring,spring-mvc,spring-security,Spring,Spring Mvc,Spring Security,在调用authenticationManager进行身份验证之前,我需要对登录表单进行一些验证。在一个现有职位的帮助下能够实现这一目标: 有人能告诉我我是遵循正确的方法还是遗漏了什么吗?特别是,我不太清楚如何显示错误消息。 在过滤器中,我使用validator对登录字段执行验证,如果出现错误,我会抛出一个异常(扩展AuthenticationException)并封装errors对象。向exception类提供getErrors()方法来检索错误 因为在任何身份验证异常的情况下,故障处理程序将异
public class UsernamePasswordLoginAuthenticationFilter extends
UsernamePasswordAuthenticationFilter {
@Autowired
private Validator loginValidator;
/* (non-Javadoc)
* @see org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter#attemptAuthentication(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
*/
@Override
public Authentication attemptAuthentication(HttpServletRequest request,
HttpServletResponse response) throws AuthenticationException {
Login login = new Login();
login.setUserId(request.getParameter("userId"));
login.setPassword(request.getParameter("password"));
Errors errors = new BeanPropertyBindingResult(login, "login");
loginValidator.validate(login, errors);
if(errors.hasErrors()) {
throw new LoginAuthenticationValidationException("Authentication Validation Failure", errors);
}
return super.attemptAuthentication(request, response);
}
}
控制器
@Controller
public class LoginController {
@RequestMapping(value="/login", method = RequestMethod.GET)
public String loginPage(@ModelAttribute("login") Login login, BindingResult result, HttpServletRequest request) {
AuthenticationException excp = (AuthenticationException)
request.getSession().getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION);
if(excp != null) {
if (excp instanceof LoginAuthenticationValidationException) {
LoginAuthenticationValidationException loginExcp = (LoginAuthenticationValidationException) excp;
result.addAllErrors(loginExcp.getErrors());
}
}
return "login";
}
@ModelAttribute
public void initializeForm(ModelMap map) {
map.put("login", new Login());
}
控制器中检查异常实例然后取出错误对象的这一部分看起来不是一种干净的方法。我不确定这是唯一的处理方法还是有人以任何其他方式处理它?请提供您的建议
谢谢
@RequestMapping(value = "/login", method = RequestMethod.GET)
public ModelAndView signInPage(
@RequestParam(value = "error", required = false) String error,
@RequestParam(value = "logout", required = false) String logout) {
ModelAndView mav = new ModelAndView();
//Initially when you hit on login url then error and logout both null
if (error != null) {
mav.addObject("error", "Invalid username and password!");
}
if (logout != null) {
mav.addObject("msg", "You've been logged out successfully.");
}
mav.setViewName("login/login.jsp");
}
现在,如果登录失败,那么它将再次点击这个url,并在其url中附加错误,就像您在spring安全文件中设置的失败url一样。
Spring安全文件:-authentication failure url=“/login?error=1”
然后您的URl变成URl/login?错误=1
然后,automatically signInPage方法将调用带有一些错误值的and。现在,error不为null,您可以设置与url对应的任何字符串,我们可以使用以下标记在jsp上显示:-
<c:if test="${not empty error}">
<div class="error">${error}</div>
</c:if>
${error}
为什么您希望/需要验证登录输入?凭据是好的,还是不好。似乎是非常自我验证…同样,您不想验证登录凭据除了登录尝试的成功或失败之外,因为您向可能的攻击者提供了比所需更多的信息吗?