springSecurityFilterChain-ObjectPostProcessor是必需的bean异常



我正在使用Spring Security构建一个Spring Boot应用程序(Spring Boot starter web和Spring Boot starter安全(。在引导过程中,我收到来自应用程序的以下错误:

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 {
}

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

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

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

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

o.s.c.a.ConfigurationClassEnhancer:@Bean方法MyConfig.propertiesConfigurer是非静态的,并返回可分配给Spring的BeanFactoryPostProcessor接口的对象。这将导致无法在方法的声明@Configuration类中处理注释,如@Autowired、@Resource和@PostConstruct。为此方法添加"static"修饰符,以避免这些容器生命周期问题;有关完整的详细信息,请参阅@Bean javadoc。

将static添加到我的bean中解决了这个问题。

最新更新