SpringBoot使用Redis缓存Shiro的Session以及遇到的坑

博主使用的是SpringBoot,如果是使用SpringMVC做框架则只要将注解转化为xml就可以了。


实现思路
重写shiro的`AbstractSessionDAO`,并将其注入到Shiro的SessionManager中,在SpringBoot的注入过程可以参考[这篇](https://blog.csdn.net/madonghyu/article/details/80111917),只要将缓存缓存redis就可以了。

接下来主要是redis的使用

  1. 首先是SpringBoot整合Redis,首先在pom文件加入依赖
        <!-- redis整合SpringBoot用 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
  1. 然后在SpringBoot中配置redis的具体配置
spring:
  redis:
    database: 0
    host: xxx
    port: 6379
    pool:
      max-active: 5000
      max-idle: 5000
      max-wait: -1
      min-idle: 0
    password: xxx
    timeout: 2000
  1. 最后在springboot的启动函数加上注解@EnableCaching就可以了。SpringBoot主要配置了RedisTemplate和StringRedisTemplate这两个来操作redis,当然也可以自己配置。具体配置如下
/**
 * 单机版的Redis的配置实现
 */
@Configuration
public class RedisConfig extends CachingConfigurerSupport {

    @Bean
    //这里可以自定义cacheManager,像是自定义全局的缓存时间之类
    public CacheManager cacheManager(RedisTemplate<?, ?> redisTemplate) {
        return new RedisCacheManager(redisTemplate);
    }


    @Bean
    //自定义的cache生成器
    public KeyGenerator keyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
                sb.append(obj.toString());
            }
            return sb.toString();
        };
    }

    @Bean
    @ConditionalOnMissingBean(name = "redisTemplate")
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
        // 1.创建 redisTemplate 模版
        RedisTemplate<String, Object> template = new RedisTemplate<>();
        // 2.关联 redisConnectionFactory
        template.setConnectionFactory(redisConnectionFactory);
        // 3.创建 序列化类
        Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class);
        ObjectMapper om = new ObjectMapper();

        // 4.设置可见度
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        // 5.启动默认的类型
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        // 6.序列化类,对象映射设置
        jackson2JsonRedisSerializer.setObjectMapper(om);
        // 7.设置 value 的转化格式和 key 的转化格式
        template.setValueSerializer(jackson2JsonRedisSerializer);
        template.setKeySerializer(new StringRedisSerializer());
        template.afterPropertiesSet();
        return template;
    }

    @Bean
    @ConditionalOnMissingBean(StringRedisTemplate.class)
    public RedisTemplate<String, String> stringRedisTemplate(RedisConnectionFactory factory) {
        return new StringRedisTemplate(factory);
    }

}


上面就是redis的配置,事实上你可以省略第三步的setValueSerializer,通过查看template.afterPropertiesSet();这个函数的源码,可以发现当没有设置ValueSerializer的时候,会默认使用JdkSerializationRedisSerializer,这个序列化模式会将value序列化成字节码,这样缓存shiro的session就没有什么问题,当是redis数据库的数据将是字节码,不方便观察。如果只是想实现缓存,不想将其序列化化为json字符串,你完全可以只使用SpringBoot自动配置的template。


  1. 下面主要说一下shiro的session缓存出错的解决方法,主要问题是session的序列化问题,如果使用上面的序列化方式,shiro的session序列化的时候会多出属性,这时使用阿里巴巴的fastjson也会序列化失败(至少我是失败的,可能是配置问题)
  2. 解决session序列化问题方法也很简单,第三步的setValueSerializer不配置就可以了。
    如果想要redis数据库的数据为json字符串,那么可以在其他用到缓存的地方使用StringRedisTemplate,或者再定义一个template。
  3. 我是直接自定义一个序列化方法将session序列化成字符串。
    即自己实现一个将session序列化为字符串和反序列化为SimpleSession的函数。
  4. 总结,shiro的session序列化成json字符串比较难,因此最好直接将其序列化成字节码。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Nginx是一款高性能的HTTP和反向代理服务器,可以实现负载均衡和高可用性。Spring Boot是一个快速构建应用程序的框架,Shiro是一个用于安全验证和访问控制的框架,而Session共享是指将用户会话信息存储在多个服务器上,以实现用户请求的无缝切换。 在Nginx和Spring Boot集群中使用Shiro时,可以使用Session共享来实现用户的无状态会话管理。具体实施如下: 1. 集群配置:将多个Spring Boot实例部署在不同的服务器上,并使用Nginx进行反向代理和负载均衡。通过配置Nginx的upstream块,将请求分发给多个Spring Boot实例。 2. Session共享:使用分布式缓存来实现Session共享。可以选择使用Redis、Memcached等分布式缓存系统,将Session数据存储在缓存中。在Spring Boot中,可以使用Spring Session提供的支持来进行Session共享的配置。 3. Shiro配置:在Spring Boot中,可以使用Shiro框架来处理身份验证和访问控制。在Shiro中,可以实现自定义的SubjectDAO来将Session存储到分布式缓存中。同时,通过配置ShiroSessionManager和SessionDAO,将Session的读取和存储操作委托给分布式缓存系统。 4. 应用程序开发:在Spring Boot应用程序中使用Shiro时,可以通过注解或配置文件来配置身份验证和访问控制规则。在验证用户身份后,可以将用户信息存储在Session中,并通过Session共享机制,在多个服务器上共享该Session数据。 通过以上步骤,可以实现在Nginx、Spring Boot集群中使用Shiro框架进行Session共享。这样,用户可以在不同的服务器上无缝切换,并享受到一致的用户体验。同时,通过Nginx的负载均衡机制,可以提高系统的性能和可伸缩性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值