fns*_*jdb 15 cookies proxy node.js webpack webpack-dev-server
我正在尝试在我的webpack开发服务器中设置代理.问题是我不控制我正在连接的服务器,我需要验证请求.
有没有办法可以将cookie添加到我发送给代理服务器的请求中?我查看了webpack dev服务器代理服务器页面,以及它链接到的node-http-proxy页面,我没有看到任何cookie提及.我也不确定是否有办法让我看到这些转发的请求,所以我无法判断我正在尝试做什么.
有任何想法吗?
小智 5
如果只需要重写代理的cookie域,请在node-http-proxy中检出cookieDomainRewrite选项。
此外,如果您想找到一种在请求/响应上围绕Cookie注入自定义行为的方法,那么请查看您可以关联的事件:
proxy.on('proxyRes', function (proxyRes, req, res) {
console.log('RAW Response from the target',JSON.stringify(proxyRes.headers, true, 2));
});
proxy.on('proxyReq', function (proxyRes, req, res) {
console.log('RAW Request from the target',JSON.stringify(proxyReq.headers, true, 2));
});
Run Code Online (Sandbox Code Playgroud)
https://github.com/nodejitsu/node-http-proxy#listening-for-proxy-events
这些选项可以添加到devServer代理的webpack.config.js中,如下所示:
{
devServer: {
proxy: {
onProxyReq: function(proxyReq, req, res){
proxyReq.setHeader('x-added', 'foobar');
},
cookieDomainRewrite: ""
}
}
}
Run Code Online (Sandbox Code Playgroud)
https://github.com/chimurai/http-proxy-middleware#http-proxy-events
进一步研究后,开发服务器似乎确实会转发您发送给它的任何 cookie。没有用于我想要做的身份验证,我想亚马逊有一些我无法解释的更多安全措施,但这就是答案。
将 cookie 添加到您发送到开发服务器的请求中,并正确设置代理。