webpack-dev-server设置proxy代理导致请求无法携带cookie解决方法

配置代理导致cookie的pathdomain不符,具体原因可查看F12控制台network具体请求的cookies标签,会有感叹号标识。

在proxy配置中加入如下设置,手动修改set-cookie的值(以修改Path为例,若修改domain亦同理):

      onProxyRes: function (proxyRes, req, res) {
        var cookies = proxyRes.headers['set-cookie'];
        var cookieRegex = /Path=\/XXX\//i;
        if (cookies) {
          var newCookie = cookies.map(function (cookie) {
            if (cookieRegex.test(cookie)) {
              return cookie.replace(cookieRegex, 'Path=/');
            }
            return cookie + ';Path=/';
          });
          delete proxyRes.headers['set-cookie'];
          proxyRes.headers['set-cookie'] = newCookie;
        }
      }

参考:
https://blog.csdn.net/weixin_34235105/article/details/91420519
https://github.com/gefeiyanga/blog/blob/master/%E7%AC%94%E8%AE%B0/umi-request%26axios.md

你可能感兴趣的:(webpack-dev-server设置proxy代理导致请求无法携带cookie解决方法)