2016-04-14 9 views
2

X509フィルタ(サブクラス:X509AuthenticationFilter)を使用したカスタムフィルタがあります。 X509フィルターの唯一の仕事は、要求から証明書を取り出すことです。資格情報が無効な場合は、対応するAuthenticationProviderがあり、BadCredentialsExceptionがスローされます。私の意図は、AuthenticationProviderBadCredentialsExceptionを投げたときに、BadCredentialsExceptionが要求をmessage.jspに転送することになるということです。しかし、それはそうしない。デバッグ時に、例外がスローされ、キャッチされ、WebAttributes.AUTHENTICATION_EXCEPTIONの下に格納されていることに気づきましたが、他のSpring SecurityクラスのどれもExceptionを利用していないようで、失敗ハンドラを使用する方法が見つかりません。Spring Security X.509認証で障害処理が機能しないのはなぜですか?

<security:http auto-config="false" pattern="/role/**" access-decision-manager-ref="adm" entry-point-ref="http403EntryPoint"> 
     <security:anonymous enabled="false"/> 
     <security:access-denied-handler error-page="/message.jsp"/> 
     <security:custom-filter ref="authFilter" position="PRE_AUTH_FILTER" /> 
</security:http> 

<bean id="http403EntryPoint" class="..." /> 

<bean id="authFilter" class="..."> 
    <property name="authenticationManager" ref="authenticationManager" /> 
    <property name="continueFilterChainOnUnsuccessfulAuthentication" value="false" /> 
</bean> 

私はauthFilterためPRE_AUTHとX509の両方のフィルタオプションを試してみました。また、continueFilterChainOnUnsuccessfulAuthenticationオプションがある場合とない場合のauthFilterを試しました。何も違いはなく、エラーは春のセキュリティでは処理されません。

私は何をしたいことです:春のセキュリティフレームワークが提供するエラー処理とX509認証を行うための

方法。私はSpring Securityがフィルタを事前認証またはX509のどちらかの位置に置く必要があるかもしれないとは思うが、それが「事前認証」であるかどうかは気にしない。私はどのようにこの作業または見て何を具体的に取得するためのヒントをしたいと思います。次のように(BadCredentialsExceptionが延びる)キャッチされないAuthenticationExceptionによるページにリダイレクトスプリングセキュリティにおける通常

+0

すべて、以下アブラハムの答えが作業を行いますが、この問題は正式に4.1のリリースですぐに固定され、春のセキュリティチームのチケットHTTPSを参照してください/ /github.com/spring-projects/spring-security/issues/3389 – KyleM

答えて

2

は、動作:

  1. FilterSecurityInterceptorAuthenticationExceptionをスロー。
  2. AuthenticationExceptionは、ExceptionTranslationFilterによって捕捉されます。
  3. ExceptionTranslationFilterは、認証を受けるentry-point-refに要求を送信します。

このサイクルのイベントは、end of the filter chainの近くで発生します。ただし、お客様のカスタムX509AuthenticationFilterAuthenticationProviderは、チェーンの早い段階で証明書を検証しています。リダイレクトに失敗した証明書の検証を取得するには、次の操作を行います

  1. はあなたAuthenticationManager/AuthenticationProviderを持っているあなたが現在行っているとして、BadCredentialsExceptionを投げます。
  2. X509AuthenticationFilterには、要求に関連付けられたAuthentication(継承された動作はAbstractPreAuthenticatedProcessingFilterである必要があります)を無効にすることができます。
  3. 別のFilterSecurityInterceptorをチェーン内に後でインストールして、Authenticationのないリクエストに対してAuthenticationExceptionを送信すると、要求がエントリポイントに送信されます。

次のようにこれを行うことができます。

<security:http auto-config="false" pattern="/role/**" entry-point-ref="failureMessageEntryPoint"> 
    <security:anonymous enabled="false"/> 
    <security:custom-filter ref="authFilter" position="PRE_AUTH_FILTER"/> <!-- authenticationManager will throw a BadCredentialsException, resulting in a null Authentication for the request --> 
    <security:intercept-url pattern="/role/**" access="isAuthenticated"/> <!-- will reject requests with a null Authentication by throwing an AuthenticationCredentialsNotFoundException --> 
                      <!-- AuthenticationExceptions will be caught by auto-configured ExceptionTranslationFilter, and sent to entry point --> 
</security:http> 

<bean id="failureMessageEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint"> <!-- not actually for login, but will work as an entry point that redirects to a page --> 
    <constructor-arg value="/message.jsp"/> 
</bean> 

<bean id="authFilter" class="..."> 
    <property name="authenticationManager" ref="authenticationManager"/> 
    <!-- let continueFilterChainOnUnsuccessfulAuthentication default to true to allow the intercept-url to reject the request --> 
</bean> 
+0

2つの関連する質問があります。フィルタの順序付けに関するSpring Securityのセクション(http://docs.spring.io/autorepo/docs/spring-security/4.1.0.RC1/reference/htmlsingle/#filter-ordering)では、ExceptionTranslationFilterは「認証処理メカニズム"それはあなたの答えと矛盾しませんか? 2番目の質問:httpチュートリアルでは、http403EntryPointを使用するように指示しています。しかし、http403EntryPointはAuthenticationExceptionの場合には実際には呼び出されません。あなたはhttp403EntryPointが必要ではないことに同意しますか? – KyleM

+0

'http403EntryPoint'は必要ないと私は同意します。通常、X509証明書の場合、認証は既にSpring Security以外で行われているため、認証のエントリポイントは存在しないため、プレースホルダエントリポイントです。そして、ええ、 'ExceptionTranslationFilter'の後に認証機構を持っているのは少しハッキリです。私は 'ExceptionTranslationFilter'の前にauth機構を残し、リダイレクトを強制するために別のフィルタを使うという答えを修正しました。 – heenenee

+0

よく考えた回答をありがとう。私はこのアプローチが好きです。私が欠けていた部分は、NULL認証プリンシパルがある場合、intercept-urlがAuthenticationExceptionをスローするということでした。これは、Spring Securityチームの奇妙なデザインの選択のようですが、FilterSecurityInterceptorはunsuccessfulAuthenticationメソッドのAbstractPreAuthenticatedProcessingFilterによって捕捉され、WebAttributes.AUTHENTICATION_EXCEPTIONキーの下に保存されていた以前のExceptionを無視します。 – KyleM

関連する問題