spring security不同的登录消息

时间:2013-09-18 22:48:09

标签: spring-security

我正在尝试使用不同的登录错误消息,例如“登录失败”或“内部系统错误”。或者甚至可能重定向到不同的页面。我尝试按照http://www.codemarvels.com/2010/12/spring-security-3-how-to-display-login-errors/所做的操作,除了我没有form-login标记,所以我无法将authentication-failure-handler-ref设置为在页面上显示。但是,我确实在FORM_LOGIN_FILTER上设置了authenticationFailureHandler(以使用ExceptionMappingAuthenticationFailureHandler),这是一个只扩展UsernamePasswordAuthenticationFilter的自定义类。

我在这里缺少什么?

EDIT-09/19/2013:我做了一些调试,我问的是错误的问题。它似乎工作正常。但是,我有多个身份验证提供程序,第一个例外的例外被第二个例外覆盖。基本上,我正在尝试模拟抛出的DB down异常,但是然后尝试对Active Directory进行身份验证,这样就完成了,我只是让用户找不到exc。因此,我没有得到我的AuthenticationServiceException。

我想我现在的问题是如何在不改变提供程序顺序的情况下从第一个问题中获取异常(数据库提供程序用于大多数用户)。

<http pattern="/**"  authentication-manager-ref='testAuthenticationManager' entry-point-ref="loginUrlAuthenticationEntryPoint" >
    <intercept-url pattern="/**" access="ROLE_USER" />
    <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
    <custom-filter position="FORM_LOGIN_FILTER" ref="customFormLoginFilter" />
     <session-management session-authentication-strategy-ref="sas"/>
 </http>

<!-- This just extends UsernamePasswordAuthenticationFilter -->
<beans:bean id="customFormLoginFilter" class="com.acme.security.CustomAuthenticationFilter" >
    <beans:property name="sessionAuthenticationStrategy" ref="sas" />
    <beans:property name="filterProcessesUrl" value="/j_spring_security_check" />
    <beans:property name="authenticationManager" ref="testAuthenticationManager"/>    
    <beans:property name="authenticationSuccessHandler" ref="successHandler"/>        
    <beans:property name="authenticationFailureHandler" ref="failureHandler" />        
</beans:bean>

<beans:bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter">
    <beans:property name="sessionRegistry" ref="sessionRegistry" />
    <beans:property name="expiredUrl" value="/login.jsp?errorCode=maxSessionsExceeded" />
</beans:bean>

<beans:bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
    <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
    <beans:property name="maximumSessions" value="1" />
</beans:bean>

<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl" />

<beans:bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
     <beans:property name="loginFormUrl" value="/login.jsp"/>
</beans:bean>

<beans:bean id="successHandler"
            class="org.springframework.security.web.authentication.SavedRequestAwareAuthenticationSuccessHandler">
    <beans:property name="defaultTargetUrl" value="/login/setup.htm"/>
    <beans:property  name="alwaysUseDefaultTargetUrl" value="true"/>        
</beans:bean>

<beans:bean id="failureHandler"
    class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
    <beans:property name="defaultFailureUrl" value="/login.jsp" />
    <beans:property name="exceptionMappings">
        <beans:props>
            <beans:prop key="org.springframework.security.authentication.AuthenticationServiceException">/login.jsp?errorCode=666</beans:prop>
        </beans:props>
    </beans:property>
</beans:bean>

0 个答案:

没有答案