弹出安全自定义错误消息未显示

时间:2014-01-15 17:54:16

标签: java spring authentication spring-security

我在这个链接上使用了一个教程:

  

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

要在登录表单上显示自定义错误消息,我在开头就知道了。但在为自定义故障验证器处理程序声明 authentication-failure-handler-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"/>

Spring Security:

    <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";

}

更新:要回答Mani的回答,我正在使用 LoginController ,它将 loginfailed 请求重定向到 login.jsp 并发送带有“ true ”值的“错误”属性。问题是我无法使用以下表达式在 login.jsp 上看到消息:

${sessionScope["SPRING_SECURITY_LAST_EXCEPTION"].message}

任何想法?非常感谢你!

2 个答案:

答案 0 :(得分:0)

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

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

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

编辑:

好的我明白了。在authentication-failure-handler-ref="myAuthErrorHandler" Spring之前处理失败案例并使用密钥SPRING_SECURITY_LAST_EXCEPTION在Session中放置消息。
添加故障处理程序后,即 authentication-failure-handler-ref="myAuthErrorHandler"
你必须处理错误。您可以在会话中设置消息。或者只是致电super.onAuthenticationFailure。你的经纪人没有做任何事情!!!

答案 1 :(得分: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 =用户名或密码无效

并保存并放入项目类路径

并在spring-contex.xml中添加bean

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