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

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:sec="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.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-2.0.xsd">
    
    <bean id="simplePortletHandlerAdapter" class="org.springframework.web.portlet.handler.SimplePortletHandlerAdapter"/>
    <bean id="simplePortletPostProcessor" class="org.springframework.web.portlet.handler.SimplePortletPostProcessor"/>    
    <bean id="springSecurityPortlet" class="org.springframework.web.portlet.sample.SecurityContextPortlet"/>    

    <!-- Handler Mapping -->
    
    <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="portletContextIntegrationInterceptor"/>
                <ref bean="portletAuthenticationInterceptor"/>
            </list>            
        </property>
        <property name="portletModeMap">
            <map>
                <entry key="view" value-ref="springSecurityPortlet"/>
            </map>
        </property>
    </bean>
    
    <!-- Exceptions Handler -->
    
    <bean id="defaultExceptionHandler" parent="defaultExceptionHandlerTemplate"/>

    <bean id="portletContextIntegrationInterceptor" class="org.springframework.security.portlet.PortletSessionContextIntegrationInterceptor"/>
    
    <bean id="portletAuthenticationInterceptor" class="org.springframework.security.portlet.PortletProcessingInterceptor">
        <property name="authenticationDetailsSource">
            <bean class="org.springframework.security.portlet.PortletPreAuthenticatedAuthenticationDetailsSource">
                <property name="mappableRolesRetriever">
                    <bean class="org.springframework.security.core.authority.mapping.SimpleMappableAttributesRetriever">
                        <property name="mappableAttributes">
                            <set>
                                <value>tomcat</value>
                                <value>admin</value>
                                <value>manager</value>
                                <!-- Some standard liferay roles -->
                                <value>Administrator</value>
                                <value>Guest</value>
                                <value>User</value>
                                <value>Power User</value>
                            </set>
                        </property>                        
                    </bean>
                </property>
            </bean>
        </property>            
        <property name="authenticationManager" ref="authenticationManager"/>
        <!-- Liferay doesn't seem to set the authType -->
        <property name="useAuthTypeAsCredentials" value="false"/>
    </bean>
    
    <sec:authentication-manager alias="authenticationManager"/>
   
    <bean id="portletAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
        <sec:custom-authentication-provider/>
        <property name="preAuthenticatedUserDetailsService">
            <bean class="org.springframework.security.web.authentication.preauth.PreAuthenticatedGrantedAuthoritiesUserDetailsService"/>
        </property>
        <property name="throwExceptionWhenTokenRejected" value="true"/>
    </bean>

<!--    
    <bean id="portletAuthProvider" class="org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationProvider">
        <sec:custom-authentication-provider/>
        <property name="preAuthenticatedUserDetailsService">
            <bean class="org.springframework.security.core.userdetails.UserDetailsByNameServiceWrapper">
                <property name="userDetailsService" ref="userDetailsService"/>
            </bean>
        </property>
    </bean>
   
    <sec:user-service id="userDetailsService">
        <sec:user name="pluto" password="notused" authorities="ROLE_USER,ROLE_A,ROLE_B"/>
        <sec:user name="tomcat" password="notused" authorities="ROLE_USER,ROLE_A,ROLE_B"/>
    </sec:user-service>    
-->    
</beans>