https アクセス(自己署名証明書) ; Rails

継続遷移には材料不足ではあるが、ものを揃えるだけならば

  1. thin で SSL 通信用のサーバーを立てる

  2. SSL 通信をさせたいコントローラ(もしくはアクション)に ssl_force を噛ませる

前提

http://mat5ukawa.hateblo.jp/entry/2014/02/28/005347

自己署名証明書を作成していること


thin

install gem

$ gem install thin

rails s -p 3000 の代わりに次を叩く

Terminal 甲
$ thin start -p 3000
Terminal 乙
$ thin start -p 3001 --ssl --ssl-verify --ssl-key-file ~/.ssl/server.key --ssl-cert-file ~/.ssl/server.crt 

ssl_force

準備

モデル設計に意味は無し

$ rails g controller top index
$ rails g Book title:string
$ rake db:migrate

app/controller/top_controller.rb

class TopController < ApplicationController
  def index
  end
end

app/views/top/index.html.erb

<%= link_to 'Go to books library', controller: :books, action: :index, only_path: false, protocol: 'https://' %>

app/controller/book_controller.rb

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]
  force_ssl if: :ssl_configured?

  ...
  private

  def ssl_configured?
    Rails.env.development? # valid in development. If deploy, change to Rails.env.production?
  end
end

http://localhost:3000/

f:id:mat5ukawa:20140228015352p:plain

http://localhost:3000/books

f:id:mat5ukawa:20140228015447p:plain

https://localhost:3001/books 安全性確認

f:id:mat5ukawa:20140228015512p:plain

https://localhost:3001/books 例外追加後

f:id:mat5ukawa:20140228015604p:plain


情報元 # thanks

Stuff I've Learned About Ruby on Rails: Running a rails server to support SSL