<?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>net.xqlee.project</groupId>
<artifactId>demo-springboot-spring-session</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<name>demo-springboot-spring-session</name>
<description>lee-leftso-v2.0</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.8.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<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-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
核心配置类,HttpSessionConfig:
package net.xqlee.project.config;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
/** 启用redis session **/
/***
*
* @author xqlee maxInactiveIntervalInSeconds设置session超时时间,默认1800秒
*/
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 1800)
public class HttpSessionConfig {
// spring boot项目可以直接配置application配置文件自动注册redis connect 工厂
// 非boot项目则需要再这里配置一下redis得链接工厂
}
编写一个测试的controller,TestController:
package net.xqlee.project.controller;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@GetMapping("/test.do")
public Object testSession(HttpServletRequest request) {
Map<String, Object> obj = new HashMap<>();
obj.put("result", true);
request.getSession().setAttribute("time", new Date().getTime());
obj.put("time", request.getSession().getAttribute("time"));
return obj;
}
}
application配置文件配置redis的链接信息
spring.redis.host=10.1.1.2
spring.redis.password=
spring.redis.port=6379
具体的redis配置信息根据自己的环境改变
https://www.leftso.com/article/298.html