使用Spring Session和Redis管理session

来源:互联网 发布:vue.js调试工具 编辑:程序博客网 时间:2024/05/18 11:22
  1. 使用Spring Boot以及Gradle构建项目
  2. 加入依赖 
    compile ‘org.springframework.session:spring-session:1.2.0.RELEASE’ 
    compile ‘org.springframework.boot:spring-boot-starter-data-redis’
  3. 使用注解 @EnableRedisHttpSession
  4. application.properties配置 
    加入redis配置:
spring.redis.database=0spring.redis.host=localhostspring.redis.password=spring.redis.pool.max-active=8spring.redis.pool.max-idle=8spring.redis.pool.max-wait=-1spring.redis.pool.min-idle=0spring.redis.port=6379
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

加入spring session 配置:

spring.session.store-type=redis
  • 1
  • 1

最后进行测试

@RequestMapping("/")@ResponseBodyString home(HttpSession session) {    session.setAttribute("test", new Date());    return "Hello World!";}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

还是原来使用session的方式,可以在redis中查看到一条记录。

阅读全文
0 0
原创粉丝点击