1 Star 0 Fork 0

walker911/shiro-demo

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
RedisShiroSessionDAO.java 2.17 KB
一键复制 编辑 原始数据 按行查看 历史
walker911 提交于 2018-02-05 22:42 . class
package com.shiro.config;
import com.shiro.config.RedisKeys;
import org.apache.shiro.session.Session;
import org.apache.shiro.session.mgt.eis.EnterpriseCacheSessionDAO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;
/**
* shiro session dao
*
*/
@Component
public class RedisShiroSessionDAO extends EnterpriseCacheSessionDAO {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
private static Logger logger = LoggerFactory.getLogger(RedisShiroSessionDAO.class);
//创建session
@Override
protected Serializable doCreate(Session session) {
Serializable sessionId = super.doCreate(session);
final String key = RedisKeys.getShiroSessionKey(sessionId.toString());
setShiroSession(key, session);
return sessionId;
}
//获取session
@Override
protected Session doReadSession(Serializable sessionId) {
Session session = super.doReadSession(sessionId);
if (session == null) {
final String key = RedisKeys.getShiroSessionKey(sessionId.toString());
session = getShiroSession(key);
}
return session;
}
//更新session
@Override
protected void doUpdate(Session session) {
super.doUpdate(session);
final String key = RedisKeys.getShiroSessionKey(session.getId().toString());
setShiroSession(key, session);
}
//删除session
@Override
protected void doDelete(Session session) {
final String key = RedisKeys.getShiroSessionKey(session.getId().toString());
redisTemplate.delete(key);
}
private Session getShiroSession(String key) {
return (Session)redisTemplate.opsForValue().get(key);
}
private void setShiroSession(String key, Session session){
if(!redisTemplate.hasKey(key)) {
redisTemplate.opsForValue().set(key, session);
}
//60分钟过期
redisTemplate.expire(key, 60, TimeUnit.MINUTES);
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/walker911/shiro-demo.git
git@gitee.com:walker911/shiro-demo.git
walker911
shiro-demo
shiro-demo
master

搜索帮助