How to display error message in my JSP page using spring security 2.0

前端 未结 6 860
忘了有多久
忘了有多久 2021-01-05 11:52

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?

I have configured th

相关标签:
6条回答
  • 2021-01-05 12:13

    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

    0 讨论(0)
  • 2021-01-05 12:15
    <c:if test="${not empty param.login_error}">
        <div class="error">
            Your login attempt was not successful, try again.<br />
            Reason: #{sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
        </div>
    </c:if>
    
    0 讨论(0)
  • 2021-01-05 12:24

    Maybe you can try this...put

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
    

    for c tag on jsp page, and then put something like below for error msg display

    <c:when test="${param.error == 1}">
    <h3 style="font-size:20; color:#FF1C19;">Wrong id or password!</h3>
    </c:when>
    

    "param.error == 1" can get return value from Spring Security(security-config.xml) like

    <beans:bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
        <beans:property name="exceptionMappings">
            <beans:props>    
            <beans:prop key="org.springframework.security.core.userdetails.UsernameNotFoundException">/login.action?error=1</beans:prop>
            </beans:props>  
        </beans:property>
    </beans:bean>
    
    0 讨论(0)
  • 2021-01-05 12:32

    I am using spring-security v 4.0.3.RELEASE

    Using the not empty did not work for me. However checking for null worked for me.

    <c:if test="${ param.error ne null}">
        Display error message
    </c:if>
    
    0 讨论(0)
  • 2021-01-05 12:35

    This worked for me :

    <c:if test="${not empty sessionScope.SPRING_SECURITY_LAST_EXCEPTION}">
    <div class="error">
        Your login attempt was not successful, try again.<br />
        Reason: ${sessionScope.SPRING_SECURITY_LAST_EXCEPTION.message}
    </div>
    </c:if>
    
    0 讨论(0)
  • 2021-01-05 12:38

    This works

    <c:if test="${param.error != null}">
         Error
    </c:if>
    
    0 讨论(0)
提交回复
热议问题