代码拉取完成,页面将自动刷新
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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。