我试图使用基于Java的配置而不是来自此博客文章的XML配置将配置导入Spring Boot: http://blog.novoj.net/2012/03/27/combining-custom-annotations-for-securing-methods-with-spring-security/
我收到以下异常:
org.springframework.beans.factory.UnsatisfiedDependencyException:使用名称' postAuthorizeAdvisor'创建bean时出错。在类路径资源[test / project / security / aop / AopSecurityConfiguration.class]中定义:通过构造函数参数表达的不满意的依赖关系,类型为[org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor]的索引0 ::没有限定bean为依赖项找到的类型[org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor]:预计至少有一个bean有资格作为autowire候选者 依赖。依赖注释:{};嵌套异常是org.springframework.beans.factory.NoSuchBeanDefinitionException:找不到类型为[org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor]的限定bean依赖:预期至少有1个bean有资格作为此依赖项的autowire候选者。依赖注释:{}
以下是包含这些bean定义的类:
@Configuration
public class AopSecurityConfiguration {
@Bean
public MethodSecurityInterceptor methodSecurityInterceptor(AuthenticationManager authenticationManager) {
MethodSecurityInterceptor msi = new MethodSecurityInterceptor();
msi.setAuthenticationManager(authenticationManager);
msi.setValidateConfigAttributes(false);
MethodSecurityExpressionHandler handler = new DefaultMethodSecurityExpressionHandler();
ExpressionBasedAnnotationAttributeFactory factory = new ExpressionBasedAnnotationAttributeFactory(handler);
ExperimentalPrePostAnnotationSecurityMetadataSource metadataSource = new ExperimentalPrePostAnnotationSecurityMetadataSource(factory);
msi.setSecurityMetadataSource(metadataSource);
ExpressionBasedPreInvocationAdvice advice = new ExpressionBasedPreInvocationAdvice();
advice.setExpressionHandler(handler);
AccessDecisionVoter<?> voter = new PreInvocationAuthorizationAdviceVoter(advice);
AccessDecisionManager manager = new AffirmativeBased(Collections.singletonList(voter));
msi.setAccessDecisionManager(manager);
return msi;
}
@Bean
public Advisor preAuthorizeAdvisor(MethodSecurityInterceptor methodSecurityInterceptor) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(@(@org.springframework.security.access.prepost.PreAuthorize *) * *.* (..))");
return new DefaultPointcutAdvisor(pointcut, methodSecurityInterceptor);
}
@Bean
public Advisor preFilterAdvisor(MethodSecurityInterceptor methodSecurityInterceptor) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(@(@org.springframework.security.access.prepost.PreFilter *) * *.* (..))");
return new DefaultPointcutAdvisor(pointcut, methodSecurityInterceptor);
}
@Bean
public Advisor postAuthorizeAdvisor(MethodSecurityInterceptor methodSecurityInterceptor) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(@(@org.springframework.security.access.prepost.PostAuthorize *) * *.* (..))");
return new DefaultPointcutAdvisor(pointcut, methodSecurityInterceptor);
}
@Bean
public Advisor postFilterAdvisor(MethodSecurityInterceptor methodSecurityInterceptor) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(@(@org.springframework.security.access.prepost.PostFilter *) * *.* (..))");
return new DefaultPointcutAdvisor(pointcut, methodSecurityInterceptor);
}
}
是否有找不到MethodSecurityInterceptor bean的原因?
修改
这是完整的堆栈跟踪:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedServletContainerCustomizerBeanPostProcessor': BeanPostProcessor before instantiation of bean failed; nes
ted exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aopSecurityConfiguration' defined in file [C:\test\test-project\build\classes\main\test\project
\security\aop\AopSecurityConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postF
ilterAdvisor' defined in class path resource [test/project/security/aop/AopSecurityConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springf
ramework.security.access.intercept.aopalliance.MethodSecurityInterceptor]: : No qualifying bean of type [org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor] found for
dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionEx
ception: No qualifying bean of type [org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor] found for dependency: expected at least 1 bean which qualifies as autowire can
didate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.context.support.PostProcessorRegistrationDelegate.registerBeanPostProcessors(PostProcessorRegistrationDelegate.java:232)
at org.springframework.context.support.AbstractApplicationContext.registerBeanPostProcessors(AbstractApplicationContext.java:615)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:465)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:957)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:946)
at test.project.Application.main(Application.java:15)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'aopSecurityConfiguration' defined in file [C:\test\test-project\build\classes\main\test\project\secur
ity\aop\AopSecurityConfiguration.class]: Initialization of bean failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postFilterA
dvisor' defined in class path resource [test/project/security/aop/AopSecurityConfiguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframewo
rk.security.access.intercept.aopalliance.MethodSecurityInterceptor]: : No qualifying bean of type [org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor] found for depend
ency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionExceptio
n: No qualifying bean of type [org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor] found for dependency: expected at least 1 bean which qualifies as autowire candidate
for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:547)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:85)
at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessBeforeInstantiation(AbstractAutoProxyCreator.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:984)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.resolveBeforeInstantiation(AbstractAutowireCapableBeanFactory.java:955)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:466)
... 13 common frames omitted
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'postFilterAdvisor' defined in class path resource [test/project/security/aop/AopSecurityConf
iguration.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor]: : No qualify
ing bean of type [org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dep
endency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.access.intercept.aopa
lliance.MethodSecurityInterceptor] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:749)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:464)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:85)
at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:324)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.getEarlyBeanReference(AbstractAutoProxyCreator.java:232)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getEarlyBeanReference(AbstractAutowireCapableBeanFactory.java:819)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$2.getObject(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:192)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:173)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:240)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:85)
at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:324)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.getEarlyBeanReference(AbstractAutoProxyCreator.java:232)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.getEarlyBeanReference(AbstractAutowireCapableBeanFactory.java:819)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$2.getObject(AbstractAutowireCapableBeanFactory.java:529)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:192)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:173)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:240)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:368)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1119)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1014)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:504)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
at org.springframework.aop.framework.autoproxy.BeanFactoryAdvisorRetrievalHelper.findAdvisorBeans(BeanFactoryAdvisorRetrievalHelper.java:92)
at org.springframework.aop.framework.autoproxy.AbstractAdvisorAutoProxyCreator.findCandidateAdvisors(AbstractAdvisorAutoProxyCreator.java:101)
at org.springframework.aop.aspectj.annotation.AnnotationAwareAspectJAutoProxyCreator.findCandidateAdvisors(AnnotationAwareAspectJAutoProxyCreator.java:85)
at org.springframework.aop.aspectj.autoproxy.AspectJAwareAdvisorAutoProxyCreator.shouldSkip(AspectJAwareAdvisorAutoProxyCreator.java:103)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:324)
at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:293)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:422)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1579)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539)
... 35 common frames omitted
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.access.intercept.aopalliance.MethodSecurityInterceptor] found for d
ependency: 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:1301)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:813)
at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:741)
... 95 common frames omitted
修改
我能够通过删除WebSecurityConfigurerAdapter的实现来解决这个问题。这删除了我对AuthenticationManagerBuilder和HttpSecurity的配置。
对于身份验证管理器,我理解我需要创建一个AuthenticationProvider来处理用户身份验证,因此我使用DaoAuthenticationProvider和InMemoryUserDetailsManager来创建一个简单的提供程序。这是以前使用AuthenticationManagerBuilder #inMemoryAuthentication。
完成的我不确定如何设置HttpSecurity的实例,以便我可以使用表单登录,我可以使用WebSecurityConfigurerAdapter。
答案 0 :(得分:0)
您必须将@Autowired
注释放在使用methodSecurityInterceptor
的方法的每个之上。
例如:
@Bean
@Autowired
public Advisor postAuthorizeAdvisor(MethodSecurityInterceptor methodSecurityInterceptor) {
AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
pointcut.setExpression("execution(@(@org.springframework.security.access.prepost.PostAuthorize *) * *.* (..))");
return new DefaultPointcutAdvisor(pointcut, methodSecurityInterceptor);
}
答案 1 :(得分:0)
我已经找到了如何纠正这一点,并不完全确定为什么会这样,但确实如此。我删除了AopSecurityConfiguration类并将methodSecurityInterceptor方法移动到我的WebSecurityConfigurerAdapter实现并删除了authenticationManager参数。在方法中,我现在调用WebSecurityConfigurerAdapter的authenticationManager方法。
这样做可以让我使用RulesRelation注释创建自定义的Pre / PostAuthorize注释注释和/或注释。
不确定原始博客上描述的切入点顾问是什么需要,也许这是以前版本所需要的但不长的是什么?