一、先把需要的依赖导进来
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
二、配置
server:
port: 8080
spring:
redis:
host: 127.0.0.1
password:
session:
store-type: redis
timeout: 60
三、配置RedisSessionConfig
import org.springframework.context.annotation.Configuration;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
@Configuration
@EnableRedisHttpSession
public class RedisSessionConfig {
}
四、controller层
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
@RestController
public class HelloController {
@RequestMapping(value = "/setsession")
public Object setSession(@RequestParam(required=false) String value, HttpSession session) {
session.setAttribute("value", value);
return session.getId();
}
@RequestMapping(value = "/getsession")
public Object getSession(HttpSession session) {
Object value = session.getAttribute("value");
Map<String, Object> map = new HashMap<>();
map.put("8080-->sessionId", session.getId());
map.put("value", value);
return map;
}
}
五、启动springboot项目
写两个相同的模块 不同的端口 8080 /9090验证session共享
![](https://i-blog.csdnimg.cn/blog_migrate/50c11bcff21721e4431fc99415772149.png)