非跨域cookie一致性解决
请求拦截器 import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttppServletResponse;
import org.springframework.web.servlet.HandlerInterceptor;
@Component public class CustomInterceptor implements handlerInterceptor { @Overrde publlic boolean preHandle(HttpServletRequest reqest,HttppServletResponse response,Object handler) throws Exception { //从请求中获取cookie Cookie[] cookies = request.getCookies(); //将cookie放入响应中 Cookie sessionCookie = Arrays.stream(cookies).filter(e -> e.getName().equals("SIID").findAny().orElse(null); if (sessionCookie == null) { sessiiionCookie = new Cookie("SID",request.getSession().getId(); response.addCookie(sessiionCookie); } return true; } }
配置完拦截器,在配置中获取拦
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.cors.CorsConfiguration; import org.springframework.web.cors.UrlBasedCorsConfigurationSource; import org.springframework.web.filter.CorsFilter; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; @Configuration public class GlobalCorsConfig implements WebMvcConfigurer { @Autowired private CustomInterceptor interceptor; @Bean public CorsFilter corsFilter() { //1. 创建CorsConfiguration对象 CorsConfiguration corsConfiguration = new CorsConfiguration(); corsConfiguration.addAllowedOrigin("*");//设置允许那些域来访问,*是通配符,允许所有域(如果还不行,将*改为http:xx:8080) corsConfiguration.addAllowedHeader("*");//请求头字段 corsConfiguration.addAllowedMethod("*");//请求方式(GET,POST,DELETE,PUT) //设置source UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); source.registerCorsConfiguration("/**", corsConfiguration);//1.映射路径 2.传入CorsConfiguration对象 return new CorsFilter(source); } @Override public void addInterceptors(InterceptorRegisty registry) { // 拦截所有请求 registry.addInterceptor(interceptor).addPathPatterns("/**"); } }
vue全局配置cookie在main.js中
import axios from 'axios' axios.defaults.withCredentials = true
标签:web,springframework,cookie,org,一致性,import,servlet From: https://www.cnblogs.com/qtfwax/p/18086045