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

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxIdle" value="100" />
		<property name="minIdle" value="10" />
		<property name="testOnBorrow" value="true" />
	</bean>

	<bean id="redisConnectionFactory"
		class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
		<property name="hostName" value="${jedis.host}" />
		<property name="port" value="${jedis.port}" />
		<property name="password" value="${jedis.password}" />
		<property name="timeout" value="${jedis.timeout}" />
		<property name="poolConfig" ref="jedisPoolConfig" />
		<!-- <property name="database" value="${jedis.dbIndex}" /> -->
	</bean>

	<bean id="redisTemplate" class="com.goafanti.core.cache.template.FastJsonRedisTemplate">
		<constructor-arg index="0" ref="redisConnectionFactory" />
	</bean>
	<bean id="sessionRedisTemplate" class="com.goafanti.core.shiro.cache.template.SessionRedisTemplate">
		<constructor-arg index="0" ref="redisConnectionFactory" />
	</bean>
	
	<bean id="cacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
		<constructor-arg index="0" ref="redisTemplate" />
		<property name="defaultExpiration" value="86400" />		
	</bean>
	
	<bean id="shrioRedisCacheManager" class="com.goafanti.core.shiro.cache.ShiroRedisCacheManager">
		<property name="redisTemplate" ref="sessionRedisTemplate" />
		<property name="expire" value="2592000" />
	</bean>
	

      

	<bean id="sessionIdGenerator"
		class="org.apache.shiro.session.mgt.eis.JavaUuidSessionIdGenerator" />

	<bean id="sessionIdCookie" class="org.apache.shiro.web.servlet.SimpleCookie">
		<constructor-arg value="AFT_SID" />
		<property name="httpOnly" value="true" />
		<property name="maxAge" value="2592000" />
		<!-- <property name="domain" value=""/> -->
	</bean>

	<bean id="customSessionListener" class="com.goafanti.core.shiro.listener.CustomSessionListener">
		<property name="redisTemplate" ref="sessionRedisTemplate" />
	</bean>

	<bean id="enterpriseCacheSessionDAO"
		class="org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO">
		<property name="sessionIdGenerator" ref="sessionIdGenerator" />
	</bean>

	<!-- -->
	<bean id="sessionValidationScheduler"
		class="org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler">
		<!-- -->
		<property name="interval" value="${session.validate.timespan}" />
		<property name="sessionManager" ref="sessionManager" />
	</bean>
	<!-- -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="userRealm" />
		<property name="sessionManager" ref="sessionManager" />
		<property name="cacheManager" ref="shrioRedisCacheManager" />
	</bean>

	<bean id="authorizationAttributeSourceAdvisor"
		class="org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor">
		<property name="securityManager" ref="securityManager" />
	</bean>

	<!-- credential matcher -->
	<bean id="credentialsMatcher"
		class="org.apache.shiro.authc.credential.HashedCredentialsMatcher">
		<property name="hashAlgorithmName" value="${pwd.hash_algorithm_name}" />
		<property name="hashIterations" value="${pwd.hash_iterations}" />
		<property name="storedCredentialsHexEncoded" value="true" />
	</bean>
	<!-- -->
	<bean id="userRealm" class="com.goafanti.core.shiro.token.UserRealm">
		<property name="credentialsMatcher" ref="credentialsMatcher" />
	</bean>

	<!-- Session Manager -->
	<bean id="sessionManager" class="com.goafanti.core.shiro.session.CustomSessionManager">
		<!-- class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager"> -->
		<property name="sessionIdUrlRewritingEnabled" value="false" />
		<!-- -->
		<property name="sessionValidationInterval" value="3600000" />
		<!-- -->
		<property name="globalSessionTimeout" value="3600000" />
		<property name="sessionDAO" ref="enterpriseCacheSessionDAO" />
		<property name="cacheManager" ref="shrioRedisCacheManager" />
		<!-- session -->
		<property name="sessionListeners">
			<list>
				<ref bean="customSessionListener" />
			</list>
		</property>
		<!-- -->
		<property name="sessionValidationScheduler" ref="sessionValidationScheduler" />
		<!-- -->
		<property name="sessionValidationSchedulerEnabled" value="true" />
		<!-- -->
		<property name="deleteInvalidSessions" value="true" />
		<!-- -->
		<property name="sessionIdCookie" ref="sessionIdCookie" />
	</bean>

	<bean id="shiroManager" class="com.goafanti.core.shiro.service.impl.ShiroManagerImpl" />

	<bean id="shiroFilter" class="org.apache.shiro.spring.web.ShiroFilterFactoryBean">
		<property name="securityManager" ref="securityManager" />
		<property name="loginUrl" value="/user/login" />
		<property name="successUrl" value="/" />
		<property name="unauthorizedUrl" value="/user/login" />
		<property name="filterChainDefinitions" value="#{shiroManager.loadFilterChainDefinitions()}" />
		<property name="filters">
			<util:map>
				<entry key="login">
					<bean id="login" class="com.goafanti.core.shiro.filter.LoginFilter" />
				</entry>
				<entry key="permission">
					<bean id="permission" class="com.goafanti.core.shiro.filter.PermissionFilter">
						<property name="loginUrl" value="/admin/login" />
					</bean>
				</entry>
				<entry key="admin">
					<bean id="admin" class="com.goafanti.core.shiro.filter.AdminFilter">
						<property name="loginUrl" value="/admin/login" />
					</bean>
				</entry>
			</util:map>
		</property>
	</bean>
	<!-- -->
	<bean id="lifecycleBeanPostProcessor" class="org.apache.shiro.spring.LifecycleBeanPostProcessor" />
</beans>