Node.js:需要目录的客户端证书(并允许重试)

标签 node.js certificate ssl-certificate

我想在 Node js 中创建一个反向代理,我可以在其中要求网站某些部分的客户端证书。

  • /secure 部分应需要客户端证书。
  • /nosecure 部分不需要客户端证书。
  • 如果用户之前未提供有效的证书,则应该能够重试客户端证书验证。
    var options = {
        key: fs.readFileSync(__dirname + '/key.pem'),
        cert: fs.readFileSync(__dirname + '/cert.pem'),
        ca: fs.readFileSync(__dirname + '/clientCA.pem'),
        requestCert: true
    };

    https.createServer(options, function (req, res) {
        //parse url
        req.parsedUrl = url.parse(req.url);

        //handle urls
        switch(req.parsedUrl.pathname) {
            case '/nosecure':
                /*
                 * This location does not require a client cert
                 */

                res.end('nosecure');
                break;
            case '/secure':
                /*
                 * This location requires a client certificate, which can be checked by getPeerCertificate()
                 * If no certificate is provided, the user will be redirected to secureError
                 */

                res.end('secure');
                break;
            case '/secureError':
                /*
                 * This location does not require a client cert
                 * It displays the error page, in case the client cert was not provided/validated on /secure.
                 */

                res.end('Certificate validation failed. <a href="/secure">Try again</a>');
                break;
            default:
                res.end('not found');       
                break;
        }
    }).listen(9000);

通常您只能在服务器级别设置这些要求。 但我可以手动检查安全部分中的证书。

但是问题仍然存在。我无法要求客户端浏览器再次向我发送证书。 这仅在 SSL session 启动时发生一次。

关于如何解决这个问题有什么想法吗?

最佳答案

我自己是个菜鸟,但我也在玩同样的事情。

也许是这样的

if (req.parsedUrl.pathname == '/secure' && !req.connection.getPeerCertificate().valid_to) req.parsedUrl.pathname = '/secureError';

关于Node.js:需要目录的客户端证书(并允许重试),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9375497/

相关文章:

linux - 带有 --prof 选项的 Node 创建多个日志文件而不是一个 v8.log

c# - windows服务器证书导出导入问题

java - 为什么我的证书没有在 https 上的 jboss 4.2 上启用?

node.js - 在没有 IIS 和私钥的情况下将 Nodejs 应用程序部署到 Windows

ssl - 使用包括中间证书在内的 key 将 PFX 转换为 PEM

html - 在jade中显示逗号分隔的数组?

node.js - 顺序递增跳数

node.js - 使用时间比较将数据插入mongodb

iis - 我可以在实时 Azure 云上使用自签名证书吗?

python - 使用 Psycopg2 与 Redshift 的 SSL 连接