2016-12-22 10 views
0

WebSecurityConfigurerAdapterを拡張して、configureメソッドをオーバーライドします。 私の目標は、ログインページをバイパスし、手動認証を行う特定のURLにフィルタを配置することです。これを行うには、ユーザーがSECTION_WITH_FILTER URLに初めてアクセスしようとしたときにのみフィルタを実行する必要があります。Springカスタムフィルタは、指定されたURLで1回だけ実行されます

@Autowired 
MyFilter myFilter; 

@Override 
protected void configure(HttpSecurity http) throws Exception { 

    logger.debug("<CONFIGURE HTTPSECURITY/>"); 

    http 
      .authorizeRequests() 
      .antMatchers("/login*.*", "/error.*", "/resources/**", "/javax**/**").permitAll() 
      .antMatchers("/SECTION_WITH_FILTER/**").permitAll() 
      .antMatchers("/", "/*.jsf", "/**/*.jsf", "/*.faces", "/**/*.faces").hasAnyRole("ADMIN") 
      .anyRequest().authenticated() 
      .and() 
      .formLogin() 
      .loginPage("/login.jsf") 
      .permitAll() 
      .and() 
      .logout() 
      .invalidateHttpSession(false) 
      .logoutSuccessUrl("/") 
      .deleteCookies("JSESSIONID") 
      .permitAll() 
      .and().csrf().disable(); 
    http 
      .addFilterBefore(myFilter, BasicAuthenticationFilter.class); 

} 

私の問題は、すべてのウェブサイトのいずれかの形で提出した後にフィルターが始まるということです。私のコードです 。

私はすでにFilterRegistrationBeanは、ユーザーが特定のURLに行くときにのみフィルタを設定するためにも使用してみました:

@Bean 
public FilterRegistrationBean filterRegistration() { 

    FilterRegistrationBean registration = new FilterRegistrationBean(); 
    registration.setFilter(myFilter); 
    registration.addUrlPatterns("/SECTION_WITH_FILTER/**"); 
    registration.setName("myFilter"); 
    registration.setEnabled(true); 
    return registration; 
} 

しかしStackOverflowError例外がスローされます。このメソッドを使用して。

org.apache.catalina.core.StandardWrapperValve invoke 
GRAVE: Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Filter execution threw an exception] with root cause 
java.lang.StackOverflowError 
    at javax.servlet.http.HttpServletRequestWrapper.getSession(HttpServletRequestWrapper.java:231) 
    at org.apache.catalina.core.ApplicationHttpRequest.getSession(ApplicationHttpRequest.java:589) 
... 

どのように私は、ユーザーがそれに到達しようとするだけで、初めてを起動する単一のURLに指定したフィルタを作るために自分のコードを変更できますか?あなたがOncePerRequestFilter実装することができ おかげ

答えて

2

は、あなた自身のビジネスを行うdoFilterInternalメソッドをオーバーライドして、いくつかのURLをフィルタリングしてはならない場合、あなたはshouldNotFilterメソッドをオーバーライドすることができます。

このフィルタは、要求がすでにフィルタされていることを識別するために使用されます。既定の実装は、具象フィルタインスタンスの構成された名前に基づいています。

関連する問題