我正在使用Spring-Security 4 XML配置在spring-mvc webapp中成功實現密碼身份驗證。Spring Security 4 - CredentialsExpiredException不重定向到重置密碼 - XML配置
我遇到的問題是,當DaoAuthenticationProvider引發CredentialsExpiredException時,系統將重定向到登錄窗體而不是重置密碼。
我上下文的安全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 =「/指數/形式」
我已經跑了調試器(我使用Eclipse),並且代碼執行如下(所有類都屬於春季安全4):
AbstractUserDetailsAuthenticationProvider.authenticate()在執行postAuthenticationChecks.check(user)時拋出CredentialsExpiredException;
ExceptionMappingAuthenticationFailureHandler.onAuthenticationFailure()獲取URL爲/resetpassword主叫getRedirectStrategy()的sendRedirect(請求,響應URL)之前。
DefaultRedirectStrategy.sendRedirect()獲取重定向URL是 「/ MyApp的/ resetpassword」
上LoginUrlAuthenticationEntryPoint.commence(HttpServletRequest的請求,響應HttpServletResponse的,的AuthenticationException authException)將出現問題。因爲useForward設置爲false,所以它會調用redirectUrl = buildRedirectUrlToLoginPage(request,response,authException);否則,調用redirectUrl = buildRedirectUrlToLoginPage。
redirectUrl最終成爲「/ index/form」。
即使我爲了覆蓋determineUrlToUseForThisRequest我得到的AuthenticationException是型InsufficientAuthenticationException的設置useForward真和子類LoginUrlAuthenticationEntryPoint。
有趣的是,我瀏覽器上的網址是https://localhost:8443/myapp/resetpassword
,但它顯示的是登錄表單。
你以前遇到過這個問題嗎?如果是這樣,你是如何得到春季重定向重置密碼?
大多數我從https://stackoverflow.com/a/14383194/158499
感謝事先獲得的配置,盧卡斯
謝謝@chaoluo。這實際上是訣竅。我花了2天試圖弄清楚這一點!但是如果我想將用戶名保留在Authentication對象內,那麼用戶只需輸入舊密碼和新密碼呢?再次感謝。 –