繁体   English   中英

没有名为'springSecurityFilterChain'的bean使用javaconfig定义错误

[英]No bean named 'springSecurityFilterChain' is defined error with javaconfig

我在增加弹簧安全性方面遇到了一些问题。 它显示一个错误:没有定义名为'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);
    }
}

在我添加安全过滤器的那一刻,它显示了这个错误。 我一直在疯狂地试图解决这个问题而没有成功。

这是我的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/");
    }

}

我一直在尝试很多,但它总是显示503错误。

我该如何解决?

您只需创建一个从AbstractSecurityWebApplicationInitializer扩展的类,它将自动为您创建/初始化安全过滤器链。 无需代码:

public class SecurityWebApplicationInitializer extends AbstractSecurityWebApplicationInitializer {}

此外,如果您只是创建一个调度程序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[]{
            "/"
        };
    }

尝试以这种方式注册安全过滤器

FilterRegistration.Dynamic securityFilter = servletContext.addFilter("springSecurityFilterChain", DelegatingFilterProxy.class);
    securityFilter.addMappingForUrlPatterns(null, false, "/*");

并在您声明为WebInitializer中的rootContext的配置类中添加@Import({WebSecurityConfig.class})App.java中

您实际上不需要手动添加安全筛选器。 您可以扩展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 {

}

我通常都会有我的安全应用程序初始化程序与@Order(1)和标准的Web应用程序初始化程序与@Order(2)

您还需要确保正确设置了组件扫描。 我已经把它指向错误的包,我之前得到了这个错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM