我使用这个链接教程:春季安全自定义错误消息没有显示
http://www.mkyong.com/spring-security/display-custom-error-message-in-spring-security/
要显示的登录表单自定义错误消息,我得到了它的开始。但声明认证失败处理程序-REF =“myAuthErrorHandler”自定义故障处理程序认证后,我看不出它的登录表单的自定义消息:
我在做什么错误?任何人都可以解释我发生了什么,为什么'无效的用户名或密码'不显示?
下面的代码:
AuthentificationListener
public class AuthentificationListener implements AuthenticationFailureHandler {
@Override
public void onAuthenticationFailure(HttpServletRequest request,
HttpServletResponse response, AuthenticationException ae)
throws IOException, ServletException {
UsernamePasswordAuthenticationToken user = (UsernamePasswordAuthenticationToken) ae
.getAuthentication();
/* User */
response.sendRedirect(request.getContextPath()
+ "/abc/main/loginfailed");
}
}
的applicationContext.xml:
<bean id="myAuthErrorHandler" class="org.abc.handler.AuthentificationListener"/>
春季安全:
<http auto-config="true">
<intercept-url pattern="/abc/main/welcome*" access="ROLE_USER" />
<intercept-url pattern="/abc/main/record/**" access="ROLE_USER" />
<form-login
login-page="/abc/main/login"
authentication-failure-handler-ref="myAuthErrorHandler"
default-target-url="/abc/main/welcome"
/>
<logout logout-success-url="/abc/main/logout" />
</http>
的login.jsp:
<c:if test="${not empty error}">
<div class="errorblock">
Your login attempt was not successful, try again.<br /> Caused :
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
</div>
</c:if>
LoginController.java:
@RequestMapping(value="/loginfailed", method = RequestMethod.GET)
public String loginerror(ModelMap model) {
model.addAttribute("error", "true");
return "login";
}
更新:要回答的答案玛尼,我m使用登录控制器,其重定向loginfailed请求的login.jsp并正在发送“错误”与“真”属性的值。问题是我无法看到使用这种表达的login.jsp消息:
${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}
任何想法?非常感谢你!
我已经回答了你对这个职位的更新添加豆,但我无法弄清楚什么我做错了。也许你告诉我的重定向?我应该如何根据你对你的回答告诉我什么来正确地重定向它?谢谢。 – Ricardo
@Ricardo F更新回答 – Mani
好的,我应该如何重写AuthentificationListener.java来处理失败处理程序上的这类错误?解决这个问题后,我想使用这个处理程序来控制登录尝试更新尝试次数。但这是另一回事。谢谢您的帮助! – Ricardo