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("/");
}
}
有什麼我錯過了?