如何使用Spring Security 2.0在我的JSP页面中显示错误消息 [英] How to display error message in my JSP page using spring security 2.0

查看:130
本文介绍了如何使用Spring Security 2.0在我的JSP页面中显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在使用Spring Security.它工作正常.但是,如果登录失败,则不会显示错误消息.我想知道如何显示错误消息?

Hi I am now using spring security. It works fine. But if login failed, no error message display. I am wondering how can I display error message?

我已经在applicationContext.xml中配置了ResourceBundleMessageSource

I have configured the ResourceBundleMessageSource in my applicationContext.xml

<!-- Spring security error message config -->
    <bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basenames">
             <list>
                  <value>messages</value>
             </list>
        </property>
    </bean>

还有我的security-config.xml

And my security-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:David="http://www.springframework.org/schema/security"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                           http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
                           http://www.springframework.org/schema/security
                           http://www.springframework.org/schema/security/spring-security-2.0.xsd">

    <David:http auto-config="true" access-denied-page="/accessDenied.html">

        <!-- Don`t set any role restriction on login.jsp -->
        <David:intercept-url pattern="/login.jsp"
            access="IS_AUTHENTICATED_ANONYMOUSLY" />

        <!-- Restrict access to All other pages -->
        <David:intercept-url pattern="/admin.jsp"
            access="ROLE_ADMIN" />

        <!-- Set the login page and what to do if login fails -->
        <David:form-login login-page="/login.jsp"
            default-target-url="/"/>
        <David:logout logout-success-url="/" />
    </David:http>

    <!-- Specify login examnination strategy -->
    <David:authentication-provider>
        <David:password-encoder hash="md5" />
        <David:jdbc-user-service
            data-source-ref="dataSource"
            users-by-username-query="select username, password, status as enabled from user where username=?"
            authorities-by-username-query="select u.username,r.name as authority
                                             from user u
                                             join user_role ur
                                               on u.id=ur.user_id
                                             join role r
                                               on r.id=ur.role_id
                                            where u.username=?" />
    </David:authentication-provider>
</beans>

我的jsp页面:

<%@ page contentType="text/html;charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<%@ page
    import="org.springframework.security.ui.AbstractProcessingFilter"%>
<%@ page
    import="org.springframework.security.ui.webapp.AuthenticationProcessingFilter"%>
<%@ page import="org.springframework.security.AuthenticationException"%>

<form id="myform" class="cmxform" method="post"
                            action="${pageContext.request.contextPath}/j_spring_security_check">
                            <fieldset>
                                <legend>
                                    Please input correct username and password to login
                                </legend>
                                <p>
                                    <label for="user">


                                        Username:
                                    </label>
                                    <input id="user" name="j_username" />
                                </p>
                                <p>
                                    <label for="pass">


                                        Password:
                                    </label>
                                    <input type="password" name="j_password" id="password" />
                                </p>
                                <p>
                                    <input type="submit" class="submit" value="Login" />
                                </p>
                            </fieldset>
                        </form>

有什么建议吗?任何帮助将不胜感激.

Any suggestions? Any help will be appreciated.

推荐答案

实际显示错误的jsp在哪里?像

Where is the jsp to actually display the error? Something like

<c:if test="${not empty param.error}">
    <!-- Display error message -->
</c:if>

如果要自定义此消息,还应该查看

If you want to customize this message, you should also look at this

这篇关于如何使用Spring Security 2.0在我的JSP页面中显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆