<?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:util="http://www.springframework.org/schema/util"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- 缓存管理器 使用Ehcache实现 -->
    <bean id="cacheManager" class="org.apache.shiro.cache.ehcache.EhCacheManager">
        <property name="cacheManagerConfigFile" value="classpath:ehcache/ehcache.xml"/>
    </bean>

    <!-- 凭证匹配器 -->
    <bean id="credentialsMatcher" class="com.xmomen.module.account.credentials.RetryLimitHashedCredentialsMatcher">
        <constructor-arg ref="cacheManager"/>
        <property name="hashAlgorithmName" value="md5"/>
        <property name="hashIterations" value="2"/>
        <property name="storedCredentialsHexEncoded" value="true"/>
    </bean>

    <!-- 会话ID生成器 -->
    <bean id="sessionIdGenerator" class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator"/>

    <!-- 会话Cookie模板 -->
    <bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg value="sid"/>
        <property name="httpOnly" value="true"/>
        <property name="maxAge" value="180000"/>
    </bean>

    <!-- 记住我    -->
    <bean id="rememberMeCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
        <constructor-arg value="rememberMe"/>
        <property name="httpOnly" value="true"/>
        <property name="maxAge" value="2592000"/><!-- 30天 -->
    </bean>

    <!-- 会话DAO -->
    <bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
        <property name="activeSessionsCacheName" value="shiro-activeSessionCache"/>
        <property name="sessionIdGenerator" ref="sessionIdGenerator"/>
    </bean>

    <!-- 会话验证调度器 -->
    <bean id="sessionValidationScheduler" class="org.apache.shiro.session.mgt.quartz.QuartzSessionValidationScheduler">
        <property name="sessionValidationInterval" value="1800000"/>
        <property name="sessionManager" ref="sessionManager"/>
    </bean>

    <!-- 会话管理器 -->
    <bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
        <property name="globalSessionTimeout" value="1800000"/>
        <property name="deleteInvalidSessions" value="true"/>
        <property name="sessionValidationSchedulerEnabled" value="true"/>
        <property name="sessionValidationScheduler" ref="sessionValidationScheduler"/>
        <property name="sessionDAO" ref="sessionDAO"/>
        <property name="sessionIdCookieEnabled" value="true"/>
        <property name="sessionIdCookie" ref="sessionIdCookie"/>
    </bean>

    <!-- rememberMe管理器 -->
    <bean id="rememberMeManager"
          class="org.apache.shiro.web.mgt.CookieRememberMeManager">
        <property name="cookie" ref="rememberMeCookie"/>
    </bean>

    <!-- 安全管理器 -->
    <bean id="securityManager" class="com.xmomen.module.core.web.mgt.WebSecurityManagerExt">
        <!--<property name="realm"  ref="userRealm"/>-->
        <property name="realms">
            <util:list>
                <!-- Realm实现 -->
                <bean id="userRealm" class="com.xmomen.module.account.realm.UserRealm">
                    <property name="userService" ref="userService"/>
                    <property name="credentialsMatcher" ref="credentialsMatcher"/>
                    <property name="cachingEnabled" value="true"/>
                    <property name="authenticationCachingEnabled" value="true"/>
                    <property name="authenticationCacheName" value="authenticationCache"/>
                    <property name="authorizationCachingEnabled" value="true"/>
                    <property name="authorizationCacheName" value="authorizationCache"/>
                </bean>
                <bean id="memberRealm" class="com.xmomen.module.account.realm.MemberRealm">
                    <property name="memberService" ref="memberService"/>
                    <property name="credentialsMatcher" ref="credentialsMatcher"/>
                    <property name="cachingEnabled" value="true"/>
                    <property name="authenticationCachingEnabled" value="true"/>
                    <property name="authenticationCacheName" value="pcAuthenticationCache"/>
                    <property name="authorizationCachingEnabled" value="true"/>
                    <property name="authorizationCacheName" value="pcAuthorizationCache"/>
                </bean>
            </util:list>
        </property>
        <property name="sessionManager" ref="sessionManager"/>
        <property name="cacheManager" ref="cacheManager"/>
        <property name="rememberMeManager" ref="rememberMeManager"/>
        
        <property name="authenticator">
            <bean class="com.xmomen.module.core.web.authenticator.MultiLoginAuthenticator"></bean>
        </property>
    </bean>

    <!-- 相当于调用SecurityUtils.setSecurityManager(securityManager) -->
    <bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="staticMethod" value="org.apache.shiro.SecurityUtils.setSecurityManager"/>
        <property name="arguments" ref="securityManager"/>
    </bean>

    <!-- 基于Form表单的身份验证过滤器 -->
    <bean id="formAuthenticationFilter" class="com.xmomen.module.core.web.filter.FormAuthenticationFilterExt">
        <property name="usernameParam" value="username"/>
        <property name="passwordParam" value="password"/>
        <property name="rememberMeParam" value="rememberMe"/>
        <property name="loginUrl" value="/login"/>
    </bean>
    
    <!-- PC端用户Form表单的身份验证过滤器 -->
    <bean id="pcFormAuthenticationFilter" class="com.xmomen.module.core.web.filter.PcFormAuthenticationFilter">
        <property name="usernameParam" value="username"/>
        <property name="passwordParam" value="password"/>
        <property name="rememberMeParam" value="rememberMe"/>
        <property name="loginUrl" value="/member/login"/>
    </bean>

    <!-- Shiro的Web过滤器 -->
    <bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
        <property name="securityManager" ref="securityManager"/>
        <property name="loginUrl" value="/login"/>
        <property name="unauthorizedUrl" value="/unauthorized"/>
        <property name="filters">
            <util:map>
                <entry key="role">
                    <!--    用户角色过滤器    -->
                    <bean class="org.apache.shiro.web.filter.authz.RolesAuthorizationFilter">
                        <property name="unauthorizedUrl" value="/unauthorized"/>
                        <property name="loginUrl" value="/login"/>
                    </bean>
                </entry>
                <entry key="authc" value-ref="formAuthenticationFilter"/>
                <entry key="memberAuthc" value-ref="pcFormAuthenticationFilter"/>
            </util:map>
        </property>
        <property name="filterChainDefinitions">
            <value>
                /remoting/** anon
                /errors/** = anon
                /bind/** = anon
                /wx/** = anon
                /unauthorized = anon
                /register = anon
                /register.do = anon
                /logout = logout
                /member/logout = anon
                /login = authc
                /member/login = memberAuthc
                /member/register = anon
                /member/register.do = anon
                /member/phonecode = anon
                /member/operationcode = anon
                /member/resetpassword = anon
                /js/** = anon
                /css/** = anon
                /product/** = anon
                /img/** = anon
                /ueditor/jsp/upload/image/** = anon
                /MP_verify_YdpEW0051d07BR8X.txt = anon
                /MP_verify_jWIslpvFiCq3GKYd.txt = anon
                /MP_verify_yXW6rBkSMeHiK8qK.txt = anon
                /fonts/** = anon
                /admin/** = role[admin]
                /wb/** = role[member]
                /** = user
            </value>
        </property>
    </bean>


</beans>