日期:2014-05-16  浏览次数:21076 次

apache-shiro杂记(一) 统计在线用户数目
org.apache.shiro.session.mgt.eis.SessionDAO接口就有此功能,不过配置shiro的时候,最好在容器上下文中指定SessionDAO bean的名字
以方便直接注入到业务bean
package org.apache.shiro.session.mgt.eis;

import org.apache.shiro.session.Session;
import org.apache.shiro.session.UnknownSessionException;
import java.io.Serializable;
import java.util.Collection;

public interface SessionDAO {

    Serializable create(Session session);

    Session readSession(Serializable sessionId) throws UnknownSessionException;

    void update(Session session) throws UnknownSessionException;

    void delete(Session session);

    Collection<Session> getActiveSessions();		// 就是这个方法
}


<bean id="sessionManager" class="org.apache.shiro.web.session.mgt.DefaultWebSessionManager">
		<property name="sessionDAO" ref="sessionDAO" />
	</bean>

<bean id="sessionDAO" class="org.apache.shiro.session.mgt.eis.MemorySessionDAO" />

<bean id="securityManager" class="org.apache.shiro.web.mgt.DefaultWebSecurityManager">
	<property name="authenticator" ref="authenticator" />
	<property name="sessionManager" ref="sessionManager" />
	<property name="cacheManager" ref="cacheManager" />
	<property name="realms">
		<list>
			<bean class="com.ztgame.sd.security.realm.JdbcRealm">
				<property name="userDao" ref="userDao" />
				<property name="credentialsMatcher" ref="hashedCredentialsMatcher" />
			</bean>
		</list>
	</property>
</bean>