1.pom.xml 依赖

    1. <!-- spring session -->
    2. <dependency>
    3. <groupId>org.springframework.session</groupId>
    4. <artifactId>spring-session</artifactId>
    5. </dependency>
    6. <!-- redis -->
    7. <dependency>
    8. <groupId>org.springframework.boot</groupId>
    9. <artifactId>spring-boot-starter-redis</artifactId>
    10. </dependency>

    2.配置属性

    1. #spring session使用存储类型
    2. spring.session.store-type=redis
    3. #spring session刷新模式:默认on-save
    4. spring.session.redis.flush-mode=on-save
    5. spring.session.redis.namespace=
    6. #session超时时间,单位秒
    7. server.session.timeout=30
    8. #redis
    9. spring.redis.host=localhost
    10. spring.redis.port=6379
    11. spring.redis.password=123456
    12. spring.redis.database=0
    13. spring.redis.pool.max-active=8
    14. spring.redis.pool.max-idle=8
    15. spring.redis.pool.max-wait=-1
    16. spring.redis.pool.min-idle=0
    17. spring.redis.timeout=0

    3.测试

    1. @RequestMapping(value = "/index")
    2. public String index(ModelMap map, HttpSession httpSession) {
    3. map.put("title", "第一个应用:sessionID=" + httpSession.getId());
    4. System.out.println("sessionID=" + httpSession.getId());
    5. return "index";
    6. }