加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – RestClient无法使用SSL客户端证书获取资源

发布时间:2020-12-17 03:23:04 所属栏目:百科 来源:网络整理
导读:我正在尝试使用RestClient来检索使用SSL客户端证书保护的页面.我的代码如下: require 'restclient'p12 = OpenSSL::PKCS12.new(File.read('client.p12'),'password')client = RestClient::Resource.new('https://example.com/',:ssl_client_key = p12.key,:v
我正在尝试使用RestClient来检索使用SSL客户端证书保护的页面.我的代码如下:

require 'restclient'

p12 = OpenSSL::PKCS12.new(File.read('client.p12'),'password')
client = RestClient::Resource.new('https://example.com/',:ssl_client_key => p12.key,:verify_ssl => OpenSSL::SSL::VERIFY_NONE)
client.get

当我运行它时,我看到以下故障:

1.9.3-p374 :007 > client.get
RestClient::BadRequest: 400 Bad Request
    from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/abstract_response.rb:48:in `return!'
    from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:230:in `process_result'
    from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:178:in `block in transmit'
    from /home/duncan/.rvm/rubies/ruby-1.9.3-p374/lib/ruby/1.9.1/net/http.rb:745:in `start'
    from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:172:in `transmit'
    from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:64:in `execute'
    from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/request.rb:33:in `execute'
    from /home/duncan/.rvm/gems/ruby-1.9.3-p374/gems/rest-client-1.6.7/lib/restclient/resource.rb:51:in `get'
    from (irb):7
    from /home/duncan/.rvm/rubies/ruby-1.9.3-p374/bin/irb:13:in `<main>'

我很确定这是一次验证失败,因为如果我没有安装客户端证书,我会在浏览器中遇到同样的错误.

我正在使用OpenSSL :: SSL :: VERIFY_NONE,因为服务器有一个自签名证书,我相信这是传递给忽略它的正确值.

任何有关如何使这项工作的建议将不胜感激 – 即使指向一些详细的文档,或建议不同的宝石可以工作.我对Gem docs或Google没有太多运气:(

解决方法

您的HTTPS请求将需要客户端证书以及密钥.尝试:

client = RestClient::Resource.new('https://example.com/',:ssl_client_cert => p12.certificate,:verify_ssl => OpenSSL::SSL::VERIFY_NONE)

如果这不起作用,您可以尝试捕获握手数据包(例如使用WireShark)以验证API是否提供证书.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读