shiro安全框架异常退出没有清除缓存信息处理方案
2017-09-11 17:53
429 查看
最近项目遇到问题,shiro框异常退出没有清除缓存信息,服务器重启后,又拿旧的缓存session来登录,造成后台报错。
这里转载网友的文章,记录一下解决方法。大体就是重写sessionManager类,做一个清除操作。
配置默认会话管理器:
<bean id="sessionManager" class="com.xzjc.common.security.SimpleWebSessionManager">
<property name="globalSessionTimeout" value="15000" />
<property name="sessionValidationInterval" value="30000" />
<property name="sessionValidationSchedulerEnabled" value="true" />
</bean>
全局的会话信息设置成15秒,检测扫描信息间隔30秒,第三个参数就是是否开启扫描
重写管理器类的一个方法
package com.xzjc.common.security;
import java.util.Collection;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.session.ExpiredSessionException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionKey;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
/**
* 会话管理器,重写
* @author zhouyujie
*/
public class SimpleWebSessionManager extends DefaultWebSessionManager {
private CacheManager cacheManager;
private final static Logger logger = Logger.getLogger(SimpleWebSessionManager.class);
public SimpleWebSessionManager() {
super();
}
public void validateSessions() {
if (logger.isInfoEnabled()){
logger.info("Validating all active sessions...");
}
int invalidCount = 0;
Collection<?> activeSessions = getActiveSessions();
if (activeSessions != null && !activeSessions.isEmpty()) {
for (Iterator<?> i$ = activeSessions.iterator(); i$.hasNext();) {
Session session = (Session) i$.next();
try {
SessionKey key = new DefaultSessionKey(session.getId());
validate(session, key);
} catch (InvalidSessionException e) {
if (cacheManager != null) {
SimpleSession s = (SimpleSession) session;
if (s.getAttribute("portal.session.id") != null){
cacheManager.getCache(null).remove(s.getAttribute("portal.session.id"));
}
}
if (logger.isDebugEnabled()) {
boolean expired = e instanceof ExpiredSessionException;
String msg = (new StringBuilder()).append("Invalidated session with id [").append(session.getId()).append("]").append(expired ? " (expired)" : " (stopped)").toString();
logger.debug(msg);
}
invalidCount++;
}
}
}
if (logger.isInfoEnabled()) {
String msg = "Finished session validation.";
if (invalidCount > 0){
msg = (new StringBuilder()).append(msg).append("[").append(invalidCount).append("] sessions were stopped.").toString();
}else{
msg = (new StringBuilder()).append(msg).append("No sessions were stopped.").toString();
}
logger.info(msg);
}
}
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
}
这样就好使了。
这里转载网友的文章,记录一下解决方法。大体就是重写sessionManager类,做一个清除操作。
配置默认会话管理器:
<bean id="sessionManager" class="com.xzjc.common.security.SimpleWebSessionManager">
<property name="globalSessionTimeout" value="15000" />
<property name="sessionValidationInterval" value="30000" />
<property name="sessionValidationSchedulerEnabled" value="true" />
</bean>
全局的会话信息设置成15秒,检测扫描信息间隔30秒,第三个参数就是是否开启扫描
重写管理器类的一个方法
package com.xzjc.common.security;
import java.util.Collection;
import java.util.Iterator;
import org.apache.log4j.Logger;
import org.apache.shiro.cache.CacheManager;
import org.apache.shiro.session.ExpiredSessionException;
import org.apache.shiro.session.InvalidSessionException;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.DefaultSessionKey;
import org.apache.shiro.session.mgt.SessionKey;
import org.apache.shiro.session.mgt.SimpleSession;
import org.apache.shiro.web.session.mgt.DefaultWebSessionManager;
/**
* 会话管理器,重写
* @author zhouyujie
*/
public class SimpleWebSessionManager extends DefaultWebSessionManager {
private CacheManager cacheManager;
private final static Logger logger = Logger.getLogger(SimpleWebSessionManager.class);
public SimpleWebSessionManager() {
super();
}
public void validateSessions() {
if (logger.isInfoEnabled()){
logger.info("Validating all active sessions...");
}
int invalidCount = 0;
Collection<?> activeSessions = getActiveSessions();
if (activeSessions != null && !activeSessions.isEmpty()) {
for (Iterator<?> i$ = activeSessions.iterator(); i$.hasNext();) {
Session session = (Session) i$.next();
try {
SessionKey key = new DefaultSessionKey(session.getId());
validate(session, key);
} catch (InvalidSessionException e) {
if (cacheManager != null) {
SimpleSession s = (SimpleSession) session;
if (s.getAttribute("portal.session.id") != null){
cacheManager.getCache(null).remove(s.getAttribute("portal.session.id"));
}
}
if (logger.isDebugEnabled()) {
boolean expired = e instanceof ExpiredSessionException;
String msg = (new StringBuilder()).append("Invalidated session with id [").append(session.getId()).append("]").append(expired ? " (expired)" : " (stopped)").toString();
logger.debug(msg);
}
invalidCount++;
}
}
}
if (logger.isInfoEnabled()) {
String msg = "Finished session validation.";
if (invalidCount > 0){
msg = (new StringBuilder()).append(msg).append("[").append(invalidCount).append("] sessions were stopped.").toString();
}else{
msg = (new StringBuilder()).append(msg).append("No sessions were stopped.").toString();
}
logger.info(msg);
}
}
public void setCacheManager(CacheManager cacheManager) {
this.cacheManager = cacheManager;
}
}
这样就好使了。
相关文章推荐
- shiro安全框架扩展教程--异常退出没有清除缓存信息处理方案
- shiro安全框架扩展教程--异常退出没有清除缓存信息处理方案
- shiro安全框架扩展教程--异常退出没有清除缓存信息处理方案
- shiro安全框架扩展教程--如何扩展realm桥接器并退出自动清空角色资源缓存
- 用EhCache缓存Shiro的本地会话,当用户没有安全退出就直接关闭浏览器,会话可能在缓存里孤立的问题。
- JSP用户安全退出【清除浏览器缓存中页面】
- shiro安全框架扩展教程--如何扩展实现我们的缓存机制(第三方容器redis,memcached)
- Spring Boot系列(十五) 安全框架Apache Shiro(二)缓存-基于Hazelcast的分布式缓存
- Spring Boot系列(十五) 安全框架Apache Shiro(二)缓存-EhCache
- shiro安全框架扩展教程--如何扩展实现我们的缓存机制(第三方容器redis,memcached)
- shiro安全框架扩展教程--如何扩展实现我们的缓存机制(第三方容器redis,memcached)
- ASP.NET网站管理系统退出 清除浏览器缓存,Session的代码
- shiro安全框架扩展教程--整合cas框架扩展自定义CasRealm
- shiro安全框架|基础知识:登录逻辑
- Apache Shiro(安全框架)
- Shiro安全框架入门篇(登录验证实例详解与源码)
- 安全认证框架-Apache Shiro研究心得
- YII Framework框架教程之安全方案详解
- 安全认证框架-apache shiro研究心得
- shiro安全框架扩展教程--整合cas框架扩展自定义CasRealm