一、Spring Boot 进行跨域请求
1 package com.atguigu.gulimall.gateway.config; 2 3 import org.springframework.context.annotation.Bean; 4 import org.springframework.context.annotation.Configuration; 5 import org.springframework.web.cors.CorsConfiguration; 6 import org.springframework.web.cors.reactive.CorsConfigurationSource; 7 import org.springframework.web.cors.reactive.CorsWebFilter; 8 import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource; 9 import org.springframework.web.server.ServerWebExchange; 10 11 @Configuration 12 public class GulimallCorsConfiguration { 13 14 @Bean 15 public CorsWebFilter corsWebFilter(){ 16 UrlBasedCorsConfigurationSource source=new UrlBasedCorsConfigurationSource(); 17 CorsConfiguration cc=new CorsConfiguration(); 18 cc.addAllowedOrigin("*"); 19 cc.addAllowedMethod("*"); 20 cc.addAllowedHeader("*"); 21 cc.setAllowCredentials(true);//跨域请求默认不包含cookie,设置为true可以包含 22 source.registerCorsConfiguration("/**",cc); 23 CorsWebFilter corsWebFilter = new CorsWebFilter(source); 24 return null; 25 } 26 }
二、SpringSession to Spring Boot
1、1依赖
<!--springsession整合redis-->
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
1、2文档
1、3配置
spring.session.store-type=redis #session保存类型
server.servlet.session.timeout= #过期时间 spring.session.redis.flush-mode=on_save # Sessions flush mode. spring.session.redis.namespace=spring:session # Namespace for keys used to store sessions.
1、4配置redis
spring.redis.host=localhost # Redis server host. spring.redis.password= # Login password of the redis server. spring.redis.port=6379 # Redis server port.
1、5JSON序列化
1、6最终自定义配置
1 @Configuration 2 public class XmallSessionConfig { 3 @Bean 4 public CookieSerializer cookieSerializer() { 5 DefaultCookieSerializer serializer = new DefaultCookieSerializer(); 6 serializer.setCookieName("JSESSIONID"); 7 serializer.setDomainName("gulimall.com"); 8 return serializer; 9 } 10 @Bean 11 public RedisSerializer<Object> springSessionDefaultRedisSerializer() { 12 return new GenericJackson2JsonRedisSerializer(); 13 } 14 }
1、7核心原理
1)、@EnableRedisHttpSession导入RedisHttpSessionConfiguration配置
1、给容器中台添加了一个组件
SessionRepository=》【RedisOperationsSessionRepository】=》redis 操作session。
2、SessionRepositoryFilter=》Filter:session‘存储过滤器;每个请求过来都必须经过filter‘
1、创建的时候,就自动从容器中获取l了SessionRepository;
2、原始的request,response都被包装。SessionRepositoryRequestWrapper,SessionRepositoryResponseWrapper
3、以后获取session。request.getSession();
4、wrappedRequest.getSession();=》SessionRepository 中获取到的
装饰者模式;
自动延期:redis中的数据也是有过期时间的
原文:https://www.cnblogs.com/ffzzcommsoft/p/14825688.html