<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:security="http://www.springframework.org/schema/security"
  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.5.xsd
      http://www.springframework.org/schema/security
      http://www.springframework.org/schema/security/spring-security-2.0.xsd">

  <!-- Filter Chain Proxy -->
  <bean id="filterChainProxy" class="org.springframework.security.util.FilterChainProxy">
    <security:filter-chain-map path-type="ant">
      <security:filter-chain pattern="/**" filters="httpSessionContextIntegrationFilter,logoutFilter,authenticationProcessingFilter,rememberMeProcessingFilter,anonymousProcessingFilter,exceptionTranslationFilter"/>
    </security:filter-chain-map>
  </bean>

  <!-- HTTP Session Context Integration Filter -->
  <bean id="httpSessionContextIntegrationFilter" class="org.springframework.security.context.HttpSessionContextIntegrationFilter"/>

  <!-- Logout Filter -->
  <bean id="logoutFilter" class="org.springframework.security.ui.logout.LogoutFilter">
    <constructor-arg value="/index.html"/>
    <constructor-arg>
      <list>
        <ref bean="rememberMeServices"/>
        <ref bean="securityContextLogoutHandler"/>
      </list>
    </constructor-arg>
    <property name="filterProcessesUrl" value="/account/logout.html"/>
  </bean>

  <bean id="securityContextLogoutHandler" class="org.springframework.security.ui.logout.SecurityContextLogoutHandler"/>

  <!-- Authentication Processing Filter -->
  <bean id="authenticationProcessingFilter" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilter">
    <property name="authenticationManager" ref="authenticationManager"/>
    <property name="authenticationFailureUrl" value="/index.html"/>
    <property name="defaultTargetUrl" value="/index.html"/>
    <property name="filterProcessesUrl" value="/account/login.html"/>
    <property name="rememberMeServices" ref="rememberMeServices"/>
  </bean>

  <bean id="authenticationManager" class="org.springframework.security.providers.ProviderManager">
    <property name="providers">
      <list>
        <ref local="databaseAuthenticationProvider"/>
        <ref local="anonymousAuthenticationProvider"/>
        <ref local="rememberMeAuthenticationProvider"/>
      </list>
    </property>
  </bean>

  <!-- Database Authentication Provider -->
  <bean id="databaseAuthenticationProvider" class="org.springframework.security.providers.dao.DaoAuthenticationProvider">
    <property name="userDetailsService" ref="userDetailsService"/>
    <property name="passwordEncoder" ref="passwordEncoder"/>
  </bean>

  <bean id="userDetailsService" class="org.unitedfront2.web.security.UserDetailsServiceImpl"/>

  <bean id="passwordEncoder" class="org.springframework.security.providers.encoding.Md5PasswordEncoder"/>

  <!-- Anonymous Authentication Provider -->
  <bean id="anonymousAuthenticationProvider" class="org.springframework.security.providers.anonymous.AnonymousAuthenticationProvider">
    <property name="key" value="${spring.security.salt}"/>
  </bean>

  <!-- Remember Me Authentication Provider -->
  <bean id="rememberMeAuthenticationProvider" class="org.springframework.security.providers.rememberme.RememberMeAuthenticationProvider">
    <property name="key" value="${spring.security.salt}"/>
  </bean>

  <bean id="rememberMeServices" class="org.springframework.security.ui.rememberme.TokenBasedRememberMeServices">
    <property name="userDetailsService" ref="userDetailsService"/>
    <property name="key" value="${spring.security.salt}"/>
    <property name="parameter" value="remember"/>
    <property name="tokenValiditySeconds" value="31536000"/><!-- a year -->
  </bean>

  <!-- Remember Me Processing Filter -->
  <bean id="rememberMeProcessingFilter" class="org.unitedfront2.web.security.RememberMeProcessingRedirectFilter">
    <property name="rememberMeServices" ref="rememberMeServices"/>
    <property name="authenticationManager" ref="authenticationManager"/>
  </bean>

  <!-- Anonymous Processing Filter -->
  <bean id="anonymousProcessingFilter" class="org.springframework.security.providers.anonymous.AnonymousProcessingFilter">
    <property name="key" value="${spring.security.salt}"/>
    <property name="userAttribute" value="anonymousUser,ROLE_ANONYMOUS"/>
  </bean>

  <!-- exceptionTranslationFilter -->
  <bean id="exceptionTranslationFilter" class="org.springframework.security.ui.ExceptionTranslationFilter">
    <property name="authenticationEntryPoint" ref="authenticationProcessingFilterEntryPoint"/>
    <property name="accessDeniedHandler">
      <bean class="org.springframework.security.ui.AccessDeniedHandlerImpl">
        <property name="errorPage" value="/error/accessDenied.html"/>
      </bean>
    </property>
  </bean>

  <bean id="authenticationProcessingFilterEntryPoint" class="org.springframework.security.ui.webapp.AuthenticationProcessingFilterEntryPoint">
    <property name="loginFormUrl" value="/"/>
  </bean>

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

</beans>
