前端
const axios = Axios.create({
baseURL: 'http://localhost:8066', 访问后台时可以正常携带cookie
// baseURL: 'http://192.168.1.107:8066', 访问后台时可以无法携带cookie
withCredentials:true,
timeout: 5000,
});
baseURL设置为本机IP时无法携带cookie,但是后端却可以获取cookie
后端代码
配置
@Configuration
public class CorsConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
//设置允许跨域的路径
registry.addMapping("/**")
//设置允许跨域请求的域名
.allowedOrigins("*")
//.allowedOrigins("http://localhost:8080")
//是否允许证书 不再默认开启
.allowCredentials(true)
//设置允许的方法
.allowedMethods("*")
//跨域允许时间
.maxAge(3600);
}
}
都试了,没区别
.allowedOrigins("*")和.allowedOrigins("http://localhost:8080")
写cookie:
public static void writeCookie(HttpServletResponse response, String cookieName, String value) {
Cookie cookie = new Cookie(cookieName, value);
cookie.setPath("/");
cookie.setMaxAge(3000 * 60);
response.addCookie(cookie);
}
找到问题了,因为我访问前端项目的时候地址栏输的是
http://localhost:8080/
地址栏改为ip就好了
http://192.168.1.107:8080/