2017-01-05 12 views
0

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 =「/インデックスにリダイレクトされますフォーム」

(すべてのクラスは、春のセキュリティ4に属している)私は(私は、Eclipseを使用しています)デバッガを走っていると、次のようにコードの実行は次のとおりです。

AbstractUserDetailsAuthenticationProvider.authenticate()CredentialsExpiredExceptionをスローします。 postAuthenticationChecks.check(user); 。

ExceptionMappingAuthenticationFailureHandler.onAuthenticationFailureは()/ResetPasswordのgetRedirectStrategy()のsendRedirect(要求、応答、URL)を呼び出す前にするURLを取得します。

DefaultRedirectStrategy.sendRedirect()問題がLoginUrlAuthenticationEntryPoint.commence(HttpServletRequestのリクエスト、HttpServletResponseの応答、含むAuthenticationException authException)で発生 "/ myappの/ ResetPasswordの" と

をリダイレクトURLを取得します。

useForwardはfalseに設定されているため、redirectUrl = buildRedirectUrlToLoginPage(request、response、authException)を呼び出します。

redirectUrlは "index/form"になります。私は私が手に含むAuthenticationExceptionのタイプInsufficientAuthenticationExceptionであるdetermineUrlToUseForThisRequest 上書きするために、真のサブクラスLoginUrlAuthenticationEntryPointにuseForwardを設定しても

興味深いのは、私のブラウザのURLはhttps://localhost:8443/myapp/resetpasswordですが、それはログインフォームです。

この問題が発生しましたか?もしそうなら、どのように春にリダイレクトしてパスワードをリセットするのですか?

私は事前にhttps://stackoverflow.com/a/14383194/158499

おかげで得られたコンフィギュレーションのほとんどは、ルーカス

答えて

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

/resetpasswordにリダイレクトするログインページにリダイレクトします。このURLは認証なしでアクセスできるようにしてください。

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

ありがとう、@ chaoluo。それは実際にトリックでした。私はこれを理解しようと2日間過ごしました!しかし、ユーザーが古いパスワードと新しいパスワードを入力するだけで済むように、ユーザー名をAuthenticationオブジェクト内に保存したい場合はどうすればよいでしょうか?ありがとう、もう一度。 –

関連する問題