springmvc和springsecurity:springsecurityfilterchain错误

z2acfund  于 2021-07-14  发布在  Java
关注(0)|答案(1)|浏览(272)

我正在开发一个使用springmvc和springsecurity的webapp。在本地启动tomcat服务器时,出现以下错误:

23-Feb-2021 16:27:52.883 GRAVE [RMI TCP Connection(3)-127.0.0.1] org.apache.catalina.core.StandardContext.filterStart Exception au démarrage du filtre [springSecurityFilterChain]
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' available

我看到很多解决方案,但是使用xml配置,但是我不使用任何xml,我想通过java类来完成所有事情。你能帮助我吗?
这是我的webconfig.java:

import com.shieldsolutions.velocity.view.VelocityConfigurer;
import com.shieldsolutions.velocity.view.VelocityViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = "junia.projet.web.controller")
public class WebConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/webjars/**").addResourceLocations("/webjars/");
    }

    @Bean
    public VelocityConfigurer velocityConfigurer(){
        VelocityConfigurer velocityConfigurer= new VelocityConfigurer();
        velocityConfigurer.setResourceLoaderPath("/WEB-INF/velocity");
        return velocityConfigurer;};

    @Bean
    public VelocityViewResolver velocityViewResolver(){
        VelocityViewResolver velocityViewResolver=new VelocityViewResolver();
        velocityViewResolver.setSuffix(".vm");
        return velocityViewResolver;};

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        //login n'a pas de controller pour l'intercepter donc il faut configurer ici le lancement du template
        registry.addViewController("/").setViewName("login");
        registry.addViewController("/login").setViewName("login");
    }
}

我的securityconfig.jave:

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;

@Configuration
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter{

    @Bean
    public UserDetailsService userDetailsService() {
        InMemoryUserDetailsManager manager = new InMemoryUserDetailsManager();
        String password = passwordEncoder().encode("user");
        System.out.print("password " + password);
        manager.createUser(User.withUsername("user").password(password).roles("USER").build());
        return manager;
    }
    @Bean
    public NoOpPasswordEncoder passwordEncoder() {
        return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //permet à tous d'accéder à la page login
        http.authorizeRequests().antMatchers("/login").permitAll();
        //redirige tout le comde vers login s'il n'y a pas eu d'authentification
        http.authorizeRequests().anyRequest().authenticated().and().formLogin().loginPage("/login").and().logout().permitAll();
        http.csrf().disable();
    }

}

我的springsecurityinitializer.java

import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer;

public class SpringSecurityInitializer extends AbstractSecurityWebApplicationInitializer {
    //enregistrement automatique des filtres pour chaque URL
}

以及我的初始值:

import junia.projet.core.config.AppConfig;
import junia.projet.core.config.DBConfig;
import junia.projet.web.config.SecurityConfig;
import junia.projet.web.config.WebConfig;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;

public class Initializer extends AbstractAnnotationConfigDispatcherServletInitializer {

   @Override
   protected Class<?>[] getRootConfigClasses() {

       return new Class<?>[] { AppConfig.class, DBConfig.class };
   }

   @Override
   protected Class<?>[] getServletConfigClasses() {
       return new Class<?>[] { WebConfig.class, SecurityConfig.class};
   }

   @Override
   protected String[] getServletMappings() {
       return new String[] {"/"};
   }
}

谢谢你的帮助!

neskvpey

neskvpey1#

我解决了我的错误,这和往常一样有点愚蠢。在initializer.java中,securityconfig类应该放在GetRootConfigClass中,而不是放在GetServletConfigClass中。

相关问题