美好的一天!
我正在尝试通过ssl连接向服务器发送POST请求,其中包含来自Windows 7和Ruby的法拉第库的.p12证书。
Ruby的版本是ruby 2.3.3p222(2016-11-21修订版56859)[x64-mingw32]
法拉第宝石的版本是:法拉第(0.14.0,0.9.2)
1)我有一个包含以下证书文件的文件夹:
2)关于代码,我有以下内容:
require "faraday"
require "json"
require "openssl"
data = [JSON_object]
host = 'https://[domain_name]'
url = '[string]/[string]'
p12 = OpenSSL::PKCS12.new(File.open('[path_to_folder_with_cert_files]/[cert_name].p12', "rb").read, "[password]")
key = p12.key
cert = p12.certificate
connection = Faraday::Connection.new host, :ssl => {
:client_cert => cert,
:client_key => key,
:ca_file => '[path_to_folder_with_cert_files]/[cert_name].crt',
:verify => false
}
puts response.status = connection.post do |req|
req.url(url)
req.headers['Content-Type'] = @headers["content_type"]
req.body = data
end
响应有403禁止。我已经测试过没有ssl连接数据,url,主机参数和状态是200 OK。
请帮助,因为我没有找到关于Ruby的Faraday和OpenSSL :: PKCS12的特定用法的教程/问题
答案 0 :(得分:0)
以下对我有用:
class Gateway
def call
connection.get do |req|
req.url(url)
req.headers['Content-Type'] = 'text/xml'
req.body = data
end
end
def connection
Faraday.new(ssl: ssl) do |builder|
builder.request :retry
builder.response(:logger) unless Rails.env.test?
builder.adapter :net_http
end
end
def ssl
{
client_key: client_key,
client_cert: client_cert,
ca_file: 'CA.crt' # optional
}
end
def url
'https://...'
end
def client_key
p12.key
end
def client_cert
p12.certificate
end
def p12
OpenSSL::PKCS12.new(p12_file.read, p12_password)
end
def p12_file
File.open('<path-to-p12-file>', 'rb')
end
def p12_password
'password' # if password protected
end
end
以及用法:
puts Gateway.new.call.body