ng 服务 --proxy-config 与 NTLM 身份验证不起作用

问题描述 投票:0回答:3

我正在尝试让 Angular cli 的内部网络服务器(我认为 webpack 使用 node-http-proxy)来使用 NTLM 身份验证,但效果不佳。

我像这样设置了 webpack 代理:

// in packages.json
...
"scripts": {
    "start": "ng serve --proxy-config proxy.conf.json",
...

proxy.config.json的内容是:

{
  "/srv": {
    "target": "http://localhost/access_form",
    "logLevel": "debug",
    "auth": "LOGIN:PASS"
  }
}

我尝试将 onProxyRes 函数添加到 JSON 选项对象,但这无法启动网络服务器。

有人对这个设置感到幸运吗?有什么指点吗?

angular webpack webpack-dev-server angular-cli node-http-proxy
3个回答
5
投票

我可以通过使用以下内容作为我的

proxy.config.js
文件来完成此工作,该文件可以传递到 angular-cli 工具,如下所示
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;

确保安装了agentkeepalive软件包:

npm install --save-dev agentkeepalive

更多信息请访问:


1
投票

http-proxy-middleware 第 39 期有部分解决方案,但有一个问题:

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(',');
      }
    }
  }
}

以下是讨论:https://github.com/chimurai/http-proxy-middleware/issues/39

包括我在内的一些用户收到异常“TypeError:cb 不是函数”。讨论引用了一个 nodejs/node 问题:“Uncaught TypeError using http.Agent in keep-alive mode #8650”,目前似乎尚未解决。

以下是讨论:https://github.com/nodejs/node/issues/8650


0
投票

@jacobappleton 给出的上述解决方案有效,但似乎连接仅针对单个请求打开。我对使用 NTLM 的了解非常有限。有人可以帮我弄清楚如何使连接保持更长的时间,以便可以执行多个 API 请求,而不是为每个请求一次又一次地打开连接吗?

© www.soinside.com 2019 - 2024. All rights reserved.