SpringBoot+Redis+Session共享

1.导入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
 
<dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
</dependency>

2.application.properties的redis配置

#redis
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=123456
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
#超时一定要大于0
spring.redis.timeout=3000
spring.session.store-type=redis

没有密码就不写,简单配置

spring.redis.host=127.0.0.1
spring.redis.port=6379

在配置redis时需要确保redis安装正确,并且配置notify-keyspace-events Egx,spring.redis.timeout设置为大于0,这里配置为0时springboot时启不起来。

3.在启动类上添加注解@EnableRedisHttpSession

import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;


@EnableRedisHttpSession

4.重点,我在集成时遇到的大坑,我是用Redis缓存User的Session,作用就是,在同一浏览器下session共享,达到,同一网站,在登录后,再次开一个窗口,依然是登录状态。重点!!!!需要将User实体类实现序列化接口

public class User implements Serializable
发布了242 篇原创文章 · 获赞 7 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/shujuku____/article/details/105367740
今日推荐