<?xml version='1.0' encoding='UTF-8' ?>
<!-- was: <?xml version="1.0" encoding="UTF-8"?> -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:p="http://www.springframework.org/schema/p"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:sec="http://www.springframework.org/schema/security"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <!-- Creating Beans with declared with spring annotations -->
    <context:annotation-config  />
    <context:component-scan base-package="org.primefaces.demo.service" />
    <context:component-scan base-package="org.primefaces.layout.converters" />
    <context:component-scan base-package="org.primefaces.demo.bean" />
    <context:component-scan base-package="org.primefaces.layout.util" />
    <context:component-scan base-package="com.triz.trizservice.security" />
    <context:component-scan base-package="com.triz.trizservice.security.impl" />
    <context:property-placeholder location="classpath:database.properties"/>

        
    <!-- Bean to encode passwords -->
    <bean id="pwdEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder">
        <constructor-arg name="strength" value="11" />
    </bean>
    
    <!-- Redirecting user when he have no right to be there -->
    <bean id="accessDenied" class="org.springframework.security.web.access.AccessDeniedHandlerImpl">
        <property name="errorPage" value="/auth/accesRefuse.xhtml" />
    </bean>
        
    <!-- Authentication in database -->
    <!-- Authentication in database -->
    <sec:authentication-manager id="dataSourceAuthenticationManager">
        <sec:authentication-provider>
            <sec:jdbc-user-service data-source-ref="dataSource"
                                   users-by-username-query=
    		    "SELECT userID, userPW FROM service_user WHERE userID =?"
                                   authorities-by-username-query=
		    "SELECT u.userID, m.fkGroupe FROM service_user u, service_member m WHERE u.userID = m.fkUser u AND u.userID =?" />
            <sec:password-encoder ref="pwdEncoder" />
        </sec:authentication-provider>
    </sec:authentication-manager>
    
    
    <sec:authentication-manager alias="authenticationManager">
        <sec:authentication-provider ref="loginService" >
        </sec:authentication-provider>
    </sec:authentication-manager>
    
    <!-- doing thing when authentication is successful -->
    <bean id="successHandler" class="com.triz.trizservice.security.impl.CostumSuccesHandler"  ></bean>
    
    <!-- Access and security configuration -->
    <sec:http auto-config="true" use-expressions="true" >
        <sec:form-login  login-page="/auth/login.xhtml" authentication-failure-url="/auth/login.xhtml" authentication-success-handler-ref="successHandler" />
        <sec:intercept-url pattern="/auth/login.xhtml" access="permitAll()"/>
        <sec:intercept-url pattern="/resources/resources/**" access="permitAll()"/>
        <sec:intercept-url pattern="/api/**" access="permitAll()"/>
        <sec:intercept-url pattern="/resources/**" access="isAuthenticated()" />
        <sec:intercept-url pattern="/views/**" access="isAuthenticated()" />
        <sec:intercept-url pattern="/favorites/**" access="isAuthenticated()" />
        <!--<sec:intercept-url pattern="/faces/view/admin/**" access="hasAnyRole('ROLE_ADMIN')" />-->
        <sec:logout logout-success-url="/" logout-url="/j_spring_security_logout" />
        <sec:access-denied-handler ref="accessDenied"/>
        <sec:session-management invalid-session-url="/auth/login.xhtml" />
    </sec:http>
    
    <tx:annotation-driven />
    <context:annotation-config />
    
    <!-- adding support for view scope-->
    <bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="org.primefaces.demo.jsf.ViewScope"/>
                </entry>
            </map>
        </property>
    </bean>

</beans>
