私は休息アプリに春のセキュリティを追加しようとしています。私はそれを行うために春のウェブサイトでチュートリアル( https://spring.io/guides/tutorials/spring-security-and-angular-js/ )に従いましたが、そこではspring-bootコンポーネントを使用します使いたくない、多分問題はここにある。
私のセキュリティ構成は次のとおりです。
@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;
}
}
スタックトレース:
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
問題は、SecurityConfigurationクラスに@EnableWebSecurity
注釈がないことです。
ただし、これはSpring-bootを使用しないことを選択したため、Spring-bootによって自動的に追加されますこれは手動で行う必要があります。
<context:component-scan>
パッケージがカバーされていることを確認します。
Spring Bootを使用して、WebSecurityConfigurerAdapter
のカスタムサブクラスで同じまたは同様の問題をトリガーします。 @EnableWebSecurity
例外を回避するには、No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor<?>]
をメインクラスに追加する必要がありました。
エラーメッセージは根本原因が何であるかを教えてくれません。私は何時間もさまざまな答えを試してみました。最終的に、ブレークポイントを置く適切な場所を見つけて、例外をスローした正確なクラスで正確な例外を見つけると、問題を修正しました。
クラスのbuildMessageにブレークポイントを配置します:org.springframework.boot.diagnostics.LoggingFailureAnalysisReporter。そこで、スタックを上って、例外を引き起こしている正確なクラスを見つけることができます。