2016-06-29 19 views
1

私のアプリケーションはAPIとブラウザの両方を提供します。私はすべてのカスタムプロバイダーとフィルターでAPIトークン認証を実装しました。設定がブラウザのバージョンを妨害しているようです。複数の認証プロバイダ(API +ブラウザ)を使用したSpringブート設定

私はドキュメントやその他の例を掘り下げた後にどこにも行かないので、解決方法について助言が必要な2つの質問があります。

1) がブラウザから送信されても​​、私のStatelessAuthenticationFilterが呼び出されています。私は持っています。リクエストマッチャーを "/ api/**"に指定しました。何故ですか?

2)AuthenticationManagerは2つを登録していません。AuthenticationProviderこれは間違って呼び出されているStatelessAuthenticationFilterをデバッグした後の私の結論です。あなたが見ることができるように

ここで私は

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig { 


    @Order(1) 
    @Configuration 
    public static class A extends WebSecurityConfigurerAdapter { 
     @Autowired 
     TokenAuthenticationProvider tokenAuthenticationProvider; 

     @Autowired 
     ApiEntryPoint apiEntryPoint; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      StatelessAuthenticationFilter filter = new StatelessAuthenticationFilter(); 
      AntPathRequestMatcher requestMatcher = new AntPathRequestMatcher("/api/**"); 
      filter.setRequiresAuthenticationRequestMatcher(requestMatcher); 
      filter.setAuthenticationManager(super.authenticationManager()); 

      http.csrf().disable() 
        .exceptionHandling().authenticationEntryPoint(apiEntryPoint) 
        .and() 
        .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
        .and() 
        .addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class); 
     } 

     @Override 
     protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.authenticationProvider(tokenAuthenticationProvider); 
     } 

     @Override 
     public void configure(WebSecurity web) throws Exception { 
      web.ignoring().antMatchers("/api/user/register"); 

     } 
    } 

    @Configuration 
    public static class B extends WebSecurityConfigurerAdapter { 
     @Override 
     protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.authenticationProvider(new DaoAuthenticationProvider()); 
     }  
    }  
} 

を持っている設定クラスだ、Bクラスが何も指定されていない、まだ私はローカルホストにアクセスするとき:8080 StatelessAuthenticationFilterが呼び出されます。ここで何が起こっているのですか?

答えて

0

クラスAでは、requestMatcherを使用するためにStatelessAuthenticationFilterを設定しています。あなたが何をするにしても、春はそれを知らない、または気にしません。また、それ以外のすべてのURIとStatelessAuthenticationFilter用に設定

http.antMatcher("/api/**") 

を使用してセキュリティ設定を制限する必要があり あなたが説明したとおりに、要求ごとに呼び出されます。

また、クラスAおよびBに@Orderを注釈する必要があります。例:multiple-httpsecurity

関連する問題