如何验证服务器SSL证书?

pra*_*ech 7 ruby ruby-on-rails httpclient httparty

我是SSL概念的新手,我正在尝试使用HTTParty连接到具有x509相互身份验证的API.

我获得了客户端证书,客户端密钥和服务器证书(都是pem文件).

我得到了它与客户端证书和密钥和使用verify: false.

现在下一步是如何验证服务器证书?

HTTParty文档链接https://github.com/jnunemaker/httparty/tree/master/docs#working-with-ssl

class ServiceClient
  include HTTParty
  DEFAULT_HEADERS = {
    'Content-Type' => 'application/json'
  }.freeze
  base_uri 'https://example.com'
  pem File.read("#{File.expand_path('.')}/path/to/certs/cert.pem")

  def self.iframe_url(**payload)
    post(
     '/test/create',
     body: payload.to_json,
     headers: DEFAULT_HEADERS,
     verify: false
    )
  end
end
Run Code Online (Sandbox Code Playgroud)

致电服务客户

payload = {user_id: "100", account_id: "1234"} 
ServiceClient.iframe_url(payload)
Run Code Online (Sandbox Code Playgroud)

编辑:

HTTParty不是一个硬性要求,所以任何http客户端的解决方案都适合我.

如果我删除verify: false我得到以下错误.

OpenSSL::SSL::SSLError: SSL_connect returned=1 errno=0 state=error: certificate verify failed
Run Code Online (Sandbox Code Playgroud)

小智 -1

使用“ OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE”

\n\n

在您的代码中,可能看起来像\xef\xbc\x9a

\n\n
    class ServiceClient\n      include HTTParty\n      OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE\n      {...your code...}\n    end\n
Run Code Online (Sandbox Code Playgroud)\n