转自:http://www.zblog.us/java/shiro_session_manager.html
Subject currentUser = SecurityUtils.getSubject();
Session session = currentUser.getSession();
session.setAttribute( "someKey", someValue);
返回值 | 方法名 | 描述 |
Object | getAttribute(Object key) | 根据key标识返回绑定到session的对象 |
Collection<Object> | getAttributeKeys() | 获取在session中存储的所有的key |
String | getHost() | 获取当前主机ip地址,如果未知,返回null |
Serializable | getId() | 获取session的唯一id |
Date | getLastAccessTime() | 获取最后的访问时间 |
Date | getStartTimestamp() | 获取session的启动时间 |
long | getTimeout() | 获取session失效时间,单位毫秒 |
void | setTimeout(long maxIdleTimeInMillis) | 设置session的失效时间 |
Object | removeAttribute(Object key) | 通过key移除session中绑定的对象 |
void | setAttribute(Object key, Object value) | 设置session会话属性 |
void | stop() | 销毁会话 |
void | touch() | 更新会话最后访问时间 |
public Session start(SessionContext context) throws AuthorizationException {
//委托给SessionManager
return this.sessionManager.start(context);
}
public Session getSession(SessionKey key) throws SessionException {
//委托给SessionManager
return this.sessionManager.getSession(key);
}
[main]
...
sessionManager = com.foo.my.SessionManagerImplementation
securityManager.sessionManager = $sessionManager
会话失效时间
[main]
...
# 3,600,000 milliseconds = 1 hour
securityManager.sessionManager.globalSessionTimeout = 3600000
DefaultWebSessionManager容器无关的SessionMannager
ini配置
sessionIdCookie=org.apache.shiro.web.servlet.SimpleCookie
sessionManager=org.apache.shiro.web.session.mgt.DefaultWebSessionManager
sessionIdCookie.name=sid
#sessionIdCookie.domain=zblog.us
#sessionIdCookie.path=
sessionIdCookie.maxAge=1800
sessionIdCookie.httpOnly=true
sessionManager.sessionIdCookie=$sessionIdCookie
sessionManager.sessionIdCookieEnabled=true
securityManager.sessionManager=$sessionManager
[main]
...
aSessionListener = com.foo.my.SessionListener
anotherSessionListener = com.foo.my.OtherSessionListener
securityManager.sessionManager.sessionListeners = $aSessionListener, $anotherSessionListener
public class MySessionListener implements SessionListener {
@Override
public void onStart(Session session) {
//会话创建时触发
System.out.println("会话创建:" + session.getId());
}
@Override
public void onExpiration(Session session) {
//会话过期时触发
System.out.println("会话过期:" + session.getId());
}
@Override
public void onStop(Session session) {
//退出/会话过期时触发
System.out.println("会话停止:" + session.getId());
}
}
[main]
...
sessionDAO = com.foo.my.SessionDAO
securityManager.sessionManager.sessionDAO = $sessionDAO
[main]
...
sessionManager = org.apache.shiro.web.session.mgt.DefaultWebSessionManager
securityManager.sessionManager = $sessionManagers
# Configure a SessionDAO and then set it:
securityManager.sessionManager.sessionDAO = $sessionDAO
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.2.3</version>
</dependency>
sessionDAO=org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO
#shiro默认的activeSessionsCacheName为shiro-activeSessionCache,
#如需重命名,可以这样设置
#sessionDAO.activeSessionsCacheName=shiro-activeSessionCache
sessionManager.sessionDAO=$sessionDAO
cacheManager = org.apache.shiro.cache.ehcache.EhCacheManager
#shiro提供了默认的配置文件,如需自定义可以这样设置
#cacheManager.cacheManagerConfigFile=classpath:ehcache.xml
securityManager.cacheManager = $cacheManager
<cache name="shiro-activeSessionCache"
maxElementsInMemory="10000"
overflowToDisk="true"
eternal="true"
timeToLiveSeconds="0"
timeToIdleSeconds="0"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
Session ID生成器
[main]
...
sessionIdGenerator = com.my.session.SessionIdGenerator
securityManager.sessionManager.sessionDAO.sessionIdGenerator = $sessionIdGenerator
//如DefaultSessionManager在创建完session后会调用该方法;
//如保存到关系数据库/文件系统/NoSQL数据库;即可以实现会话的持久化;
//返回会话ID;主要此处返回的ID.equals(session.getId());
Serializable create(Session session);
//根据会话ID获取会话
Session readSession(Serializable sessionId) throws UnknownSessionException;
//更新会话;如更新会话最后访问时间/停止会话/设置超时时间/设置移除属性等会调用
void update(Session session) throws UnknownSessionException;
//删除会话;当会话过期/会话停止(如用户退出时)会调用
void delete(Session session);
//获取当前所有活跃用户
Collection<Session> getActiveSessions();
[main]
...
sessionValidationScheduler = org.apache.shiro.session.mgt.ExecutorServiceSessionValidationScheduler
# 默认是3,600,000 毫秒 = 1 小时:
sessionValidationScheduler.interval = 3600000
securityManager.sessionManager.sessionValidationScheduler = $sessionValidationScheduler
[main]
...
securityManager.sessionManager.sessionValidationSchedulerEnabled = false
main]
...
securityManager.sessionManager.deleteInvalidSessions = false
<error-page>
<exception-type>org.apache.shiro.session.InvalidSessionException</exception-type>
<location>/invalidSession.jsp</location>
</error-page>
对于shiro实现集群功能,后续文章会介绍。