简体   繁体   English

Ruby on Rails SSL_connect错误

[英]Ruby on Rails SSL_connect error

I have the following controller- courses-controller.rb: 我有以下controller- courses-controller.rb:

class CoursesController < ApplicationController
  def index
    @search_term = params[:looking_for] || 'jhu'
    @courses = Coursera.for(@search_term)
  end
end

Following model - coursera.rb: 以下型号 - coursera.rb:

class Coursera
    include HTTParty

    base_uri 'https://api.coursera.org/api/catalog.v1/courses'
    default_params fields: "smallIcon,shortDescription", q: "search"
    format :json

    def self.for term
        get("", query: { query: term})["elements"]
    end
end

And following view- index.html.erb: 并在view- index.html.erb之后:

<h1>Searching for - <%= @search_term %></h1>

<table border="1">
    <tr>
        <th>Image</th>
        <th>Name</th>
        <th>Description</th>
    </tr>
    <% @courses.each do |course| %>
    <tr class=<%= cycle('even', 'odd') %>>
        <td><%= image_tag(course["smallIcon"])%></td>
        <td><%= course["name"] %></td>
        <td><%= course["shortDescription"] %></td>
    </tr>
    <% end %>
</table>

But, when I do http://localhost:3000/courses/index , I get the following error: 但是,当我执行http:// localhost:3000 / courses / index时 ,我收到以下错误:

SSL_connect returned=1 errno=0 state=SSLv3 read server certificate B: certificate verify failed Application Trace | SSL_connect返回= 1 errno = 0状态= SSLv3读取服务器证书B:证书验证失败应用程序跟踪| Framework Trace | 框架跟踪| Full Trace app/models/coursera.rb:9:in for' app/controllers/courses_controller.rb:4:in index' 完整跟踪app / models / coursera.rb:9: for' app/controllers/courses_controller.rb:4:in index'

Please tell me what to do. 请告诉我应该怎么做。 Nothing works! 什么都行不通!

It looks like its complaining that there isn't a valid certificate? 看起来它抱怨没有有效的证书? If thats the case you can tell HTTParty to ignore the invalid cert for now, like this: 如果是这种情况,您可以告诉HTTParty现在忽略无效的证书,如下所示:

class Coursera
    include HTTParty

    default_options.update(verify: false)

...

If that works, then you probably want to figure out why it thinks the cert is invalid. 如果可行,那么您可能想知道它认为证书无效的原因。

The answer posted by Mike above would work for you since you are ignoring invalid cert. 由于您忽略了无效证书,上面Mike发布的答案对您有用。

Well this might be a workaround to get the thing done. 那么这可能是一个解决方法来完成工作。 But it's not recommended as you cannot take this code to production. 但不建议这样做,因为您无法将此代码用于生产。

So it's better to ignore that just for the development env. 因此,最好忽略仅仅用于开发环境。

Just add the following code block to the end of your application.rb and you should be good to go. 只需将以下代码块添加到application.rb的末尾,就可以了。

if Rails.env.development?
  OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
end
 base_uri 'https://api.coursera.org/api/catalog.v1/courses' ... 

The certificate contains the following DNS names: 证书包含以下DNS名称:

$ openssl s_client -connect api.coursera.org:443 -tls1 -servername api.coursera.org | \
    openssl x509 -text -noout | grep DNS
...
    DNS:*.coursera.org, DNS:coursera.org

When you connect to https://localhost:3000/... , the certificate must include a DNS name of localhost . 当您连接到https://localhost:3000/... ,证书必须包含DNS名称localhost

Common Names (CN) don't matter. 通用名称(CN)无关紧要。 What matters is the Subject Alternate Names (SAN), and they must include all DNS names you user to contact the server. 重要的是主题备用名称(SAN),它们必须包含您用户与服务器联系的所有DNS名称。

Also see the following on creating well formed, SSL certificates: 另请参阅以下有关创建格式良好的SSL证书的信息:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM