Spring Security 4 - CredentialsExpiredException不重定向到重置密码 - XML config

时间:2017-01-05 13:31:14

标签: spring-mvc spring-security thymeleaf reset-password

我正在使用Spring-Security 4 XML配置在spring-mvc webapp中成功实现密码身份验证。

我遇到的问题是,当DaoAuthenticationProvider抛出CredentialsExpiredException时,系统会重定向到login-form,而不是重置密码。

我的context-security xml配置如下:

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

鉴于上述配置,当我输入凭据已过期的用户的正确用户名和密码时,我会被重定向到url =“/ index / form”

我运行了调试器(我正在使用Eclipse),代码执行如下(所有类都属于Spring Security 4):

AbstractUserDetailsAuthenticationProvider.authenticate()在执行postAuthenticationChecks.check(user)时抛出 CredentialsExpiredException ;

ExceptionMappingAuthenticationFailureHandler.onAuthenticationFailure()在调用 getRedirectStrategy()之前获取 / resetpassword 的网址.sendRedirect(request,response,url);

DefaultRedirectStrategy.sendRedirect()将重定向网址设为“/ myapp / resetpassword”

问题出现在 LoginUrlAuthenticationEntryPoint.commence(HttpServletRequest请求,HttpServletResponse响应,AuthenticationException authException)上。

因为useForward设置为false,所以它调用redirectUrl = buildRedirectUrlToLoginPage(request,response,authException);

redirectUrl 最终成为“/ index / form”。

即使我将 useForward 设置为true并将子类设置为LoginUrlAuthenticationEntryPoint以覆盖 determineUrlToUseForThisRequest ,我得到的AuthenticationException的类型为 InsufficientAuthenticationException

有趣的是我浏览器上的网址是https://localhost:8443/myapp/resetpassword,但它显示的是登录表单。

你以前遇到过这个问题吗?如果是这样,你是如何获得重定向重置密码的?

我从https://stackoverflow.com/a/14383194/158499

获得的大部分配置

提前致谢,卢卡斯

1 个答案:

答案 0 :(得分:2)

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

重定向到/resetpassword时会重定向到登录页面。请允许此网址无需身份验证即可访问。

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