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

    <http pattern="/" security='none' />
    <http auto-config="true" use-expressions="true">
        
        <intercept-url pattern="/css/**" access="permitAll" />
        <intercept-url pattern="/design/**" access="permitAll" />
        <intercept-url pattern="/images/**" access="permitAll" />
        <intercept-url pattern="/js/**" access="permitAll" />        
        <intercept-url pattern="/index.jsp**" access="permitAll" />
        <intercept-url pattern="/jsp/**" access="isAuthenticated()" />
        <intercept-url pattern="/**" access="isAuthenticated()" />

        <form-login login-page="/index.jsp"
                    default-target-url="/inicio.action"
                    always-use-default-target="true"
                    authentication-failure-url="/index.jsp?login_error=1"
                    authentication-success-handler-ref="customAuthenticationSuccessHandler"
                    authentication-failure-handler-ref="customAuthenticationFailureHandler" />
		<logout logout-url="/j_spring_security_logout" 
                success-handler-ref="customLogoutSuccessHandler"/>
    </http>

    <authentication-manager alias="authenticationManager">
        <authentication-provider>
            <jdbc-user-service data-source-ref="dsDLA"
                users-by-username-query="
                    select 
                        EXPEDIENTE as username, 
                        PASSWORD as password, 
                        B_USUARIO_LOGUEADO as enabled  
                    from 
                        DLA_C_USUARIO
                    where
                        EXPEDIENTE = ?"
                authorities-by-username-query="
                    select 
                        usr.EXPEDIENTE as expediente,     
                        per.DESC_PERFIL as perfil
                    from 
                        DLA_C_USUARIO usr,
                        DLA_C_USUARIO_PERFIL usrper,
                        DLA_C_PERFIL per
                    where
                        usr.EXPEDIENTE = usrper.EXPEDIENTE
                    AND usrper.ID_PERFIL = per.ID_PERFIL
                    AND usr.EXPEDIENTE = ?" />
            <password-encoder hash="md5" />
        </authentication-provider>
    </authentication-manager>

    <!-- Beans de los manejadores personalizados -->
    <beans:bean id="customAuthenticationSuccessHandler" class="com.mx.dla.configuration.CustomAuthenticationSuccessHandler" />
    <beans:bean id="customAuthenticationFailureHandler" class="com.mx.dla.configuration.CustomAuthenticationFailureHandler" />
    <beans:bean id="customLogoutSuccessHandler" class="com.mx.dla.configuration.CustomLogoutSuccessHandler" />
</beans:beans>
