1.导入依赖
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
<!-- <version>2.3.0.RELEASE</version>-->
</dependency>
2.编写配置类 (其他详细配置请参照官方文档)[https://spring.io/projects/spring-session]
@Configuration
public class GulimallSessionConfig {
@Bean
public CookieSerializer cookieSerializer(){
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
cookieSerializer.setDomainName("gulimall.com");
cookieSerializer.setCookieName("GULISESSION");
return cookieSerializer;
}
@Bean
public RedisSerializer<Object> springSessionDefaultRedisSerializer() {
return new GenericJackson2JsonRedisSerializer();
}
}
3.开启全局session注解
@EnableRedisHttpSession //启动类上加
4.编写yml文件
spring.session.store-type: redis
5.像session中添加我们想要添加的数据
@GetMapping(value = "/login")
public String login(LoginUser loginUser,HttpSession session){
session.setAttribute("loginUser",loginUser);
return "login";
}
6.再页面中取出使用(这里使用thymeleaf模板引擎)
// 取值方式
1. [[${session.loginUser.attrxxx}]]
2. <span th:text="${session.loginUser.attrxxx}"></span>
标签:return,cookieSerializer,spring,loginUser,session,springcloud,public,分布式 From: https://www.cnblogs.com/huoziqi/p/17579844.html