问题描述
错误如下:严重:Exceptionsendingcontextinitializedeventtolistenerinstanceofclassorg.springframework.web.context.ContextLoaderListenerorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'org.springframework.security.filterChains':Cannotresolvereferencetobean'org.springframework.security.web.DefaultSecurityFilterChain#4'whilesettingbeanproperty'sourceList'withkey[4];nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'org.springframework.security.web.DefaultSecurityFilterChain#4':Cannotresolvereferencetobean'mySecurityFilter'whilesettingconstructorargumentwithkey[10];nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'mySecurityFilter'definedinfile[D:wpapache-tomcat-7.0.54webappsSpringSecurity_01WEB-INFclassescnzzkjwpsecurityMySecurityFilter.class]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.IllegalArgumentException:AnAuthenticationManagerisrequiredatorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1393)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1134)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:522)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)atorg.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:608)atorg.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932)atorg.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)atorg.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389)atorg.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294)atorg.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112)atorg.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4973)atorg.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5467)atorg.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)atorg.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559)atorg.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549)atjava.util.concurrent.FutureTask$Sync.innerRun(UnknownSource)atjava.util.concurrent.FutureTask.run(UnknownSource)atjava.util.concurrent.ThreadPoolExecutor$Worker.runTask(UnknownSource)atjava.util.concurrent.ThreadPoolExecutor$Worker.run(UnknownSource)atjava.lang.Thread.run(UnknownSource)Causedby:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'org.springframework.security.web.DefaultSecurityFilterChain#4':Cannotresolvereferencetobean'mySecurityFilter'whilesettingconstructorargumentwithkey[10];nestedexceptionisorg.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'mySecurityFilter'definedinfile[D:wpapache-tomcat-7.0.54webappsSpringSecurity_01WEB-INFclassescnzzkjwpsecurityMySecurityFilter.class]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.IllegalArgumentException:AnAuthenticationManagerisrequiredatorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:329)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:107)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:353)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:154)atorg.springframework.beans.factory.support.ConstructorResolver.resolveConstructorArguments(ConstructorResolver.java:615)atorg.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:148)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1051)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:955)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:490)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)...27moreCausedby:org.springframework.beans.factory.BeanCreationException:Errorcreatingbeanwithname'mySecurityFilter'definedinfile[D:wpapache-tomcat-7.0.54webappsSpringSecurity_01WEB-INFclassescnzzkjwpsecurityMySecurityFilter.class]:Invocationofinitmethodfailed;nestedexceptionisjava.lang.IllegalArgumentException:AnAuthenticationManagerisrequiredatorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1488)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:524)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:461)atorg.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:295)atorg.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)atorg.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:292)atorg.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)atorg.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)...41moreCausedby:java.lang.IllegalArgumentException:AnAuthenticationManagerisrequiredatorg.springframework.util.Assert.notNull(Assert.java:112)atorg.springframework.security.access.intercept.AbstractSecurityInterceptor.afterPropertiesSet(AbstractSecurityInterceptor.java:121)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1547)atorg.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1485)...48more
报的是身份认证管理器没注入,我检查了配置文件,已经配置了,如下:<!--对密码进行MD5编码--><beans:beanid="passwordEncoder"class="org.springframework.security.authentication.encoding.Md5PasswordEncoder"/><!--通过myUserDetailServiceImpl,Spring会控制用户的访问级别.也可以理解成:以后我们和数据库操作就是通过myUserDetailServiceImpl来进行关联.--><beans:beanid="myUserDetailService"class="cn.zzkj.wp.security.MyUserDetailServiceImpl"/><!--指定一个自定义的认证管理器:authentication-manager:myUserDetailServiceImpl--><authentication-manageralias="myAuthenticationManager"><authentication-provideruser-service-ref="myUserDetailService"><!--密码加密处理--><password-encoderref="passwordEncoder"/></authentication-provider></authentication-manager>
求大神看一下,帮忙找一下,到底是哪里出了问题。。。
解决方案
解决方案二:
由于篇幅问题,下面贴出MyUserDetailServiceImpl文件如下:packagecn.zzkj.wp.security;importjava.util.Collection;importjava.util.HashSet;importjava.util.List;importjava.util.Set;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.dao.DataAccessException;importorg.springframework.security.core.GrantedAuthority;importorg.springframework.security.core.authority.GrantedAuthorityImpl;importorg.springframework.security.core.userdetails.User;importorg.springframework.security.core.userdetails.UserDetails;importorg.springframework.security.core.userdetails.UserDetailsService;importorg.springframework.security.core.userdetails.UsernameNotFoundException;importorg.springframework.stereotype.Component;importcn.zzkj.wp.dao.ResourcesDao;importcn.zzkj.wp.dao.UserDao;importcn.zzkj.wp.model.Resources;@ComponentpublicclassMyUserDetailServiceImplimplementsUserDetailsService{@AutowiredprivateUserDaouserDao;@AutowiredprivateResourcesDaoresourcesDao;@OverridepublicUserDetailsloadUserByUsername(Stringusername)throwsUsernameNotFoundException,DataAccessException{System.err.println("-----------MyUserDetailServiceImplloadUserByUsername-----------");//取得用户的权限cn.zzkj.wp.model.Userusers=userDao.querySingleUser(username);if(users==null)thrownewUsernameNotFoundException(username+"notexist!");Collection<GrantedAuthority>grantedAuths=obtionGrantedAuthorities(users);//封装成springsecurity的userUseruserdetail=newUser(users.getUserName(),users.getUserPassword(),true,true,true,true,grantedAuths//用户的权限);returnuserdetail;}//或得用户权限@SuppressWarnings("deprecation")privateCollection<GrantedAuthority>obtionGrantedAuthorities(cn.zzkj.wp.model.Useruser){//根据用户id,加载该用户拥有的所有资源List<Resources>resources=resourcesDao.getUserResources(String.valueOf(user.getUserId()));Set<GrantedAuthority>authSet=newHashSet<GrantedAuthority>();for(Resourcesres:resources){//ZZQ用户可以访问的资源名称(或者说用户所拥有的权限)注意:必须"ROLE_"开头//关联代码:applicationContext-security.xml//关联代码:com.huaxin.security.MySecurityMetadataSource#loadResourceDefineauthSet.add(newGrantedAuthorityImpl("ROLE_"+res.getResKey()));}returnauthSet;}}
springsecurity版本我用的3.1
解决方案三:
springsecurity太复杂了,apacheshiro不错
解决方案四:
有没有人知道啊?这个是什么问题啊?
解决方案五:
引用2楼javaee_ssh的回复:
springsecurity太复杂了,apacheshiro不错
但现在自学的就是springsecurity的嘛,shiro以后再说了。。。