春季安全性错误:java.lang.IllegalStateException:无法在自身之后配置anyRequest

时间:2019-11-23 02:33:10

标签: spring-boot spring-security

在尝试启动Spring Boot应用程序时出现此错误,我似乎无法弄清原因。我们非常感谢您的帮助。

  

org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / security / config / annotation / web / configuration / WebSecurityConfiguration.class]中创建名称为'springSecurityFilterChain'的bean时出错:通过工厂实例化Bean方法失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[javax.servlet.Filter]:工厂方法'springSecurityFilterChain'抛出了异常;嵌套的异常是java.lang.IllegalStateException:无法在自身之后配置anyRequest

super.configure(http)方法失败。 这是我的'SpringSecurityConfig.java'类

@Configuration
@EnableWebSecurity(debug = true)
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {
    @Autowired
    private CustomUserDetailService customUserDetailsService;

    @Bean
    public JWTAuthenticationFilter jwtAuthenticationFilter() {
        return new JWTAuthenticationFilter();
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .cors()
                .and()
                .csrf()
                .disable()
                .authorizeRequests()
                .antMatchers("/auth/**").permitAll()
                .anyRequest()
                .authenticated()
                .and()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS);

            http.addFilterBefore(jwtAuthenticationFilter(), UsernamePasswordAuthenticationFilter.class);

        super.configure(http);
    }




    @Bean
    public BCryptPasswordEncoder bCryptPasswordEncoder() {

        return new BCryptPasswordEncoder();
    }

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
        return super.authenticationManagerBean();
    }
}

完整堆栈跟踪

  

org.springframework.beans.factory.BeanCreationException:在类路径资源[org / springframework / security / config / annotation / web / configuration / WebSecurityConfiguration.class]中创建名称为'springSecurityFilterChain'的bean时出错:通过工厂实例化Bean方法失败;嵌套的异常是org.springframework.beans.BeanInstantiationException:无法实例化[javax.servlet.Filter]:工厂方法'springSecurityFilterChain'抛出了异常;嵌套的异常是java.lang.IllegalStateException:无法在自身之后配置anyRequest       在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:645)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:475)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1338)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1177)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:557)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.lambda $ doGetBean $ 0(AbstractBeanFactory.java:323)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:310)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:879)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)〜[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)〜[spring-context-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)〜[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]       在org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)上[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]       在org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:315)[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)上[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]       在org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)上[spring-boot-2.2.1.RELEASE.jar:2.2.1.RELEASE]       在lk.apiit.eirlss.bnco_vehicle_rental_backend.BncoVehicleRentalBackendApplication.main(BncoVehicleRentalBackendApplication.java:10)[classes /:na]       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_131]       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_131]       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_131]       在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_131]       在org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)[spring-boot-devtools-2.2.1.RELEASE.jar:2.2.1.RELEASE]   由以下原因引起:org.springframework.beans.BeanInstantiationException:无法实例化[javax.servlet.Filter]:工厂方法“ springSecurityFilterChain”引发了异常;嵌套的异常是java.lang.IllegalStateException:无法在自身之后配置anyRequest       在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:640)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       ...省略了26个通用框架   引起原因:java.lang.IllegalStateException:本身无法配置anyRequest       在org.springframework.util.Assert.state(Assert.java:73)〜[spring-core-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry.anyRequest(AbstractRequestMatcherRegistry.java:74)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.configure(WebSecurityConfigurerAdapter.java:355)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在lk.apiit.eirlss.bnco_vehicle_rental_backend.Auth.Config.SpringSecurityConfig.configure(SpringSecurityConfig.java:43)〜[classes /:na]       在org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.getHttp(WebSecurityConfigurerAdapter.java:231)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:322)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.init(WebSecurityConfigurerAdapter.java:92)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在lk.apiit.eirlss.bnco_vehicle_rental_backend.Auth.Config.SpringSecurityConfig $$ EnhancerBySpringCGLIB $$ 10db399e.init()〜[classes /:na]       在org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:370)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:324)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:41)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:104)〜[spring-security-config-5.2.1.RELEASE.jar:5.2.1.RELEASE]       在sun.reflect.NativeMethodAccessorImpl.invoke0(本机方法)〜[na:1.8.0_131]       在sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)〜[na:1.8.0_131]       在sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)〜[na:1.8.0_131]       在java.lang.reflect.Method.invoke(Method.java:498)〜[na:1.8.0_131]       在org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)〜[spring-beans-5.2.1.RELEASE.jar:5.2.1.RELEASE]       ...省略了27个共同的框架

1 个答案:

答案 0 :(得分:5)

由于要使用自定义安全配置,因此不应致电super.configure(http)

由于父configure(http)方法已经在调用.authorizeRequests().anyRequest().authenticated()并且错误消息中提到

,导致了错误
  

本身无法配置anyRequest