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

2023-12-24

我想用ng serve与受保护的后端服务器Windows 身份验证(NTLM)。这与这些帖子中的情况几乎相同(示例1 https://stackoverflow.com/questions/42981555/angular-cli-windows-authentication-backend, 示例2 https://stackoverflow.com/questions/40777763/ng-serve-proxy-config-with-ntlm-authentication-is-not-working),除非通过以下方式访问服务器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(",");
        }
    }
}

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

UPDATE:使用 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)

通常,当我经过几天的努力在 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(",");
        }
    }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Windows 身份验证 * 和 HTTPS* 配置 ng 服务代理 的相关文章

随机推荐