2016-03-07 68 views
2

我得到异常No bean named 'springSecurityFilterChain' is defined,当启动web应用程序,但不明白为什么。我的配置:Java Spring安全例外

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(securedEnabled = true) 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 
    @Inject private TokenProvider tokenProvider; 
    @Inject private CustomUserDetailsService userDetailsService; 
    @Inject private DefaultEntryPoint defaultEntryPoint; 
    @Inject 
    public void registerGlobalAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
     auth 
       .userDetailsService(userDetailsService) 
       .passwordEncoder(passwordEncoder()); 
    } 

    @Bean 
    @Override 
    public AuthenticationManager authenticationManagerBean() throws Exception { 
     return super.authenticationManagerBean(); 
    } 

    private XAuthTokenConfigurer securityConfigurerAdapter() { 
     return new XAuthTokenConfigurer(userDetailsService, tokenProvider); 
    } 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
     .and() 
       .exceptionHandling() 
       .authenticationEntryPoint(defaultEntryPoint) 
     .and() 
       .apply(securityConfigurerAdapter()) 
     .and() 
       .csrf().disable() 
       .authorizeRequests() 
       .antMatchers("/", "/login", "/logout").permitAll() 
       .antMatchers("/feed").authenticated() 
       .anyRequest().authenticated(); 
    } 
    @Bean 
    public TokenProvider tokenProvider(){ 
     String secret = "secret"; 
     int validityInSeconds = 172800; 
     return new TokenProvider(secret, validityInSeconds, 172800); 
    } 
    @Override 
    public void configure(WebSecurity web) throws Exception { 
     web.ignoring() 
       .antMatchers("/favicon.ico") 
       .antMatchers("/resources/**") 
       .antMatchers("/userresources/**"); 
    } 
    @Bean 
    public PasswordEncoder passwordEncoder(){ 
     return new BCryptPasswordEncoder(); 
    } 
} 

此安全初始化配置:

@Configuration 
public class SecurityInit extends AbstractSecurityWebApplicationInitializer { 
} 

和应用初始化:

@Configuration 
public class ApplicationInitializer implements WebApplicationInitializer { 

    public void onStartup(ServletContext servletContext) throws ServletException { 
     AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); 
     ctx.register(WebMvcConfig.class); 
     ctx.register(DatabaseConfig.class); 
     ctx.register(SecurityConfig.class); 

     ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); 

     ctx.setServletContext(servletContext); 

     servlet.setLoadOnStartup(1); 
     servlet.addMapping("/"); 

    } 
} 

有什么我错过了?

回答

0

在我的应用程序已经定义,这是web.xml中

<filter> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <filter-class> 
     org.springframework.web.filter.DelegatingFilterProxy 
    </filter-class> 
</filter> 

<filter-mapping> 
    <filter-name>springSecurityFilterChain</filter-name> 
    <url-pattern>/*</url-pattern> 
</filter-mapping> 

而且我在classpath弹簧网罐子。