2010-09-03 59 views
28

我正在升级应用程序到Rails 3.0.0,想知道添加SSL的标准方法是否已经改变(我隐约记得演示表明路由器现在可以处理SSL,尽管我不确定它是否会仅用于演示目的)。我目前使用“ssl_requirement”gem,但它给出:Rails 3 SSL弃用

拒绝警告:使用#request_uri已弃用。改为使用完整路径。 (从ensure_proper_protocol在/Library/Ruby/Gems/1.8/gems/ssl_requirement-0.1.0/lib/ssl_requirement.rb:53称为)

此外,它似乎打破处理新的“数据的方法时'属性。例如:

<%= link_to "Logout", user_path, :method => :delete %> 

从所述应用的SSL部分存取时工作正常,但失败(试图使显示动作)从非SSL部,再后,当(在用户控制器的所有操作需要SSL,虽然我知道销毁行为不会传输安全数据)。

回答

46

它确实对Rails 3很简单在config/routes.rb

MyApplication::Application.routes.draw do 
    resources :sessions, :constraints => { :protocol => "https" } 
end 

或者,如果你需要强制SSL多重路线:

MyApplication::Application.routes.draw do 
    scope :constraints => { :protocol => "https" } do 
    # All your SSL routes. 
    end 
end 

并链接到SSL路线可以这样做:

<%= link_to "Logout", sessions_url(:protocol => 'https'), :method => :delete %> 

如果您希望自动重定向某些控制器(或实际上,一些子路径)为等效的基于HTTPS的URL,您可以添加这样的事情你的路由(我想这部分比较简单):

# Redirect /foos and anything starting with /foos/ to https. 
match "foos(/*path)", :to => redirect { |_, request| 
    "https://" + request.host_with_port + request.fullpath } 
+1

这似乎比使用'ssl_requirement'更复杂。它是在Rails 3中使用它的新标准方法,还是'ssl_requirement'仍然可用?谢谢。 – 2010-09-03 15:58:09

+1

@Kevin:除了自动重定向,我觉得这很容易。而且,这一切都可以通过* standard * routing DSL完成,这是Rails 2无法实现的,因此需要一个外部库。 – molf 2010-09-03 16:11:43

+4

在开发环境中,执行 scope:constraints => {:protocol => Rails.env.production? ? 'https':'http'}做 ... 结束 来源:http://www.themomorohoax.com/2010/10/08/using-ssl-in-rails-3 – mysmallidea 2011-04-27 03:22:53

20

花一个下午的时间寻找最佳的解决方案后,我的办法解决在这篇文章中描述:http://clearcove.ca/blog/2010/11/how-to-secure-a-rails-app-on-heroku-with-ssl-firesheep/其中引用这篇文章:Force SSL using ssl_requirement in Rails 2 app

基本上做到这一点:

# lib/middleware/force_ssl.rb 
class ForceSSL 
    def initialize(app) 
    @app = app 
    end 

    def call(env) 
    if env['HTTPS'] == 'on' || env['HTTP_X_FORWARDED_PROTO'] == 'https' 
     @app.call(env) 
    else 
     req = Rack::Request.new(env) 
     [301, { "Location" => req.url.gsub(/^http:/, "https:") }, []] 
    end 
    end 
end 

# config/application.rb 
config.autoload_paths += %W(#{ config.root }/lib/middleware) 

# config/environments/production.rb 
config.middleware.use "ForceSSL" 
+1

我更喜欢这种方法,因为即使在开发环境中,@ molf的解决方案也需要SSL。 – 2011-01-26 18:17:33

14

Toppic是旧的,但只是使用Google的人:

在*应用程序/控制器/ your_controller.rb *

class LostPasswordsController < ApplicationController 

    force_ssl 

    def index 
    #.... 
    end 
end 

如果应用控制器全球使用它

http://apidock.com/rails/ActionController/ForceSSL/ClassMethods/force_ssl

... THX S.L.对于尖

+0

谢谢你。结合“except”和“only”选项,这是选择性SSL的一个很好的解决方案。 – LouieGeetoo 2012-04-04 20:55:00

+1

导轨3.1只有 – 2012-05-26 03:08:03

+0

b.t.w. 'config.force_ssl'和控制器'force_ssl'之间存在差异http://www.eq8.eu/blogs/14-config-force_ssl-is-different-than-controller-force_ssl – equivalent8 2016-05-08 10:08:30

1

在以后的Rails(至少3.12+),您可以使用以下,环境特定的:

在配置/环境/ production.rb(或其他环境)

# Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. 
config.force_ssl = true