如何使用redis存储HttpSession
1、代码Demo
1)引入依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.spring.platform</groupId>
<artifactId>platform-bom</artifactId>
<version>Athens-SR2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<dependency>
<!-- Import dependency management from Spring Boot -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>1.4.3.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<!-- Additional lines to be added here... -->
<dependencies>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.3.RELEASE</version>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2)配置Spring Session
添加@EnableRedisHttpSession注解,该注解会自动创建一个springSessionRepositoryFilter的Filter,这个Filter负责用Spring Session来替换原先的默认HttpSession实现,在这个例子中,Spring Session是用Redis来实现的
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@EnableRedisHttpSession
public class HttpSessionConfig {
}
3)操作HttpSession
这里我们将实现两个操作,一个是往Session中写入数据,另一个是查询数据
@RestController
public class Example {
@RequestMapping("/set")
String set(HttpServletRequest req) {
req.getSession().setAttribute("testKey", "testValue");
return "设置session:testKey=testValue";
}
@RequestMapping("/query")
String query(HttpServletRequest req) {
Object value = req.getSession().getAttribute("testKey");
return "查询Session:\"testKey\"=" + value;
}
}
4)编写main方法
编写main方法,使用@SpringBootApplication注解标注,如果查看该注解源码的话,会发现相当于添加了@SpringBootConfiguration @EnableAutoConfiguration @ComponentScan等注解。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class APP {
public static void main(String[] args) throws Exception {
SpringApplication.run(APP.class, args);
}
}
5)配置文件
在resources下新建application.properties:
spring.session.store-type = redis
#server.servlet.session.timeout =#会话超时。如果未指定持续时间后缀,则使用秒。
spring.session.redis.namespace = spring:session#用于存储会话的键的命名空间。
spring.redis.host = localhost
spring.redis.password = foobared
spring.redis.port = 6379
2、安装redis
3、配置redis
1)下载好redis
2)运行 redis-server.exe
3)打开命令指示符cd到redis文件夹
4)进入redis:输入redis-cli
5)设置密码:config set requirepass “密码”
6)输入密码:auth “密码”
4、查看缓存
1)浏览器输入http://localhost:8080/set,设置Session
2)命令指示符输入 keys *,查看redis是否有数据插入
3)浏览器输入http://localhost:8080/query,查询Session
4)清空缓存(flushall命令)
5)再次打开http://localhost:8080/query,可以看到数据消失,操作成功
本文地址:https://blog.csdn.net/aaaPostcard/article/details/110286650
上一篇: 七夕节要吃什么?
下一篇: 杨桃什么季节吃的?怎么挑选杨桃?