简体   繁体   English

spring 安全性:NoSuchBeanDefinitionException:找不到 [org.springframework.security.config.annotation.ObjectPostProcessor] 类型的合格 bean

[英]spring security: NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found

I'm trying to add spring-security to my rest app.我正在尝试将 spring-security 添加到我的休息应用程序中。 I followed the tutorial ( https://spring.io/guides/tutorials/spring-security-and-angular-js/ ) on spring website to do it but there it uses spring-boot component which I don't want to use, maybe problem is here.我按照 spring 网站上的教程( https://spring.io/guides/tutorials/spring-security-and-angular-js/ )来做到这一点,但它使用了我不想使用的 spring-boot 组件,也许问题就在这里。

My security configuration is here:我的安全配置在这里:

@Configuration
@Order(2147483636)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.httpBasic().and().authorizeRequests()
            .antMatchers("/rest", "/").permitAll().anyRequest()
            .authenticated().and().csrf()
            .csrfTokenRepository(csrfTokenRepository()).and()
            .addFilterAfter(csrfHeaderFilter(), CsrfFilter.class);
}

private Filter csrfHeaderFilter() {
    return new OncePerRequestFilter() {
        @Override
        protected void doFilterInternal(HttpServletRequest request,
                                        HttpServletResponse response, FilterChain filterChain)
                throws ServletException, IOException {
            CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class
                    .getName());
            if (csrf != null) {
                Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
                String token = csrf.getToken();
                if (cookie == null || token != null
                        && !token.equals(cookie.getValue())) {
                    cookie = new Cookie("XSRF-TOKEN", token);
                    cookie.setPath("/");
                    response.addCookie(cookie);
                }
            }
            filterChain.doFilter(request, response);
        }
    };
}

private CsrfTokenRepository csrfTokenRepository() {
    HttpSessionCsrfTokenRepository repository = new HttpSessionCsrfTokenRepository();
    repository.setHeaderName("X-XSRF-TOKEN");
    return repository;
    }
}

Stack trace:堆栈跟踪:

ERROR ContextLoader:353 - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'securityConfiguration': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setObjectPostProcessor(org.springframework.security.config.annotation.ObjectPostProcessor); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at weblogic.servlet.internal.EventsManager$FireContextListenerAction.run(EventsManager.java:678)
at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321)
at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:120)
at weblogic.servlet.provider.WlsSubjectHandle.run(WlsSubjectHandle.java:57)
at weblogic.servlet.internal.EventsManager.executeContextListener(EventsManager.java:243)
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:200)
at weblogic.servlet.internal.EventsManager.notifyContextCreatedEvent(EventsManager.java:185)
at weblogic.servlet.internal.WebAppServletContext.preloadResources(WebAppServletContext.java:1838)
at weblogic.servlet.internal.WebAppServletContext.start(WebAppServletContext.java:2876)
at weblogic.servlet.internal.WebAppModule.startContexts(WebAppModule.java:1661)
at weblogic.servlet.internal.WebAppModule.start(WebAppModule.java:823)
at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:360)
at weblogic.application.internal.ExtensibleModuleWrapper$StartStateChange.next(ExtensibleModuleWrapper.java:356)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)
at weblogic.application.internal.ExtensibleModuleWrapper.start(ExtensibleModuleWrapper.java:138)
at weblogic.application.internal.flow.ModuleListenerInvoker.start(ModuleListenerInvoker.java:124)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:216)
at weblogic.application.internal.flow.ModuleStateDriver$3.next(ModuleStateDriver.java:211)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)
at weblogic.application.internal.flow.ModuleStateDriver.start(ModuleStateDriver.java:73)
at weblogic.application.internal.flow.StartModulesFlow.activate(StartModulesFlow.java:24)
at weblogic.application.internal.BaseDeployment$2.next(BaseDeployment.java:729)
at weblogic.application.utils.StateMachineDriver.nextState(StateMachineDriver.java:42)
at weblogic.application.internal.BaseDeployment.activate(BaseDeployment.java:258)
at weblogic.application.internal.SingleModuleDeployment.activate(SingleModuleDeployment.java:48)
at weblogic.application.internal.DeploymentStateChecker.activate(DeploymentStateChecker.java:165)
at weblogic.deploy.internal.targetserver.AppContainerInvoker.activate(AppContainerInvoker.java:80)
at weblogic.deploy.internal.targetserver.operations.AbstractOperation.activate(AbstractOperation.java:587)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.activateDeployment(ActivateOperation.java:150)
at weblogic.deploy.internal.targetserver.operations.ActivateOperation.doCommit(ActivateOperation.java:116)
at weblogic.deploy.internal.targetserver.operations.AbstractOperation.commit(AbstractOperation.java:339)
at weblogic.deploy.internal.targetserver.DeploymentManager.handleDeploymentCommit(DeploymentManager.java:846)
at weblogic.deploy.internal.targetserver.DeploymentManager.activateDeploymentList(DeploymentManager.java:1275)
at weblogic.deploy.internal.targetserver.DeploymentManager.handleCommit(DeploymentManager.java:442)
at weblogic.deploy.internal.targetserver.DeploymentServiceDispatcher.commit(DeploymentServiceDispatcher.java:176)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.doCommitCallback(DeploymentReceiverCallbackDeliverer.java:195)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer.access$100(DeploymentReceiverCallbackDeliverer.java:13)
at weblogic.deploy.service.internal.targetserver.DeploymentReceiverCallbackDeliverer$2.run(DeploymentReceiverCallbackDeliverer.java:68)
at weblogic.work.SelfTuningWorkManagerImpl$WorkAdapterImpl.run(SelfTuningWorkManagerImpl.java:548)
at weblogic.work.ExecuteThread.execute(ExecuteThread.java:311)
at weblogic.work.ExecuteThread.run(ExecuteThread.java:263)

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire method: public void org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter.setObjectPostProcessor(org.springframework.security.config.annotation.ObjectPostProcessor); nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:659)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
... 54 more

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1326)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1072)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:967)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredMethodElement.inject(AutowiredAnnotationBeanPostProcessor.java:616)
... 56 more

The problem is that you do not have @EnableWebSecurity annotation on your SecurityConfiguration class.问题是您的 SecurityConfiguration 类上没有@EnableWebSecurity注释。

This would have been added by Spring-boot automatically, however since you opted for not using Spring-boot this needs to be taken care of manually .这本来是由 Spring-boot 自动添加的,但是由于您选择不使用 Spring-boot,因此需要手动处理

I use Spring Boot and trigger the same or similar issue with a custom subclass of WebSecurityConfigurerAdapter .我使用 Spring Boot 并使用WebSecurityConfigurerAdapter的自定义子类触发相同或类似的问题。 I needed to add @EnableWebSecurity to main class to work around the No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor<?>] exception.我需要将@EnableWebSecurity添加到主类以解决No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor<?>]异常No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor<?>] @EnableWebSecurity No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor<?>]

You need add @EnableWebSecurity and @Configuration , the code below is from WebSecurityConfigurerAdapter class, see exception message :您需要添加@EnableWebSecurity@Configuration ,以下代码来自WebSecurityConfigurerAdapter类,请参阅异常消息:

private ObjectPostProcessor<Object> objectPostProcessor = new ObjectPostProcessor<Object>() {
    public <T> T postProcess(T object) {
        throw new IllegalStateException(
                ObjectPostProcessor.class.getName()
                        + " is a required bean. Ensure you have used @EnableWebSecurity and @Configuration");
    }
};

检查您的<context:component-scan>以确保您的包裹已被覆盖。

The error message doesn't tell you what the root cause is.错误消息不会告诉您根本原因是什么。 I spent hours trying different answers.我花了几个小时尝试不同的答案。 Eventually, I fixed the problem when I found the right place to put the break point and find the exact exception with the exact class that threw the exception.最终,当我找到放置断点的正确位置并使用引发异常的确切类找到确切的异常时,我解决了问题。

Place a break point in buildMessage of the class: org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter.在类的 buildMessage 中放置一个断点:org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter。 There you can go up the stack to find the exact class that is causing the exception.您可以在堆栈中向上查找导致异常的确切类。

In my case a Spring Security configuration was accidently included in my applicationContext.就我而言,我的 applicationContext 中不小心包含了 Spring Security 配置。 So my solution was to exclude the Spring Security configuration.所以我的解决方案是排除Spring Security 配置。

Note笔记

This is not an answer to the question but a solution for the error message in the title.这不是问题的答案,而是标题中错误消息的解决方案。 So please consider other users who search for this error and find this post, before voting me down.因此,请考虑其他搜索此错误并找到此帖子的用户,然后再投票给我。

暂无
暂无

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

相关问题 没有 &#39;org.springframework.security.config.annotation.ObjectPostProcessor 类型的合格 bean<?> &#39; 当我使用 ContextHierarchy 时可用 - No qualifying bean of type 'org.springframework.security.config.annotation.ObjectPostProcessor<?>' available when I use ContextHierarchy NoSuchBeanDefinitionException:没有可用的“org.springframework.security.config.annotation.web.builders.HttpSecurity”类型的合格 bean - NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' available 找不到类型为[org.springframework.security.config.annotation.web.builders.HttpSecurity]的限定bean - No qualifying bean of type [org.springframework.security.config.annotation.web.builders.HttpSecurity] found for dependency 没有“org.springframework.security.core.userdetails.UserDetailsService”类型的合格bean - No qualifying bean of type 'org.springframework.security.core.userdetails.UserDetailsService' Spring Neo4j:NoSuchBeanDefinitionException:找不到依赖项类型为[org.springframework.data.neo4j.support.Neo4jTemplate]的合格bean - Spring Neo4j: NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.neo4j.support.Neo4jTemplate] found for dependency 没有类型为&#39;org.springframework.security.authentication.AuthenticationProvider&#39;的合格Bean - No qualifying bean of type 'org.springframework.security.authentication.AuthenticationProvider' 引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:没有符合条件的 bean 类型 - Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 由以下原因引起:org.springframework.beans.factory.NoSuchBeanDefinitionException:Spring 5中没有任何类型的合格Bean - Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type in Spring 5 Spring 引导:考虑在您的配置中定义类型为“org.springframework.security.config.annotation.web.builders.HttpSecurity”的 bean - Spring Boot : Consider defining a bean of type 'org.springframework.security.config.annotation.web.builders.HttpSecurity' in your configuration Spring单元测试的结果在NoSuchBeanDefinitionException中:没有类型为&#39;org.springframework.mail.javamail.JavaMailSender&#39;的合格bean - Spring unit test results in NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM