本文共 7591 字,大约阅读时间需要 25 分钟。
增加下面代码到pom.xml
org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis
pom.xml 文件
4.0.0 cn.netkiller deploy 0.0.1-SNAPSHOT jar deploy.netkiller.cn Deploy project for Spring Boot org.springframework.boot spring-boot-starter-parent 1.4.1.RELEASE UTF-8 UTF-8 1.8 org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-data-redis org.springframework.session spring-session-data-redis org.springframework.boot spring-boot-starter-security org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-websocket org.webjars webjars-locator org.webjars sockjs-client 1.0.2 org.webjars stomp-websocket 2.3.3 org.webjars bootstrap 3.3.7 org.webjars jquery 3.1.0 org.springframework.boot spring-boot-starter-test test org.apache.tomcat.embed tomcat-embed-jasper provided javax.servlet jstl mysql mysql-connector-java junit junit test org.springframework.boot spring-boot-maven-plugin spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false spring-snapshots Spring Snapshots https://repo.spring.io/snapshot true spring-milestones Spring Milestones https://repo.spring.io/milestone false
spring.session.store-type=redis 将Session 存储在Redis中
spring.redis.database=0spring.redis.host=192.168.4.1spring.redis.port=6379#spring.redis.password=spring.redis.pool.max-active=8spring.redis.pool.max-wait=30spring.redis.pool.max-idle=8spring.redis.pool.min-idle=0spring.redis.timeout=10spring.session.store-type=redis
package cn.netkiller;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.EnableAutoConfiguration;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.ComponentScan;import org.springframework.data.jpa.repository.config.EnableJpaRepositories;import org.springframework.data.mongodb.repository.config.EnableMongoRepositories;import org.springframework.scheduling.annotation.EnableScheduling;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@SpringBootApplication@EnableAutoConfiguration@ComponentScan@EnableMongoRepositories@EnableJpaRepositories@EnableSchedulingpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
RedisHttpSessionConfig.java
package cn.netkiller.config;import org.springframework.context.annotation.Configuration;import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;@Configuration@EnableRedisHttpSessionpublic class RedisHttpSessionConfig { public RedisHttpSessionConfig() { // TODO Auto-generated constructor stub }}
package cn.netkiller.web;import java.util.Date;import javax.servlet.http.HttpSession;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.ResponseBody;@Controllerpublic class TestController { public TestController() { // TODO Auto-generated constructor stub } @RequestMapping("/session/set") @ResponseBody public String set(HttpSession session) { String key = "test"; session.setAttribute(key, new Date()); return key; } @RequestMapping("/session/get") @ResponseBody public String get(HttpSession session) { String value = (String) session.getAttribute("test").toString(); return value; }}
keys spring:session:* 查看 Session Key
$ telnet 192.168.4.1 6379 Connecting to 192.168.4.1:6379... Connection established. To escape to local shell, press 'Ctrl+Alt+]'. keys spring:session:* *7 $68 spring:session:sessions:expires:a510f46f-0a2f-4649-af05-34bd750562c1 $40 spring:session:expirations:1476100200000 $40 spring:session:expirations:1476098400000 $60 spring:session:sessions:f6494a2f-591e-42ba-b381-ce2596f4046d $60 spring:session:sessions:a510f46f-0a2f-4649-af05-34bd750562c1 $112 spring:session:index:org.springframework.session.FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME:user $60 spring:session:sessions:627018c8-243e-43ac-87b9-fc07f130c899
spring.session.store-type=jdbcspring.session.jdbc.table-name=SESSIONS
原文出处:Netkiller 系列 手札
本文作者:陈景峯 转载请与作者联系,同时请务必标明文章原始出处和作者信息及本声明。