1. 程式人生 > >配置shiro會話監聽

配置shiro會話監聽

配置監聽類

import org.apache.shiro.session.Session;
import org.apache.shiro.session.SessionListener;

public class SessionListener1 implements SessionListener {

	@Override
	public void onStart(Session session) {//會話建立觸發 已進入shiro的過濾連就觸發這個方法
		// TODO Auto-generated method stub
		System.out.println("會話建立:" + session.getId());
	}

	@Override
	public void onStop(Session session) {//退出
		// TODO Auto-generated method stub
		System.out.println("退出會話:" + session.getId());
	}

	@Override
	public void onExpiration(Session session) {//會話過期時觸發
		// TODO Auto-generated method stub
		System.out.println("會話過期:" + session.getId()); 
	}

}

在shiro的配置檔案中新增啟動監聽類的入口:
<!-- Shiro's main business-tier object for web-enabled applications -->
	<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
		<property name="realm" ref="shiroDbRealm" />
		<property name="cacheManager" ref="shiroEhcacheManager" />
		<property name="sessionManager" ref="sessionManager" /> <!-- 配置sessionListener -->
	</bean>
	
	<!-- 自定義session監聽器 -->
	<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
		<property name="sessionListeners" ref="sessionListener1" />
	</bean>
    <bean id="sessionListener1" class="org.springside.examples.quickstart.service.sessionservice.SessionListener1"></bean>