javaconfig没有定义名为“springSecurityFilterChain”的bean错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23572516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
No bean named 'springSecurityFilterChain' is defined error with javaconfig
提问by Carlos López
I'm having some problems adding spring security. It shows an error that says:No bean named 'springSecurityFilterChain' is defined
我在添加弹簧安全性时遇到了一些问题。它显示了一个错误:没有定义名为“springSecurityFilterChain”的 bean
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.
在我添加安全过滤器的那一刻,它显示了这个错误。我像疯了一样试图解决这个问题,但没有成功。
This is my WebSecurityConfigurerAdapter
这是我的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();
}
}
WebConfig
网络配置
@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/");
}
}
I've been trying a lot, but it always shows an 503 error.
我一直在尝试很多,但它总是显示 503 错误。
How can I fix it?
我该如何解决?
采纳答案by Charlires
Try registering the security filter this way
尝试以这种方式注册安全过滤器
FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
securityFilter.addMappingForUrlPatterns(null, false, "/*");
And add the @Import({WebSecurityConfig.class})
in the configuration class you declare as your rootContext in WebInitializerin your case is in App.java
并@Import({WebSecurityConfig.class})
在您在WebInitializer中声明为 rootContext 的配置类中添加您的情况在App.java 中
回答by Bal
You can simply create a class that extends from AbstractSecurityWebApplicationInitializer and it will automatically create/initialize the security filter chain for you. No code needed:
您可以简单地创建一个从 AbstractSecurityWebApplicationInitializer 扩展的类,它会自动为您创建/初始化安全过滤器链。不需要代码:
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {}
Also, if you're only just creating a single dispatcher servlet, you could consider simply extending your WebAppIntializer class from AbstractAnnotationConfigDispatcherServletInitializer:
此外,如果您只是创建一个调度程序 servlet,您可以考虑简单地从 AbstractAnnotationConfigDispatcherServletInitializer 扩展您的 WebAppIntializer 类:
public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?>[] getRootConfigClasses() {
return new Class[]{WebSecurityConfig.class, App.class};
}
@Override
protected Class<?>[] getServletConfigClasses() {
return new Class[]{WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{
"/"
};
}
回答by jyore
You do not actually need to add the security filter manually. You can extend AbstractSecurityWebApplicationInitializer
, which will insert the filter. You do not need to add any additional code than what is in the example below:
您实际上不需要手动添加安全过滤器。您可以扩展AbstractSecurityWebApplicationInitializer
,这将插入过滤器。除了以下示例中的代码外,您不需要添加任何其他代码:
package com.example.spring.security.config;
import org.springframework.core.annotation.Order;
import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;
@Order(1)
public class SecurityWebAppInitializer extends AbstractSecurityWebApplicationInitializer {
}
I typically will have my Security App Initializer with @Order(1)
and the standard Web App Initializer with @Order(2)
.
我通常会@Order(1)
使用带有@Order(2)
.
You also need to make sure that your component scan is setup correctly. I have had it pointing at a wrong package and I have gotten this error before.
您还需要确保您的组件扫描设置正确。我让它指向了一个错误的包,我以前也遇到过这个错误。