2015-03-13 95 views
1

我想配置一個基於Java的春季安全重定向到未經過身份驗證的任何請求登錄頁面,目前有以下配置:春季安全重定向到登錄表單與基於Java的配置

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    public void configure(WebSecurity security) throws Exception { 
     security 
       .ignoring() 
        .antMatchers("/resources/**") 
       ; 
    } 

    @Override 
    protected void configure(HttpSecurity httpSecurity) throws Exception { 
     httpSecurity 
       .formLogin() 
        .loginPage("/login").permitAll() 
       .and() 
       .authorizeRequests() 
        .anyRequest().authenticated() 
       ; 

    } 
} 

在實現WebApplicationInitializer我有以下類:

AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
rootContext.register(SecurityConfig.class); 
container.addListener(new ContextLoaderListener(rootContext)); 

configure(HttpSecurity httpSecurity)方法中設置一個斷點表明,該方法被調用在啓動時,但沒有請求重定向到/login

回答

0

要回答我自己的問題與其他人有類似的問題。解決辦法是增加一個新的空類,如下所示延伸AbstractSecurityWebApplicationInitializer

public class SecurityApplicationInitializer extends AbstractSecurityWebApplicationInitializer { 
    // Nothing 
}