2016-11-07 3 views
3
@SuppressWarnings("SpringJavaAutowiringInspection") 
@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private JwtAuthenticationEntryPoint unauthorizedHandler; 

    @Autowired 
    private UserDetailsService userDetailsService; 

    @Autowired 
    public void configureAuthentication(AuthenticationManagerBuilder 
     authenticationManagerBuilder) throws Exception { 
     authenticationManagerBuilder.userDetailsService(userDetailsService); 
    } 

    @Bean 
    public JwtAuthenticationTokenFilter authenticationTokenFilterBean() throws Exception { 
     return new JwtAuthenticationTokenFilter(); 
    } 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     httpSecurity 
     .csrf().disable() 
     .exceptionHandling() 
      .authenticationEntryPoint(unauthorizedHandler) 
      .and() 
     .sessionManagement() 
      .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
      .and() 
     .authorizeRequests() 
      .antMatchers("/test").permitAll() 
      .antMatchers("/api/**").permitAll() 
      .anyRequest().authenticated(); 

     httpSecurity.addFilterBefore(authenticationTokenFilterBean(), UsernamePasswordAuthenticationFilter.class); 
    } 
} 

私はSpring Securityの前に実行されるカスタムフィルタを持っています。私はいくつかのURL(/testのような)をフィルタから除外し、春のセキュリティと他のものを傍受されるようにしたいと思っています(/api/**のように)。スプリングセキュリティでカスタムフィルタのURLを除外

郵便配達員を使用してlocalhost/testをテストするとき、私はantMatchers("/test").permitAll()を持っていてもまだフィルタを通過します。

フィルタをバイパスするにはどうすればよいですか?

答えて

3

あなたはWebSecurity#ignoringを参照してください、いくつかのURLの春のセキュリティフィルターチェーンを無効にすることができます

は春のセキュリティを無視すべきである必要があることをRequestMatcherインスタンスを追加することができます。 Spring Securityが提供するウェブセキュリティ(SecurityContextを含む)は、一致するHttpServletRequestでは利用できません。通常、登録されるリクエストは静的リソースのみである必要があります。動的なリクエストの場合は、代わりにすべてのユーザーを許可するようリクエストをマッピングすることを検討してください。

使用例:

webSecurityBuilder.ignoring() 
// ignore all URLs that start with /resources/ or /static/ 
      .antMatchers("/resources/**", "/static/**"); 

したがって、あなたはWebSecurityConfigurerAdapter#configureを上書きすることができます。

オーバーライドこのメソッドはWebSecurityを設定します。たとえば、特定の要求を無視する場合。

+0

@YoungHyunYoo:構成全体を除外したい場合は、antMatcher(antMatchersではなく)を使用することもできます。https://stackoverflow.com/questions/35890540/when-to-use-も参照してください。春のセキュリティ - antmatcher。 – dur

関連する問題