2015-11-04 4 views
5

スプリングブートアプリケーションで設定されたフィルタのコードは以下のとおりです。私の2番目のフィルタBは、私がリクエストをすると呼び出されません。スプリングセキュリティフィルタが呼び出されていません

import org.springframework.security.config.annotation.web.builders.HttpSecurity; 
import org.springframework.security.config.annotation.web.builders.WebSecurity; 
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; 
import org.springframework.security.config.annotation.web.servlet.configuration.EnableWebMvcSecurity; 

@Configuration 
@EnableWebMvcSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Override 
    public void configure(WebSecurity webSecurity) throws Exception { 
     webSecurity.ignoring().antMatchers(HttpMethod.GET, "/health"); 
    } 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS); 
     http.addFilterBefore(new A(), BasicAuthenticationFilter.class); 
     http.addFilterAfter(new B(), new A().getClass()); 
    } 
} 

import org.springframework.web.filter.GenericFilterBean; 
public class A extends GenericFilterBean { 

    @Override 
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) 
      throws IOException, ServletException { 
     System.out.println("filter A"); 
    } 
} 

import org.springframework.web.filter.GenericFilterBean; 
public class B extends GenericFilterBean { 

    @Override 
    public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) 
      throws IOException, ServletException { 
     System.out.println("filter B");   
    } 
} 

編集:

public class A extends GenericFilterBean { 

     @Override 
     public void doFilter(ServletRequest arg0, ServletResponse arg1, FilterChain arg2) throws IOException, ServletException { 
      System.out.println("filter A Before"); 
      arg2.doFilter(arg0,arg1); 
      System.out.println("filter A After"); 
     } 
} 
+0

少し拡張できますか?どのようなエラーメッセージが表示されますか?あなたの問題を解決するために何を試しましたか? – kebs

+0

まあ、エラーはありません。ちょうどフィルタBを呼び出さないだけです。 – Harshana

+1

あなたの 'doFilter'メソッドはリクエスト処理を停止します。処理を続行するには 'filterchain.doFilter(request、response);'を呼び出す必要があります。あなたは適切な処理を破っています。 –

答えて

4

あなたの設定は正しいです。しかし、Filter AからFilter BへのリクエストをM. Deinumで述べたように渡す必要があります。ちょうど印刷は動かない。あなたのコードでは、arg2.doFilter()Filter Aにする必要があります。

それは、この方法の

典型的な実装は次のパターンに従うだろう、と言うdocsから:必要に応じてカスタム実装を要求オブジェクトをラップ要求

  • を調べ

    1. を〜 入力フィルタリングのフィルタコンテンツまたはヘッダ
    2. 必要に応じて出力フィルタリング用 フィルタコンテンツ又はヘッダに
    3. いずれれるFilterChain オブジェクトを使用して、チェーン内の次のエンティティを呼び出す(chain.doFilterは())、またはしない要求を渡すカスタム実装と応答オブジェクトをラップ/レスポンスペア フィルタチェーン内の次のエンティティへのリクエスト 処理
    4. フィルタチェーン内の次の エンティティの呼び出し後に、応答に直接ヘッダーを設定します。
    関連する問題