2010-10-07 127 views
4

使用Spring Security 3與Struts 2和Tiles 2一起使用時,我有一個登錄頁面,當它應該執行並按預期執行登錄時出現 - 但是當我輸入錯誤的用戶憑據時,我返回到登錄頁面,沒有有關出錯的信息。我檢查了我所有的配置參數,並且看不清問題出在哪裏。爲什麼我沒有收到Spring Security登錄錯誤消息?

我的Spring Security XML配置如下:

<http auto-config="true" use-expressions="true"> 
    <intercept-url pattern="/" access="permitAll" /> 
    <intercept-url pattern="/css/**" access="permitAll" /> 
    <intercept-url pattern="/images/**" access="permitAll" /> 
    <intercept-url pattern="/js/**" access="permitAll" /> 
    <intercept-url pattern="/public/**" access="permitAll" /> 
    <intercept-url pattern="/home/**" access="permitAll" /> 
    <intercept-url pattern="/user/**" access="hasRole('AUTH_MANAGE_USERS')" /> 
    <intercept-url pattern="/group/**" access="hasRole('AUTH_MANAGE_USERS')" /> 
    <intercept-url pattern="/**" access="isAuthenticated()" /> 
    <access-denied-handler error-page="/403.html"/> 
    <form-login login-page="/public/login.do" always-use-default-target="false"/> 
    <logout invalidate-session="true" logout-success-url="/public/home.do"/> 
</http> 

我的Struts的Action看起來是這樣的:

<package name="public" namespace="/public" extends="secure"> 
    <action name="login"> 
     <result name="success" type="tiles">tiles.login.panel</result> 
     <result name="input" type="tiles">tiles.login.panel</result> 
     <result name="error">/WEB-INF/jsp/error.jsp</result> 
    </action> 
    <action name="logout"> 
     <result name="success" type="redirect">/j_spring_security_logout</result> 
    </action> 
</package> 

而且login.jsp頁面(瓷磚的一部分)查找異常來自Spring Security ...

<c:if test="${not empty param.login_error}"> 
    <span class="actionError"> 
    Your login attempt was not successful, try again.<br/><br/> 
     Reason: <c:out value="${SPRING_SECURITY_LAST_EXCEPTION.message}"/>. 
    </span> 
</c:if> 
<form id="loginForm" name="loginForm" action="/j_spring_security_check" method="post"> 
    ... 
</form> 

有誰能告訴我我失蹤了什麼?預先感謝任何/所有答覆。

回答

7

Spring Security未自動設置param.login_error。您需要如下做manaully:

<form-login 
    login-page="/public/login.do" 
    authentication-failure-url = "/public/login.do?login_error=1" 
    always-use-default-target="false"/> 
+0

完美。謝謝!我知道這是我在配置中丟失的東西。不幸的是,SPRING_SECURITY_LAST_EXCEPTION提供了一個非常技術性的錯誤,我無法向用戶顯示。原因:localhost:10389;嵌套異常是javax.naming.CommunicationException:localhost:10389 [根異常是java.net.ConnectException:連接被拒絕:連接]。 - 因爲我使用LDAP進行身份驗證。在我向用戶展示的範圍中是否有更有意義的消息 - 或者只是使用錯誤的用戶ID或密碼? – Griff 2010-10-07 18:30:04

+1

@Griff:我認爲'壞用戶ID或密碼'是出於安全原因更好,以便惡意的人不能看到什麼是錯的。 – axtavt 2010-10-07 18:33:17

+0

奇怪。 Spring的文檔說「默認爲/ spring_security_login?login_error,它將自動處理自動登錄頁面生成器...」。 它可以使用HTML(而不是JSP)嗎? – OhadR 2012-08-23 12:03:43

1

一個建議在最後的評論與錯誤信息的轉換幫助像被使用的AuthenticationFailureHandler到不同的異常類型不同的錯誤代碼映射的UI層代碼可以查找唯一的消息。它看起來像:

<security:form-login login-page="/login" 
    authentication-failure-handler-ref="authenticationFailureHandler"/> 

<bean id="authenticationFailureHandler" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler"> 
    <property name="defaultFailureUrl" value="/login?reason=login_error"/> 
    <property name="exceptionMappings"> 
     <map> 
     <entry><key><value>org.springframework.security.authentication.LockedException</value></key> 
      <value>/login?reason=user_locked</value></entry> 

      <entry><key><value>org.springframework.security.authentication.DisabledException</value></key> 
      <value>/login?reason=user_disabled</value></entry> 

      <entry><key><value>org.springframework.security.authentication.AuthenticationServiceException</value></key> 
      <value>/login?reason=connection</value></entry> 
     </map> 
    </property> 
</bean> 
相關問題