axios的baseURL设为localhost可带cookie,改成IP则无法带cookie,后端却能获取cookie为何?

前端

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);
}
阅读 5.2k
1 个回答
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题