2010-10-07 142 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> 
相关问题