我正在使用 http-proxy-middleware ( https://www.npmjs.com/package/http-proxy-middleware ) 来实现另一个 REST API 的代理,该 API 启用了基于客户端证书的身份验证 (requestCert: true,拒绝未授权:真)。
客户端调用代理 API ( https://localhost:3000/auth ) 其中配置了 http-proxy-middleware 并且应该将其代理到另一个具有客户端的 REST API ( https://localhost:3002/auth )启用了基于侧证书的身份验证(requestCert:true,rejectUnauthorized:true)。
我不希望在代理上发生任何特定的身份验证。当我使用基于客户端证书的身份验证路由到此目标端点的路径调用代理时,它失败并显示错误消息:
在代理服务器中收到错误:
[HPM] Rewriting path from "/auth" to ""
[HPM] GET /auth ~> https://localhost:3002/auth
RAW REQUEST from the target {
"host": "localhost:3000",
"connection": "close"
}
redirecting to auth
[HPM] Error occurred while trying to proxy request from localhost:3000 to https://localhost:3002/auth (EPROTO) (https://nodejs.org/api/errors.html#errors_common_system_errors)
客户端收到错误:
Proxy error: Error: write EPROTO 28628:error:14094410:SSL routines:ssl3_read_bytes:sslv3 alert handshake failure:c:\ws\deps\openssl\openssl\ssl\record\rec_layer_s3.c:1536:SSL alert number 40
我不需要代理以任何方式验证/处理传入请求附带的客户端证书(我为此设置了安全:false),而只是将其转发到目标端点。我们看到从客户端收到的证书没有被传递/代理/转发到目标端点,因此基于证书的身份验证在目标端点上失败。
客户端请求直接发送到目标端点时有效,但通过 http-proxy-middleware 代理发送时无效。
我的测试服务器,客户端代码如下供参考。
是否有某种方法可以配置 http-proxy-middleware 以便它将从客户端接收到的客户端证书转发/代理到目标端点,以便客户端发送的客户端证书可用于基于证书的验证在目标 REST 端点上?
您能否指导我如何使用 http-proxy-middleware 包或任何其他合适的方式来做到这一点?提前致谢。
服务器代码
// Certificate based HTTPS Server
var authOptions = {
key: fs.readFileSync('./certs/server-key.pem'),
cert: fs.readFileSync('./certs/server-crt.pem'),
ca: fs.readFileSync('./certs/ca-crt.pem'),
requestCert: true,
rejectUnauthorized: true
};
var authApp = express();
authApp.get('/auth', function (req, res) {
res.send("data from auth");
});
var authServer = https.createServer(authOptions, authApp);
authServer.listen(3002);
// HTTP Proxy Middleware
var authProxyConfig = proxy({
target: 'https://localhost:3002/auth',
pathRewrite: {
'^/auth': '' // rewrite path
},
changeOrigin: true,
logLevel: 'debug',
secure: false,
onProxyReq: (proxyReq, req, res) => {
// Incoming request ( req ) : Not able to see the certificate that was passed by client.
// Refer the following client code for the same
},
onError: (err, req, res) => {
res.end(`Proxy error: ${err}.`);
}
});
proxyApp.use('/auth', authProxyConfig);
var unAuthOptions = {
key: fs.readFileSync('./certs/server-key.pem'),
cert: fs.readFileSync('./certs/server-crt.pem'),
ca: fs.readFileSync('./certs/ca-crt.pem'),
requestCert: false,
rejectUnauthorized: false
};
var proxyServer = https.createServer(unAuthOptions, proxyApp);
proxyServer.listen(3000);
客户代码
var fs = require('fs');
var https = require('https');
process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
var options = {
hostname: 'localhost',
port: 3000,
path: '/auth',
method: 'GET',
key: fs.readFileSync('./certs/client1-key.pem'),
cert: fs.readFileSync('./certs/client1-crt.pem'),
ca: fs.readFileSync('./certs/ca-crt.pem')
};
var req = https.request(options, function (res) {
res.on('data', function (data) {
process.stdout.write(data);
});
});
req.end();