问题
I'm trying to get angular cli's internal webserver (webpack uses node-http-proxy I think) to work with NTLM authentication and coming up short.
I set up the webpack proxy like this:
// in packages.json
...
"scripts": {
"start": "ng serve --proxy-config proxy.conf.json",
...
The contents of proxy.config.json is:
{
"/srv": {
"target": "http://localhost/access_form",
"logLevel": "debug",
"auth": "LOGIN:PASS"
}
}
I'm trying to add a onProxyRes function to the JSON options object but this fails to start the webserver.
Has anyone had any luck with this set up? Any pointers?
回答1:
I was able to get this working by using the following as my proxy.config.js
file that can be passed to the angular-cli tool like so ng serve --watch --proxy-config proxy.config.js
:
var Agent = require("agentkeepalive");
var keepaliveAgent = new Agent({
maxSockets: 100,
keepAlive: true,
maxFreeSockets: 10,
keepAliveMsecs: 1000,
timeout: 60000,
keepAliveTimeout: 30000 // free socket keepalive for 30 seconds
});
var onProxyRes = function (proxyRes, req, res) {
var key = 'www-authenticate';
proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
};
const PROXY_CONFIG = [
{
target: "http://localhost:12345",
context: "/api",
secure: false,
changeOrigin: true,
auth: "LOGIN:PASS",
loglevel: "debug",
onProxyRes: onProxyRes,
agent: keepaliveAgent
}
];
module.exports = PROXY_CONFIG;
Make sure you install the agentkeepalive package:
npm install --save-dev agentkeepalive
Further information can be found at:
- https://github.com/chimurai/http-proxy-middleware/issues/39
- https://github.com/angular/angular-cli/blob/4008768f8634c18bafd3c8e69e45f6464934e9fe/docs/documentation/stories/proxy.md
- https://github.com/nodejitsu/node-http-proxy/blob/master/examples/http/ntlm-authentication.js
回答2:
There is a partial solution in http-proxy-middleware issue 39, but it has an issue:
var Agent = require('agentkeepalive');
{
devServer: {
'/api/*': {
target: 'http://localhost:12121',
logLevel: 'debug',
agent: new Agent({
maxSockets: 100,
keepAlive: true,
maxFreeSockets: 10,
keepAliveMsecs:1000,
timeout: 60000,
keepAliveTimeout: 30000 // free socket keepalive for 30 seconds
}),
onProxyRes: proxyRes => {
var key = 'www-authenticate';
proxyRes.headers[key] = proxyRes.headers[key] && proxyRes.headers[key].split(',');
}
}
}
}
Here's the discussion: https://github.com/chimurai/http-proxy-middleware/issues/39
Some users, including me, are getting the exception "TypeError: cb is not a function". The discussion references a nodejs/node issue: "Uncaught TypeError using http.Agent in keep-alive mode #8650" that appears to be unresolved at this time.
Here's the discussion: https://github.com/nodejs/node/issues/8650
来源:https://stackoverflow.com/questions/40777763/ng-serve-proxy-config-with-ntlm-authentication-is-not-working