Spring Security 4 - CredentialsExpiredException not redirecting to reset password - XML config

2.3k views Asked by At

I am using Spring-Security 4 XML configuration to successfully implement password authentication in a spring-mvc webapp.

The problem I have is that when CredentialsExpiredException is thrown by DaoAuthenticationProvider, the system redirects to login-form, instead of reset password.

My context-security xml configuration is as follow:

<?xml version="1.0" encoding="UTF-8"?>
<b:beans xmlns="http://www.springframework.org/schema/security"
xmlns:b="http://www.springframework.org/schema/beans" 
xmlns:p="http://www.springframework.org/schema/p"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans     http://www.springframework.org/schema/beans/spring-beans.xsd 
                    http://www.springframework.org/schema/security  http://www.springframework.org/schema/security/spring-security.xsd">

<b:bean id="authenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
    <b:constructor-arg value="/index/form" />
</b:bean>

<http auto-config="true" use-expressions="true" disable-url-rewriting="true" entry-point-ref="authenticationEntryPoint" >
    <intercept-url pattern="/" access="permitAll" requires-channel="https"/>
    <intercept-url pattern="/index" access="permitAll" requires-channel="https"/>
    <intercept-url pattern="/index/*" access="permitAll" requires-channel="https"/>
    <intercept-url pattern="/**" access="hasAnyRole('USER','SYS_ADMIN' )" requires-channel="https"/>
    <form-login   
            login-page="/index/form"
                default-target-url="/dashboard"
                login-processing-url="/index"
                username-parameter="username"
                password-parameter="password"
                authentication-failure-handler-ref="exceptionTranslationFilter"
                always-use-default-target="true"/>
    <logout logout-url="/logout" logout-success-url="/index/logout"/>

    <session-management>
        <concurrency-control max-sessions="1" error-if-maximum-exceeded="true" />
    </session-management>
</http>

 <!-- password expiry functionality starts  -->
<b:bean id="exceptionTranslationFilter" class="org.springframework.security.web.authentication.ExceptionMappingAuthenticationFailureHandler">
    <b:property name="exceptionMappings">
        <b:props>
            <b:prop key="org.springframework.security.authentication.CredentialsExpiredException">/resetpassword</b:prop>
        </b:props>
    </b:property>
    <b:property name="defaultFailureUrl" value="/index/error"/>
</b:bean>


 <b:bean id="authenticationManager" class="org.springframework.security.authentication.ProviderManager">
    <b:constructor-arg name="providers">
        <b:list>
            <b:ref bean="daoAuthenticationProvider"/>
        </b:list>
    </b:constructor-arg>
</b:bean> 

<b:bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider ">
    <b:property name="userDetailsService" ref="customUserDetailsService" />
    <b:property name="passwordEncoder" ref="passwordEncoder" />
</b:bean>



<authentication-manager alias="authenticationManager">
    <authentication-provider user-service-ref="customUserDetailsService" />
    <authentication-provider ref="daoAuthenticationProvider" /> 
</authentication-manager>


<b:bean id="passwordEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" >
</b:bean>

Given the above configuration, when I enter correct username and password of user whose credentials have expired, I get redirected to url = "/index/form"

I have ran the debugger (I'm using Eclipse), and the code execution is as follows (all classes belong to Spring Security 4):

AbstractUserDetailsAuthenticationProvider.authenticate() throws CredentialsExpiredException when executing postAuthenticationChecks.check(user);

ExceptionMappingAuthenticationFailureHandler.onAuthenticationFailure() gets the url to be /resetpassword before calling getRedirectStrategy().sendRedirect(request, response, url);

DefaultRedirectStrategy.sendRedirect() gets the redirect url to be "/myapp/resetpassword"

The problem occurs on LoginUrlAuthenticationEntryPoint.commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException).

Because useForward is set to false, it calls redirectUrl = buildRedirectUrlToLoginPage(request, response, authException);

redirectUrl ends up being "/index/form".

Even if I set useForward to true and subclass LoginUrlAuthenticationEntryPoint in order to override determineUrlToUseForThisRequest the AuthenticationException I get is of type InsufficientAuthenticationException.

The interesting thing is that the url on my browser is https://localhost:8443/myapp/resetpassword, but what it displays is the login form.

Have you encountered this problem before? If so, how did you get spring to redirect to reset password?

Most of the configuration I obtained from https://stackoverflow.com/a/14383194/158499

Thanks in advance, Lucas

1

There are 1 answers

1
chaoluo On BEST ANSWER
 <intercept-url pattern="/**" access="hasAnyRole('USER','SYS_ADMIN' )" requires-channel="https"/>

when redirect to /resetpassword will redirect to login page. Please allow this url to access without authentication.

<intercept-url pattern="/resetpassword" access="permitAll" requires-channel="https"/>