javaconfig没有定义名为“springSecurityFilterChain"的bean错误 [英] No bean named 'springSecurityFilterChain' is defined error with javaconfig
问题描述
我在添加 Spring 安全性时遇到了一些问题.它显示一个错误:没有定义名为springSecurityFilterChain"的 bean
I'm having some problems adding spring security. It shows an error that says:No bean named 'springSecurityFilterChain' is defined
public class WebInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
// Create the 'root' Spring application context
AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext();
rootContext.register(App.class);
servletContext.addListener(new ContextLoaderListener(rootContext));
// security filter
servletContext.addFilter(
"springSecurityFilterChain",
new DelegatingFilterProxy("springSecurityFilterChain"))
.addMappingForUrlPatterns(null, false, "/*");
// Manage the lifecycle of the root application context
AnnotationConfigWebApplicationContext webContext = new AnnotationConfigWebApplicationContext();
webContext.register(WebConfig.class);
webContext.setServletContext(servletContext);
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(webContext));
servlet.addMapping("/");
servlet.setLoadOnStartup(1);
}
}
在我添加安全过滤器的那一刻,它显示了这个错误.我一直在疯狂地试图解决这个问题,但没有成功.
In the moment I add the security filter, it shows this error. I've been like crazy trying to resolve this with no success.
这是我的WebSecurityConfigurerAdapter
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.inMemoryAuthentication().withUser("tom").password("123456").roles("USER");
auth.inMemoryAuthentication().withUser("bill").password("123456").roles("ADMIN");
auth.inMemoryAuthentication().withUser("james").password("123456").roles("SUPERADMIN");
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/signin").access("hasRole('ROLE_ADMIN')")
.and().formLogin();
}
}
网络配置
@Configuration
@EnableWebMvc
@ComponentScan(value = {"com.hp.visitor.controller"})
@Import({ WebSecurityConfig.class })
public class WebConfig extends WebMvcConfigurerAdapter {
@Bean
UrlBasedViewResolver setupViewResolver(){
UrlBasedViewResolver resolver = new UrlBasedViewResolver();
resolver.setPrefix("/");
resolver.setSuffix(".jsp");
resolver.setViewClass(JstlView.class);
return resolver;
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("/static/");
}
}
我尝试了很多,但总是显示 503 错误.
I've been trying a lot, but it always shows an 503 error.
我该如何解决?
推荐答案
尝试以这种方式注册安全过滤器
Try registering the security filter this way
FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
securityFilter.addMappingForUrlPatterns(null, false, "/*");
并在您声明为 WebInitializer 中的 rootContext 的配置类中添加 @Import({WebSecurityConfig.class})
在您的情况下在 App.java
And add the @Import({WebSecurityConfig.class})
in the configuration class you declare as your rootContext in WebInitializer in your case is in App.java
这篇关于javaconfig没有定义名为“springSecurityFilterChain"的bean错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!