spring boot 使用 redis session

发布时间:2020-07-02 02:13:49 作者:北极冷冷冷
来源:网络 阅读:1166

spring boot redis 使用 2

使用redis共享session

分布式系统中,Session 共享有很多的解决方案,其中托管到缓存中应该是最常用的方案之一

  1. pom文件中引入依赖

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

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

spring session 中默认加入了 security 不添加 security 依赖会抛异常:
java.lang.ClassNotFoundException: org.springframework.security.web.authentication.RememberMeServices

2.redis 配置

#redis配置
#Redis服务器地址
spring.redis.host=192.168.5.10
#Redis服务器连接端口
spring.redis.port=6379
#Redis数据库索引(默认为0)
spring.redis.database=4
#连接池最大连接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=50
#连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=3000
#连接池中的最大空闲连接
spring.redis.jedis.pool.max-idle=20
#连接池中的最小空闲连接
spring.redis.jedis.pool.min-idle=2
#连接超时时间(毫秒)
spring.redis.timeout=5000

spring.session.store-type=redis

3.在启动类中开启 redis session

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
@MapperScan("com.example.demo.dao.mybatis")
@EnableCaching
@EnableRedisHttpSession
public class DemoApplication 

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class}) 去掉 security 权限认证自动装配
不去掉会引起权限不足

----end----

推荐阅读:
  1. springBoot(20):使用Spring Session实现集群-redis
  2. spring boot + redis 实现session共享

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

boot redis session edi

上一篇:kubeadm快速搭建k8s集群

下一篇:Live Exchange到Office 365遷移軟件

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》