如何使用 Windows 身份验证 * 和 HTTPS* 配置 ng 服务代理

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

我想将

ng serve
与受 Windows 身份验证 (NTLM) 保护的后端服务器一起使用。这与这些帖子中的情况几乎相同(示例 1示例 2),只不过服务器是通过 HTTPS 访问的。

当我尝试使用相同的建议解决方案(适用于 HTTP)时,我收到 404 错误(但当然可以通过此 URL 访问服务器,我可以直接使用浏览器进行测试)。

const Agent = require("agentkeepalive");

module.exports = {
    '/api': {
        target: "https://my-server.example.com",
        secure: false,
        changeOrigin: true,
        agent: new Agent({
            maxSockets: 100,
            keepAlive: true,
            maxFreeSockets: 10,
            keepAliveMsecs: 100000,
            timeout: 6000000,
            keepAliveTimeout: 90000
        }),
        onProxyRes: proxyRes => {
            const key = "www-authenticate";
            proxyRes.headers[key] = proxyRes.headers[key] &&
                proxyRes.headers[key].split(",");
        }
    }
}

任何帮助,包括一些诊断问题的方法,我们将不胜感激。

更新:使用 logLevel = "debug" 我得到以下堆栈跟踪(我不明白,因为相同的代理配置在 HTTPS 上运行良好,如果不是 Windows 配置的话):

TypeError [ERR_INVALID_PROTOCOL]: Protocol "https:" not supported. Expected "http:"
    at new ClientRequest (_http_client.js:119:11)
    at Object.request (https.js:289:10)
    at Array.stream (C:\myproject\node_modules\http-proxy\lib\http-proxy\passes\web-incoming.js:126:74)      
    at ProxyServer.<anonymous> (C:\myproject\node_modules\http-proxy\lib\http-proxy\index.js:81:21)
    at middleware (C:\myproject\node_modules\http-proxy-middleware\lib\index.js:46:13)
    at handle (C:\myproject\node_modules\webpack-dev-server\lib\Server.js:322:18)
    at app.use (C:\myproject\node_modules\webpack-dev-server\lib\Server.js:330:47)
    at Layer.handle_error (C:\myproject\node_modules\express\lib\router\layer.js:71:5)
    at trim_prefix (C:\myproject\node_modules\express\lib\router\index.js:315:13)
    at C:\myproject\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\myproject\node_modules\express\lib\router\index.js:335:12)
    at next (C:\myproject\node_modules\express\lib\router\index.js:275:10)
    at Layer.handle [as handle_request] (C:\myproject\node_modules\express\lib\router\layer.js:97:5)
    at trim_prefix (C:\myproject\node_modules\express\lib\router\index.js:317:13)
    at C:\myproject\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\myproject\node_modules\express\lib\router\index.js:335:12)
angular-cli windows-authentication webpack-dev-server
2个回答
14
投票

通常,当我经过几天的努力在 Stack Overflow 上发布问题时,我会在接下来的一个小时内自己找到解决方案......

问题是

Agent
仅适用于 HTTP。对于 HTTPS,我们必须使用
HttpsAgent
:

const HttpsAgent = require('agentkeepalive').HttpsAgent;

module.exports = {
    '/api': {
        target: "https://my-server.example.com",
        secure: false,
        changeOrigin: true,
        agent: new HttpsAgent({
            maxSockets: 100,
            keepAlive: true,
            maxFreeSockets: 10,
            keepAliveMsecs: 100000,
            timeout: 6000000,
            keepAliveTimeout: 90000
        }),
        onProxyRes: proxyRes => {
            const key = "www-authenticate";
            proxyRes.headers[key] = proxyRes.headers[key] &&
                proxyRes.headers[key].split(",");
        }
    }
}

0
投票

我在最新的 .net + Angular +Spa 模板和添加 Windows 身份验证方面面临类似的问题,有人可以分享一个包含所有配置的 git 存储库吗?这会非常有帮助。

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