spring boot整合redis实现shiro的分布式session共享的方法

 更新时间:2018年01月26日 16:11:32   作者:牛奋lch  
本篇文章主要介绍了spring boot整合redis实现shiro的分布式session共享的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

我们知道,shiro是通过SessionManager来管理Session的,而对于Session的操作则是通过SessionDao来实现的,默认的情况下,shiro实现了两种SessionDao,分别为CachingSessionDAO和MemorySessionDAO,当我们使用EhCache缓存时,则是使用的CachingSessionDAO,不适用缓存的情况下,就会选择基于内存的SessionDao.所以,如果我们想实现基于Redis的分布式Session共享,重点在于重写SessionManager中的SessionDao。我们的重写代码如下:

package com.chhliu.springboot.shiro.cache; 
import java.io.Serializable; 
import java.util.Collection; 
import java.util.concurrent.TimeUnit; 
import org.apache.shiro.session.Session; 
import org.apache.shiro.session.UnknownSessionException; 
import org.apache.shiro.session.mgt.eis.AbstractSessionDAO; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.stereotype.Service;  
@Service 
@SuppressWarnings({ "rawtypes", "unchecked" }) 
public class RedisSessionDao extends AbstractSessionDAO { 
 
  // Session超时时间,单位为毫秒 
  private long expireTime = 120000; 
 
  @Autowired 
  private RedisTemplate redisTemplate;// Redis操作类,对这个使用不熟悉的,可以参考前面的博客 
 
  public RedisSessionDao() { 
    super(); 
  } 
 
  public RedisSessionDao(long expireTime, RedisTemplate redisTemplate) { 
    super(); 
    this.expireTime = expireTime; 
    this.redisTemplate = redisTemplate; 
  } 
 
  @Override // 更新session 
  public void update(Session session) throws UnknownSessionException { 
    System.out.println("===============update================"); 
    if (session == null || session.getId() == null) { 
      return; 
    } 
    session.setTimeout(expireTime); 
    redisTemplate.opsForValue().set(session.getId(), session, expireTime, TimeUnit.MILLISECONDS); 
  } 
 
  @Override // 删除session 
  public void delete(Session session) { 
    System.out.println("===============delete================"); 
    if (null == session) { 
      return; 
    } 
    redisTemplate.opsForValue().getOperations().delete(session.getId()); 
  } 
 
  @Override// 获取活跃的session,可以用来统计在线人数,如果要实现这个功能,可以在将session加入redis时指定一个session前缀,统计的时候则使用keys("session-prefix*")的方式来模糊查找redis中所有的session集合 
  public Collection<Session> getActiveSessions() { 
    System.out.println("==============getActiveSessions================="); 
    return redisTemplate.keys("*"); 
  } 
 
  @Override// 加入session 
  protected Serializable doCreate(Session session) { 
    System.out.println("===============doCreate================"); 
    Serializable sessionId = this.generateSessionId(session); 
    this.assignSessionId(session, sessionId); 
 
    redisTemplate.opsForValue().set(session.getId(), session, expireTime, TimeUnit.MILLISECONDS); 
    return sessionId; 
  } 
 
  @Override// 读取session 
  protected Session doReadSession(Serializable sessionId) { 
    System.out.println("==============doReadSession================="); 
    if (sessionId == null) { 
      return null; 
    } 
    return (Session) redisTemplate.opsForValue().get(sessionId); 
  } 
 
  public long getExpireTime() { 
    return expireTime; 
  } 
 
  public void setExpireTime(long expireTime) { 
    this.expireTime = expireTime; 
  } 
 
  public RedisTemplate getRedisTemplate() { 
    return redisTemplate; 
  } 
 
  public void setRedisTemplate(RedisTemplate redisTemplate) { 
    this.redisTemplate = redisTemplate; 
  } 
} 

SessionDao实现完了之后,我们就需要将SessionDao加入SessionManager中了,代码如下:

 @Bean 
  public DefaultWebSessionManager configWebSessionManager(){ 
    DefaultWebSessionManager manager = new DefaultWebSessionManager(); 
    manager.setCacheManager(cacheManager);// 加入缓存管理器 
    manager.setSessionDAO(sessionDao);// 设置SessionDao 
    manager.setDeleteInvalidSessions(true);// 删除过期的session 
    manager.setGlobalSessionTimeout(sessionDao.getExpireTime());// 设置全局session超时时间 
    manager.setSessionValidationSchedulerEnabled(true);// 是否定时检查session 
     
    return manager; 
  } 

最后一步就是将SessionManager配置到SecurityManager中了

@Bean 
  public SecurityManager securityManager(DefaultWebSessionManager webSessionManager) { 
    DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); 
    // 设置realm. 
    securityManager.setRealm(myShiroRealm()); 
 
    // 注入缓存管理器; 
    securityManager.setCacheManager(cacheManager);// 这个如果执行多次,也是同样的一个对象; 
     
    // session管理器 
    securityManager.setSessionManager(webSessionManager); 
     
    //注入记住我管理器; 
    securityManager.setRememberMeManager(rememberMeManager()); 
    return securityManager; 
  } 

测试结果如下:

==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
===============update================ 
==============doReadSession================= 
==============doReadSession================= 
===============update================ 
==============doReadSession================= 
==============doReadSession================= 
==============doReadSession================= 
权限配置-->MyShiroRealm.doGetAuthorizationInfo() 
==============doReadSession================= 

我们会发现,当一个页面中存在多个资源的时候,会不停的调用doReadSession,update方法来读取和更新session,目前这个问题还没有想到比较好的解决方案。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java中MapStruct入门使用及对比

    Java中MapStruct入门使用及对比

    MapStruct是一个Java注解处理器框架,用于简化Java Bean之间的映射,本文主要介绍了Java中MapStruct入门使用及对比,感兴趣的可以了解一下
    2023-12-12
  • ManyToMany单向、双向:@JoinTable的使用

    ManyToMany单向、双向:@JoinTable的使用

    这篇文章主要介绍了ManyToMany单向、双向:@JoinTable的使用方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-12-12
  • 关于springboot集成阿里云短信的问题

    关于springboot集成阿里云短信的问题

    这篇文章主要介绍了springboot集成阿里云短信的方法,本文通过实例代码图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-11-11
  • Spring Cloud OpenFeign实例介绍使用方法

    Spring Cloud OpenFeign实例介绍使用方法

    Spring Cloud OpenFeign 对 Feign 进行了二次封装,使得在 Spring Cloud 中使用 Feign 的时候,可以做到使用 HTTP 请求访问远程服务,就像调用本地方法一样的,开发者完全感知不到这是在调用远程访问,更感知不到在访问 HTTP 请求
    2022-09-09
  • JDBC连接mysql乱码异常问题处理总结

    JDBC连接mysql乱码异常问题处理总结

    这篇文章主要介绍了JDBC连接mysql乱码异常问题处理的办法和思路,有需要的朋友参考学习下。
    2017-12-12
  • Java并发编程之ConcurrentLinkedQueue解读

    Java并发编程之ConcurrentLinkedQueue解读

    这篇文章主要介绍了Java并发编程之ConcurrentLinkedQueue解读,非阻塞的实现方式则可以使用循环CAS的方式来实现,而ConcurrentLinkedQueue就是juc包中自带的经典非堵塞方式实现的工具类,需要的朋友可以参考下
    2023-12-12
  • java如何接收和发送ASCII数据

    java如何接收和发送ASCII数据

    这篇文章主要介绍了java如何接收和发送ASCII数据问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教
    2023-09-09
  • MyEclipse2018中安装Mybatis generator插件的实现步骤

    MyEclipse2018中安装Mybatis generator插件的实现步骤

    这篇文章主要介绍了MyEclipse2018中安装Mybatis generator插件的实现步骤,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-02-02
  • Spring Bean生命周期之BeanDefinition的合并过程详解

    Spring Bean生命周期之BeanDefinition的合并过程详解

    这篇文章主要为大家详细介绍了Spring Bean生命周期之BeanDefinition的合并过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助

    2022-03-03
  • 基于OAuth2.0授权系统的验证码功能的实现

    基于OAuth2.0授权系统的验证码功能的实现

    本篇教程给大家分享基于OAuth2.0授权系统的验证码功能的实现,验证码功能的实现是采用Zuul网关的Filter过滤器进行校验验证码,具体实现代码跟随小编一起看看吧
    2021-05-05

最新评论