pom.xml
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.wzq</groupId>
<artifactId>session</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>session</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-session-data-redis-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!--redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
servlet:
session:
timeout: 3600s
spring:
application:
name: sesion
session:
store-type: redis
redis:
namespace: spring:session
timeout: 3600s
redis:
database: 1
host: 192.168.0.45
port: 6379
password: 123456
timeout: 20000
lettuce:
pool:
pool:
max-active: 8
max-wait: -1
max-idle: 8
min-idle: 0
run.class
package com.wzq.session;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@SpringBootApplication
/**
* maxInactiveIntervalInSeconds 设置session超时时间 默认 30分钟
*/
@EnableRedisHttpSession(maxInactiveIntervalInSeconds = 5000)
public class SessionApplication {
public static void main(String[] args) {
SpringApplication.run(SessionApplication.class, args);
}
}
test方法
package com.wzq.session.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
/**
* @description: 测试Controller
* @author: Wzq
* @create: 2020-09-03 14:37
*/
@RestController
@RequestMapping
public class TestController {
/**
* 登录
* @param session
* @return
*/
@RequestMapping("login")
public String login(HttpSession session,String msg){
session.setAttribute("msg",msg);
return session.getId();
}
/**
* 测试方法
* @param session
* @return
*/
@RequestMapping("test")
public Map<String, Object> test(HttpSession session){
String sessionId = session.getId();
Object msg = session.getAttribute("msg");
Map<String,Object> map = new HashMap<>();
map.put("sessionId", sessionId);
map.put("msg", msg);
return map;
}
}