Created
February 22, 2019 09:37
-
-
Save neilwong2012/d4b3c51de6760722b6680f488c8e8f14 to your computer and use it in GitHub Desktop.
webpack proxy setcookie
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var cookiejar = require('cookiejar') | |
let cookie | |
function relayRequestHeaders(proxyReq, req) { | |
Object.keys(req.headers).forEach((key) => { | |
proxyReq.setHeader(key, req.headers[key]) | |
}) | |
if (cookie) { | |
proxyReq.setHeader('cookie', cookie) | |
} | |
} | |
function relayResponseHeaders(proxyRes) { | |
const setCookieHeaders = proxyRes.headers['set-cookie'] || [] | |
const modifiedSetCookieHeaders = setCookieHeaders | |
.map(str => new cookiejar.Cookie(str)) | |
.map((item) => { | |
item.secure = false | |
return item | |
}) | |
.map(item => item.toString()) | |
proxyRes.headers['set-cookie'] = modifiedSetCookieHeaders | |
proxyRes.headers['Access-Control-Allow-Credentials'] = true | |
proxyRes.headers['Access-Control-Allow-Headers'] = 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild' | |
proxyRes.headers['Access-Control-Allow-Methods'] = 'PUT, POST, GET, DELETE, OPTIONS' | |
} | |
proxyTable: [{ | |
context: ['/opapi', '/auth', '/logincenter'], | |
//target: 'https://test.ipalfish.com:30000/', | |
target: 'https://www.example.com0/', | |
changeOrigin: true, | |
secure: false, | |
cookieDomainRewrite: { | |
'*': 'localhost', // 把相应的 cookie 域都设置成 localhost | |
}, | |
cookiePathRewrite: { | |
'*': '/', | |
}, | |
onProxyReq: relayRequestHeaders, | |
onProxyRes: relayResponseHeaders, | |
debug: true | |
}], |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment