<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans" 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.xsd">

	<!-- ======================== FILTER CHAIN ======================= -->
	<bean id="filterChainProxy"
		class="org.acegisecurity.util.FilterChainProxy">
		<property name="filterInvocationDefinitionSource">
			<value>
				CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
				PATTERN_TYPE_APACHE_ANT
				/secure/**=httpSessionContextIntegrationFilter,contextHolderAwareRequestFilter,rememberMeProcessingFilter,securityEnforcementFilter
				/ws/**=httpSessionContextIntegrationFilter,basicProcessingFilter,contextHolderAwareRequestFilter,basicSecurityEnforcementFilter
				/j_acegi_security_check*=httpSessionContextIntegrationFilter,authenticationProcessingFilter
				/welcome.action=httpSessionContextIntegrationFilter,contextHolderAwareRequestFilter,rememberMeProcessingFilter
			</value>
		</property>
	</bean>

	<!-- ===================== httpSessionContextIntegrationFilter ==================== -->
	<bean id="httpSessionContextIntegrationFilter"
		class="org.acegisecurity.context.HttpSessionContextIntegrationFilter">
		<property name="context">
			<value>
				org.acegisecurity.context.SecurityContextImpl
			</value>
		</property>
	</bean>
	
	<!-- ===================== authenticationProcessingFilter ==================== -->
	<bean id="authenticationProcessingFilter"
		class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilter">
		<property name="authenticationManager">
			<ref bean="authenticationManager" />
		</property>
		<property name="rememberMeServices">
			<ref bean="rememberMeServices" />
		</property>
		<property name="authenticationFailureUrl">
			<value>/welcome.action?login_error=true</value>
		</property>
		<property name="defaultTargetUrl">
			<value>/secure/showTodos.action</value>
		</property>
		<property name="filterProcessesUrl">
			<value>/j_acegi_security_check</value>
		</property>
	</bean>
	
	<bean id="authenticationManager"
		class="org.acegisecurity.providers.ProviderManager">
		<property name="providers">
			<list>
				<ref local="daoAuthenticationProvider" />
				<ref local="rememberMeAuthenticationProvider" />
			</list>
		</property>
	</bean>

	<!-- Automatically receives AuthenticationEvent messages from DaoAuthenticationProvider -->
	<bean id="loggerListener"
		class="org.acegisecurity.event.authentication.LoggerListener" />

	<bean id="daoAuthenticationProvider"
		class="org.acegisecurity.providers.dao.DaoAuthenticationProvider">
		<property name="userDetailsService">
			<ref local="userDetailsService" />
		</property>
		<property name="userCache">
			<ref local="userCache" />
		</property>
	</bean>

	<bean id="userDetailsService"
		class="tudu.security.UserDetailsServiceImpl">
		<property name="userManager">
			<ref bean="userManager" />
		</property>
	</bean>

	<bean id="userCache"
		class="org.acegisecurity.providers.dao.cache.EhCacheBasedUserCache">
		<property name="cache">
			<bean
				class="org.springframework.cache.ehcache.EhCacheFactoryBean">
				<property name="cacheManager">
					<bean
						class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean" />
				</property>
				<property name="cacheName">
					<value>AcegiUserCache</value>
				</property>
			</bean>
		</property>
	</bean>
	
	<bean id="rememberMeAuthenticationProvider"
		class="org.acegisecurity.providers.rememberme.RememberMeAuthenticationProvider">
		<property name="key">
			<value>tuduRocks</value>
		</property>
	</bean>

	<!-- ===================== contextHolderAwareRequestFilter ==================== -->
	<bean id="contextHolderAwareRequestFilter"
		class="org.acegisecurity.wrapper.SecurityContextHolderAwareRequestFilter" />
		
	<!-- ===================== rememberMeProcessingFilter ==================== -->
	<bean id="rememberMeProcessingFilter"
		class="org.acegisecurity.ui.rememberme.RememberMeProcessingFilter">
		<property name="rememberMeServices">
			<ref local="rememberMeServices" />
		</property>
	</bean>
	
	<bean id="rememberMeServices"
		class="org.acegisecurity.ui.rememberme.TokenBasedRememberMeServices">
		<property name="userDetailsService">
			<ref local="userDetailsService" />
		</property>
		<property name="tokenValiditySeconds">
			<value>2592000</value>
		</property>
		<property name="key">
			<value>tuduRocks</value>
		</property>
	</bean>

	<!-- ===================== securityEnforcementFilter ==================== -->
	<bean id="securityEnforcementFilter"
		class="org.acegisecurity.intercept.web.SecurityEnforcementFilter">
		<property name="filterSecurityInterceptor">
			<ref local="filterInvocationInterceptor" />
		</property>
		<property name="authenticationEntryPoint">
			<ref local="authenticationProcessingFilterEntryPoint" />
		</property>
	</bean>

	<!-- Note the order that entries are placed against the objectDefinitionSource is critical.
		The FilterSecurityInterceptor will work from the top of the list down to the FIRST pattern that matches the request URL.
		Accordingly, you should place MOST SPECIFIC (ie a/b/c/d.*) expressions first, with LEAST SPECIFIC (ie a/.*) expressions last -->
	<bean id="filterInvocationInterceptor"
		class="org.acegisecurity.intercept.web.FilterSecurityInterceptor">
		<property name="authenticationManager">
			<ref local="authenticationManager" />
		</property>
		<property name="accessDecisionManager">
			<ref local="accessDecisionManager" />
		</property>
		<property name="objectDefinitionSource">
			<value>
				CONVERT_URL_TO_LOWERCASE_BEFORE_COMPARISON
				PATTERN_TYPE_APACHE_ANT
				/secure/admin/**=ROLE_ADMIN
				/secure/**=ROLE_USER
				/ws/**=ROLE_USER
			</value>
		</property>
	</bean>

	<bean id="accessDecisionManager"
		class="org.acegisecurity.vote.AffirmativeBased">
		<property name="allowIfAllAbstainDecisions">
			<value>false</value>
		</property>
		<property name="decisionVoters">
			<list>
				<ref local="roleVoter" />
			</list>
		</property>
	</bean>

	<bean id="roleVoter" class="org.acegisecurity.vote.RoleVoter">
		<property name="rolePrefix">
			<value />
		</property>
	</bean>

	<bean id="authenticationProcessingFilterEntryPoint"
		class="org.acegisecurity.ui.webapp.AuthenticationProcessingFilterEntryPoint">
		<property name="loginFormUrl">
			<value>/welcome.action</value>
		</property>
		<property name="forceHttps">
			<value>false</value>
		</property>
	</bean>
	
	<!-- ===================== basicProcessingFilter ==================== -->
	<bean id="basicProcessingFilter" class="org.acegisecurity.ui.basicauth.BasicProcessingFilter">
  		<property name="authenticationManager"><ref bean="authenticationManager"/></property>
  		<property name="authenticationEntryPoint"><ref bean="basicAuthenticationEntryPoint"/></property>
	</bean>

	<bean id="basicAuthenticationEntryPoint" class="org.acegisecurity.ui.basicauth.BasicProcessingFilterEntryPoint">
  		<property name="realmName"><value>Tudu Lists Realm</value></property>
	</bean>
	
	<bean id="basicSecurityEnforcementFilter"
		class="org.acegisecurity.intercept.web.SecurityEnforcementFilter">
		<property name="filterSecurityInterceptor">
			<ref local="filterInvocationInterceptor" />
		</property>
		<property name="authenticationEntryPoint">
			<ref local="basicAuthenticationEntryPoint" />
		</property>
	</bean>
</beans>
