2010-10-02 52 views
4

嗨我現在使用spring安全性。它工作正常。但是如果登錄失敗,則不會顯示錯誤消息。我想知道如何顯示錯誤信息?如何使用spring security 2.0在我的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> 

任何建議:

我在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> 

我的安全-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頁面中配置的ResourceBundleMessageSource會?任何幫助將不勝感激。

回答

3

jsp在哪裏顯示錯誤?像

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

如果您想自定義該消息的東西,你也應該看看this

+0

謝謝您的回覆。這有幫助,但如果使用Struts2呢?什麼是使用struts2的格式? – 2010-10-02 13:54:13

+0

你知道嗎? – 2010-10-02 13:58:55

11
<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> 
2

也許你可以試試這個...把

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

對於C標籤上jsp頁面,然後把下面的東西寫成如下錯誤信息msg顯示

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

「param.error == 1」 可以使用Spring Security(安全-config.xml中),如

<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> 
3

這工作

<c:if test="${param.error != null}"> 
    Error 
</c:if> 
1

這爲我工作得到返回值:

<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

我正在使用spring-security v 4.0.3.RELEASE

Usin g not empty不適合我。然而檢查null爲我工作。

<c:if test="${ param.error ne null}"> 
    Display error message 
</c:if> 
相關問題