Spring 尽管已在 Java 配置中定义,但仍显示未找到安全过滤器
Spring Security Filter showing not found despite being defined in Java config
我已经使用基于 Java 的配置配置了我的 Spring 应用程序。当我启动我的应用程序时,出现错误 NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
。我已经定义了所有的配置 classes 但这个错误仍然出现。我该如何解决这个问题?
我的主要class:
public class SiteMain implements WebApplicationInitializer {
private void initSpringMvcServlet(ServletContext container) {
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(MvcConfig.class);
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private void initSpringRootContext(ServletContext container) {
XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
rootContext.setConfigLocation("/WEB-INF/site.xml");
container.addListener(new ContextLoaderListener(rootContext));
}
@Override
public void onStartup(ServletContext container) throws ServletException {
initSpringRootContext(container);
initSpringMvcServlet(container);
}
}
我的安全初始化程序 class 是:
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { }
我的 MVC 配置 class 是:
@Configuration
@EnableWebMvc
@ComponentScan
@Import(SecurityConfig.class)
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
}
}
我的安全配置 class 是:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/resources/**").permitAll().anyRequest().authenticated()
.and().formLogin().loginPage("/").permitAll()
.and().logout().permitAll()
.and().rememberMe();
}
}
尝试替换此行:
dispatcherContext.register(MvcConfig.class);
与:
dispatcherContext.setConfigLocation("your.config.package");
在下面添加一行:
container.addListener(new ContextLoaderListener(dispatcherContext));
不需要 @Import(SecurityConfig) 因为 setConfigLocation
会自动检测所有带注释的 @Configuration 类。
我已经使用基于 Java 的配置配置了我的 Spring 应用程序。当我启动我的应用程序时,出现错误 NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined
。我已经定义了所有的配置 classes 但这个错误仍然出现。我该如何解决这个问题?
我的主要class:
public class SiteMain implements WebApplicationInitializer {
private void initSpringMvcServlet(ServletContext container) {
AnnotationConfigWebApplicationContext dispatcherContext = new AnnotationConfigWebApplicationContext();
dispatcherContext.register(MvcConfig.class);
ServletRegistration.Dynamic dispatcher = container.addServlet("dispatcher", new DispatcherServlet(dispatcherContext));
dispatcher.setLoadOnStartup(1);
dispatcher.addMapping("/");
}
private void initSpringRootContext(ServletContext container) {
XmlWebApplicationContext rootContext = new XmlWebApplicationContext();
rootContext.setConfigLocation("/WEB-INF/site.xml");
container.addListener(new ContextLoaderListener(rootContext));
}
@Override
public void onStartup(ServletContext container) throws ServletException {
initSpringRootContext(container);
initSpringMvcServlet(container);
}
}
我的安全初始化程序 class 是:
public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer { }
我的 MVC 配置 class 是:
@Configuration
@EnableWebMvc
@ComponentScan
@Import(SecurityConfig.class)
public class MvcConfig extends WebMvcConfigurerAdapter {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/").setCachePeriod(31556926);
}
}
我的安全配置 class 是:
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/resources/**").permitAll().anyRequest().authenticated()
.and().formLogin().loginPage("/").permitAll()
.and().logout().permitAll()
.and().rememberMe();
}
}
尝试替换此行:
dispatcherContext.register(MvcConfig.class);
与:
dispatcherContext.setConfigLocation("your.config.package");
在下面添加一行:
container.addListener(new ContextLoaderListener(dispatcherContext));
不需要 @Import(SecurityConfig) 因为 setConfigLocation
会自动检测所有带注释的 @Configuration 类。