在学习spring security时,启动服务器报错

问题描述

错误如下:严重: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以后再说了。。。

时间: 2024-09-20 07:57:44

在学习spring security时,启动服务器报错的相关文章

ssh整合-spring整合hibernate做测试时没有报错,当三个整合时启动就报错啦,求大神指点!!

问题描述 spring整合hibernate做测试时没有报错,当三个整合时启动就报错啦,求大神指点!! 报的错误org.springframework.scheduling.quartz.JobMethodInvocationFailedException: Invocation of method 'ecsUnsigned' on target class [class $Proxy17] failed; nested exception is org.springframework.tran

spring定时任务-Spring+quartz本地启动不报错,发布到应用服务器一段时间后报错

问题描述 Spring+quartz本地启动不报错,发布到应用服务器一段时间后报错

task-使用spring自带的Task,放在服务器上启动时会报错

问题描述 使用spring自带的Task,放在服务器上启动时会报错 spring-mvc org.springframework.beans.factory.BeanDefinitionStoreException: Failed to read candidate component class Caused by: java.lang.TypeNotPresentException: Type java.lang.annotation.Repeatable not present at co

javaweb项目,tomcat启动异常报错,spring文件初始化问题,大神help

问题描述 javaweb项目,tomcat启动异常报错,spring文件初始化问题,大神help 本地启动没问题,把编译好的项目放到别的机器就不行,tomcat版本是一样的,都是8.0.12,jdk也是一样的,本地的系统是windows,另外一台是linux ERROR 2015-08-13 14:50:01,873 ContextLoader:initWebApplicationContext - Context initialization failed java.lang.NullPoin

spring 写junit测试时使用aop报错

问题描述 spring 写junit测试时使用aop报错 当我在我的配置文件中加上如下配置会包下面的错误,我不加时就不会出错这是为什么 <aop:config> <aop:pointcut id="interceptorPointCuts" expression="execution(* cn.qtt.service.*.*(..)) " /> <aop:pointcut id="interceptorPointCutsNew

eclipse服务器启动一直报错

问题描述 eclipse服务器启动一直报错 这是哪里出了问题? 解决方案 jsp未能编译成.class文件 解决方案二: windows启动eclipse报错eclipse 中导入 maven项目 启动报错启动Tomcat服务器报错: 解决方案三: 你的jsp页面出问题了,没有编译成class文件 解决方案四: http://zhidao.baidu.com/question/276657176.html 解决方案五: JSP页面的问题,字节码文件没有被编译! 解决方案六: 看看依赖包是不是全的

logback access模块和jetty集成,启动jetty报错!!!

问题描述 logback access模块和jetty集成,启动jetty报错!!! 官方资料:http://logback.qos.ch/access.html 配置文件: class=""org.eclipse.jetty.server.handler.ContextHandlerCollection""/> class=""org.eclipse.jetty.server.handler.DefaultHandler"&qu

tomcat启动报错-SSH + Oracle 整合开发 启动服务报错,运行查询操作,没影响

问题描述 SSH + Oracle 整合开发 启动服务报错,运行查询操作,没影响 信息: Initializing Spring root WebApplicationContextlog4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).log4j:WARN Please initialize the log4j system properly.2013-1

WAS项目启动问题报错求指导

问题描述 WAS项目启动问题报错求指导 [14-6-11 11:17:53:894 CST] 00000000 WindowsServic 1 In executeCommand args are: D:Program FilesIBMWebSphereAppServerbinWASService.exe -status 2YE3JI6FPVNYTGENode01 [14-6-11 11:17:53:957 CST] 00000000 WindowsServic 1 Runtime.exec()