|
|
|
@ -66,3 +66,55 @@ export default defineConfig({
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
## webpack和vite中设置开发环境下代理服务器的cookie
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- webpack的实现
|
|
|
|
|
|
|
|
``` javascript
|
|
|
|
|
|
|
|
devServer: {
|
|
|
|
|
|
|
|
proxy: {
|
|
|
|
|
|
|
|
'/dev/api': {
|
|
|
|
|
|
|
|
target: 'https://example.com', // 代理的目标地址
|
|
|
|
|
|
|
|
changeOrigin: true,
|
|
|
|
|
|
|
|
pathRewrite: {
|
|
|
|
|
|
|
|
'/dev/api': ''
|
|
|
|
|
|
|
|
},
|
|
|
|
|
|
|
|
// 这里实现
|
|
|
|
|
|
|
|
onProxyReq: function (proxyReq, req, res) {
|
|
|
|
|
|
|
|
// 设置固定的cookie, 支持多个cookie同时设置
|
|
|
|
|
|
|
|
// proxyReq.setHeader('Cookie', 'token=xxx; name=123; pwd=456;')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 直接转发当前域名下的所有cokkie, 配合浏览器插件注入cookie
|
|
|
|
|
|
|
|
if (req.headers.cookie) {
|
|
|
|
|
|
|
|
proxyReq.setHeader('Cookie', req.headers.cookie);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
- vite的实现
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
|
|
|
server: {
|
|
|
|
|
|
|
|
proxy: {
|
|
|
|
|
|
|
|
'/dev/api': {
|
|
|
|
|
|
|
|
target: 'https://example.com', // 代理的目标地址
|
|
|
|
|
|
|
|
changeOrigin: true,
|
|
|
|
|
|
|
|
rewrite: (path) => path.replace('/dev/api', ''),
|
|
|
|
|
|
|
|
configure: (proxyReq, _options) => {
|
|
|
|
|
|
|
|
// 这里实现
|
|
|
|
|
|
|
|
proxy.on('proxyReq', function (proxyReq, req, res, options) {
|
|
|
|
|
|
|
|
// 设置固定的cookie, 支持多个cookie同时设置
|
|
|
|
|
|
|
|
// proxyReq.setHeader('Cookie', 'token=xxx; name=123; pwd=456;')
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 直接转发当前域名下的所有cokkie, 配合浏览器插件注入cookie
|
|
|
|
|
|
|
|
if (req.headers.cookie) {
|
|
|
|
|
|
|
|
proxyReq.setHeader('Cookie', req.headers.cookie);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
```
|
|
|
|
|