ng serve --proxy-config with NTLM authentication is not working

你。 提交于 2021-01-27 07:43:32

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!