2014-01-15 40 views
0

我使用这个链接教程:春季安全自定义错误消息没有显示

http://www.mkyong.com/spring-security/display-custom-error-message-in-spring-security/

要显示的登录表单自定义错误消息,我得到了它的开始。但声明认证失败处理程序-REF =“myAuthErrorHandler”自定义故障处理程序认证后,我看不出它的登录表单的自定义消息:

enter image description here

我在做什么错误?任何人都可以解释我发生了什么,为什么'无效的用户名或密码'不显示?

下面的代码:

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} 

任何想法?非常感谢你!

回答

0

验证失败后,您要求重定向到不同的页面。

response.sendRedirect(request.getContextPath() 
     + "/abc/main/loginfailed"); 

您的登录失败的jsp不显示?它在登录页面中没有像errorblock那样的div?你在loginfailed.jsp中看到了不同的错误吗?

编辑:

好吧,我明白了。authentication-failure-handler-ref="myAuthErrorHandler"之前弹簧处理故障案例并将密钥放入会话中,密钥为SPRING_SECURITY_LAST_EXCEPTION
添加故障处理程序后,即
authentication-failure-handler-ref="myAuthErrorHandler"
您必须处理该错误。您可以在会话中设置消息。或者直接致电super.onAuthenticationFailure。你的处理程序没有做任何事情!

+0

我已经回答了你对这个职位的更新添加豆,但我无法弄清楚什么我做错了。也许你告诉我的重定向?我应该如何根据你对你的回答告诉我什么来正确地重定向它?谢谢。 – Ricardo

+0

@Ricardo F更新回答 – Mani

+0

好的,我应该如何重写AuthentificationListener.java来处理失败处理程序上的这类错误?解决这个问题后,我想使用这个处理程序来控制登录尝试更新尝试次数。但这是另一回事。谢谢您的帮助! – Ricardo

0
<div style="color: red"> 
    Your login attempt was not successful, try again.<br /> Caused : 
    ${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message} 
</div> 

使用此,清除形式的login.jsp

创建属性文件名作为

  • mymessages.properties
  • AbstractUserDetailsAuthenticationProvider.badCredentials =无效用户名或密码

并保存并放置到您的项目分类SPATH

,并在弹簧contex.xml

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basenames"> 
     <list> 
      <value>mymessages</value> 
     </list> 
    </property> 
</bean>