如何使用证书对HTTP Web请求进行身份验证?

3
我需要能够向要求客户端使用特定证书进行身份验证的Web服务发送HTTP GET请求。.net代码如下:
if (certificate != null) 
    request.ClientCertificates.Add(certificate); 
return request; 

我还没有找到Rails中的等效方法。有什么建议吗?

1个回答

3
如果使用基本的net/https,那么就很简单:
require 'net/https'
require 'uri'

uri = URI.parse(ARGV[0] || 'https://localhost/')
http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true if uri.scheme == "https"  # enable SSL/TLS
http.key = pkey #Sets an OpenSSL::PKey::RSA or OpenSSL::PKey::DSA object.
http.cert= cert #Sets an OpenSSL::X509::Certificate object as client certificate

http.start {
  http.request_get(uri.path) {|res|
    print res.body
  }
}

如果您将它们合并,您需要使用一些openssl实用程序方法对它们进行处理。

对于自定义http客户端,您应该阅读ruby openssl库的文档以了解详细信息。

但简单来说,类似以下内容应该可以工作:

ctx = OpenSSL::SSL::SSLContext.new
ctx.key = private_key_file
ctx.cert = certificate_file

然后为您的连接提供上下文。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接