Spring security SpringSecurity4.x:如何获取身份验证失败期间使用的登录名?

Spring security SpringSecurity4.x:如何获取身份验证失败期间使用的登录名?,spring-security,Spring Security,当用户尝试使用错误的登录名/密码进行身份验证时,我希望重新显示所使用的登录名 在Spring Security 3.x中,以下代码允许我实现这一点: <input type="text" th:value="${session.SPRING_SECURITY_LAST_EXCEPTION != null ? session.SPRING_SECURITY_LAST_EXCEPTION.authentication.principal : ''}" name="login" value=""

当用户尝试使用错误的登录名/密码进行身份验证时,我希望重新显示所使用的登录名

在Spring Security 3.x中,以下代码允许我实现这一点:

<input type="text" th:value="${session.SPRING_SECURITY_LAST_EXCEPTION != null ? session.SPRING_SECURITY_LAST_EXCEPTION.authentication.principal : ''}" name="login" value="" />
这是因为
BadCredentialsException
现在


问题是:如何在SpringSecurity4.x中实现相同的行为?

我的解决方法如下:

进入my security-context.xml并向表单登录元素添加了身份验证失败处理程序ref属性


然后,我也在security-context.xml中为此创建了一个bean


接下来,我让我的LoginController扩展SimpleRulthenticationFailureHandler,添加了onAuthenticationFailure方法,该方法设置会话中的最后一个用户名并重定向到登录失败页面,在该页面中,控制器方法从会话中获取该用户名并将其放在模型上,然后将其从会话中删除

公共类LoginController扩展SimpleRuthenticationFailureHandler{
@RequestMapping(value=“/login”,method=RequestMethod.GET)
公共字符串登录(ModelMap模型){
返回“安全/登录”;
}
@RequestMapping(value=“/loginfailed”,method=RequestMethod.GET)
公共字符串登录错误(HttpServletRequest请求,ModelMap模型){
model.addAttribute(“错误”、“真实”);
model.addAttribute(“LAST_USERNAME”,request.getSession().getAttribute(“LAST_USERNAME”));
request.getSession().removeAttribute(“最后的用户名”);
返回“安全/登录”;
}
@凌驾
AuthenticationFailure(HttpServletRequest rq、HttpServletResponse rs、AuthenticationException e)上的公共无效引发IOException、ServletException{
rq.getSession().setAttribute(“最后一个_用户名”,rq.getParameter(“j_用户名”);
getRedirectStrategy().sendRedirect(rq,rs,“/loginfailed”);
}
}
它在视图中获取集合,如下所示


[2015-05-20 23:15:49.648] boot - 56559 ERROR [qtp83824030-29] --- ErrorController: Exception 'org.springframework.web.util.NestedServletException' occurred at page /account/auth
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "exceptionOccurred ? session.SPRING_SECURITY_LAST_EXCEPTION.authentication.principal : ''" (account/auth:82)
...
Caused by: org.thymeleaf.exceptions.TemplateProcessingException: Exception evaluating SpringEL expression: "exceptionOccurred ? session.SPRING_SECURITY_LAST_EXCEPTION.authentication.principal : ''" (account/auth:82)
...
Caused by: org.springframework.expression.spel.SpelEvaluationException: EL1008E:(pos 59): Property or field 'authentication' cannot be found on object of type 'org.springframework.security.authentication.BadCredentialsException' - maybe not public?
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.readProperty(PropertyOrFieldReference.java:227)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:94)
    at org.springframework.expression.spel.ast.PropertyOrFieldReference.getValueInternal(PropertyOrFieldReference.java:81)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueRef(CompoundExpression.java:57)
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:87)
    at org.springframework.expression.spel.ast.Ternary.getValueInternal(Ternary.java:56)
    at org.springframework.expression.spel.ast.SpelNodeImpl.getValue(SpelNodeImpl.java:120)
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:267)
    at org.thymeleaf.spring4.expression.SpelVariableExpressionEvaluator.evaluate(SpelVariableExpressionEvaluator.java:139)
    ... 94 more