Spring security2 保险配置

Spring security2 安全配置
在Spring配置过程中,我遇到了创建Bean出错的问题,错误如下:
引用

2012-03-09 08:58:31,330 ERROR (org.springframework.web.context.ContextLoader:215) - Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainProxy': Initialization of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainList': Cannot resolve reference to bean '_rememberMeFilter' while setting bean property 'filters' with key [6]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeFilter': Cannot resolve reference to bean '_rememberMeServices' while setting bean property 'rememberMeServices'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeServices': Initialization of bean failed; nested exception is org.springframework.security.config.SecurityConfigurationException: More than one UserDetailsService registered. Please use a specific Id in your configuration
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3727)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4162)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:718)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1012)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:442)
at org.apache.catalina.core.StandardService.start(StandardService.java:450)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:700)
at org.apache.catalina.startup.Catalina.start(Catalina.java:551)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:275)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_filterChainList': Cannot resolve reference to bean '_rememberMeFilter' while setting bean property 'filters' with key [6]; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeFilter': Cannot resolve reference to bean '_rememberMeServices' while setting bean property 'rememberMeServices'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeServices': Initialization of bean failed; nested exception is org.springframework.security.config.SecurityConfigurationException: More than one UserDetailsService registered. Please use a specific Id in your configuration
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveManagedList(BeanDefinitionValueResolver.java:287)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:126)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.security.config.FilterChainProxyPostProcessor.postProcessBeforeInitialization(FilterChainProxyPostProcessor.java:52)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:350)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1331)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
... 29 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeFilter': Cannot resolve reference to bean '_rememberMeServices' while setting bean property 'rememberMeServices'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name '_rememberMeServices': Initialization of bean failed; nested exception is org.springframework.security.config.SecurityConfigurationException: More than one UserDetailsService registered. Please use a specific Id in your configuration
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:275)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:104)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1245)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:269)
... 47 more

我的DBSource是自己定义的,不是用的Spring的JDBC用的是IBatic的数据源,这个问题导致我工作2、3天没有进展,试过各种方法,最后发现是配置文件的问题。具体原因是配置文件中多配置了:
   <b:authentication-provider>
       <b:password-encoder hash="md5"/>
       <b:jdbc-user-service data-source-ref="dataSource"/>
   </b:authentication-provider>

去掉后,一切正常。现将正确的配置贴出来,以备后用。
<?xml version="1.0" encoding="UTF-8"?>
<!--
  - Application context containing authentication, channel
  - security and web URI beans.
  -
  - Only used by "filter" artifact.
  -
  - $Id: applicationContext-acegi-security.xml 1425 2006-04-28 06:43:50Z benalex $
  -->

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:b="http://www.springframework.org/schema/security"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                        http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.1.xsd
                        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd" default-autowire="byName">
    <b:http auto-config="true" realm="Contacts Realm" access-denied-page="/accessDenied.jsp">
        <b:intercept-url pattern="/" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <b:intercept-url pattern="/index.html" access="IS_AUTHENTICATED_ANONYMOUSLY"/>
        <b:intercept-url pattern="/Login.jsp" access="IS_AUTHENTICATED_ANONYMOUSLY"/> 
        <b:intercept-url pattern="/image/**" filters="none"/>
        <b:intercept-url pattern="/common/**" filters="none"/>
        <b:intercept-url pattern="/scripts/**" filters="none"/>
        <b:intercept-url pattern="/welcome.jsp" filters="none"/>   
        <b:intercept-url pattern="/js/**" filters="none"/>
        <b:intercept-url pattern="/j_spring_security_switch_user" access="ROLE_SUPERVISOR"/>
        <b:intercept-url pattern="/**" access="ROLE_USER"/>

        <b:form-login login-page="/Login.jsp"/>
        <b:logout logout-success-url="/Login.jsp"/>
    </b:http>
	
   <!-- b:authentication-provider>
       <b:password-encoder hash="md5"/>
       <b:jdbc-user-service data-source-ref="dataSource"/>
   </b:authentication-provider  -->
   
   <bean id="authenticationProcessingFilter" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
   		<b:custom-filter before="AUTHENTICATION_PROCESSING_FILTER"/>
   		<property name="authenticationManager" ref="authenticationManager"></property>
   		<property name="authenticationFailureUrl" value="/spring_security_login"/>
   		<property name="defaultTargetUrl" value="/main.action" />
   </bean>
   <bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager">
	   	<property name="providers">
	   		<list>
	   			<ref local="daoAuthenticationProvider"/>
	   		</list>
	   	</property>
   </bean>
   <bean id="daoAuthenticationProvider" class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
   		<property name="userDetailsService" ref="userService"/>
   		<property name="userCache" ref="userCache"/>
   </bean>
   <bean id="userCache" class="org.springframework.security.providers.dao.cache.EhCacheBasedUserCache">
   		<property name="cache" ref="userCacheBacked"/>
   </bean>
   <bean id="userCacheBacked" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
   		<property name="cacheManager" ref="cacheManager"/>
   		<property name="cacheName" value="userCache"/>
   </bean>
   <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
   		<property name="configLocation" value="classpath:ehcache-security.xml"/>
   </bean>
   <bean id="filterSecurityInterceptor" class="org.springframework.security.intercept.web.FilterSecurityInterceptor">
   		<b:custom-filter before="FILTER_SECURITY_INTERCEPTOR"/>
   		<property name="authenticationManager" ref="authenticationManager"/>
   		<property name="accessDecisionManager" ref="accessDecisionManager"/>
   		<property name="objectDefinitionSource" ref="databaseFilterInvocationDefinitionSource"/>
   </bean>
   <bean id="accessDecisionManager" class="org.springframework.security.vote.AffirmativeBased">
   		<property name="decisionVoters">
			<list>
				<bean class="org.springframework.security.vote.RoleVoter">
					<property name="rolePrefix" value=""/>
				</bean>
			</list>
   		</property>
   </bean>
   <bean id="databaseFilterInvocationDefinitionSource" class="org.springframework.security.intercept.web.DefaultFilterInvocationDefinitionSource">
   		<constructor-arg type="org.springframework.security.util.UrlMatcher" ref="antUrlPathMatcher"/>
   		<constructor-arg type="java.util.LinkedHashMap" ref="requestMap"/>
   </bean>
   <bean id="antUrlPathMatcher" class="org.springframework.security.util.AntUrlPathMatcher"/>
   <bean id="requestMap" class="cn.com.pzhsteel.service.SpringSecurityConfigServiceImp" init-method="init">
   		<property name="roleDB" ref="roleDAO"/>
   </bean>
   <!-- bean id="methodSeurityInterceptor" class="org.springframework.security.intercept.method.aopalliance.MethodSecurityInterceptor">
   		<property name="authenticationManager" ref="authenticationManager"/>
   		<property name="accessDecisionManager" ref="accessDecisionManager"/>
   		<property name="objectDefinitionSource" ref=""/>
   </bean-->
   
   <!-- Automatically receives AuthenticationEvent messages -->
   <bean id="loggerListener" class="org.springframework.security.event.authentication.LoggerListener"/>

   <!-- Filter used to switch the user context. Note: the switch and exit url must be secured
        based on the role granted the ability to 'switch' to another user -->
   <!-- In this example 'rod' has ROLE_SUPERVISOR that can switch to regular ROLE_USER(s) -->
   <bean id="switchUserProcessingFilter" class="org.springframework.security.ui.switchuser.SwitchUserProcessingFilter" autowire="byType">
       <b:custom-filter position="SWITCH_USER_FILTER"/>       
       <property name="targetUrl" value="/secure/index.htm"/>
   </bean>
</beans>


我的异常网推荐解决方案:org.springframework.beans.factory.BeanCreationException:,http://www..net/j2ee/10759.html