首页 > 解决方案 > springSecurityFilterChain - ObjectPostProcessor 是必需的 bean 异常

问题描述

我正在使用 Spring Security(spring-boot-starter-web 和 spring-boot-starter-security)构建一个 Spring Boot 应用程序。我在启动期间从我的应用程序收到以下错误:

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.servlet.Filter]: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.IllegalStateException: org.springframework.security.config.annotation.ObjectPostProcessor is a required bean. Ensure you have used @EnableWebSecurity and @Configuration
        at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:625) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
        at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:455) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
        at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1288) ~[spring-beans-5.1.2.RELEASE.jar:5.1.2.RELEASE]
...

我的应用程序类包括以下内容:

@SpringBootApplication
public class CustomPropertiesApplication {

    public static void main(String[] args) {
        SpringApplication.run(CustomPropertiesApplication.class, args);
    }
}

下一个类中的 bean 似乎是问题所在。如果它被排除,那么应用程序将启动而不会出错。

@Configuration
@EnableWebSecurity
public class MyConfig extends WebSecurityConfigurerAdapter {

    @Bean
    public CustomPropertyPlaceholderConfigurer propertyConfigurer(ApplicationContext context) {
        return new CustomPropertyPlaceholderConfigurer();
    }

}

现在这个 CustomPropertyPlaceholderConfigurer 类什么都不做,我有一些类似的遗留类,但是在尝试解决这个问题时,我从我的测试应用程序中删除了所有其他内容。

public class CustomPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer {
}

我不知道下一步该尝试什么。我在 Spring Security 和 Spring Boot 中查找了有关构建自定义属性占位符配置器的详细信息,但没有发现任何有用的信息。

版本:Spring Boot - 2.1.0.RELEASE | Spring Security - 5.1.1.RELEASE | JDK 1.8

另外,我意识到这个应用程序并没有真正做任何事情,有一个更大的应用程序具有更复杂的逻辑,这里的这个示例应用程序只是为了复制我的问题以使其对 stackoverflow 来说很小。

标签: javaspringspring-bootspring-securityspring-web

解决方案


我现在看到答案在我的输出日志中是正确的,我只是没能看到它。

oscaConfigurationClassEnhancer :@Bean 方法MyConfig.propertyConfigurer 是非静态的,并返回一个可分配给 Spring 的 BeanFactoryPostProcessor 接口的对象。这将导致无法处理方法的声明@Configuration 类中的@Autowired、@Resource 和@PostConstruct 等注解。将“静态”修饰符添加到此方法以避免这些容器生命周期问题;有关完整的详细信息,请参阅@Bean javadoc。

向我的 bean 添加静态解决了这个问题。


推荐阅读