我有一个需要在 SSL 下运行的 Rails 应用程序。我尝试了 ssl_requirement,但似乎我必须在每个 Controller 中输入所有操作。
有什么方法可以在带有 ssl_requirement 的应用程序 Controller 中添加 before_filter,以便当用户请求为 http 时,应用程序将自动重定向到 https?
谢谢大家。 :)
最佳答案
使用 Rack Middleware .
# lib/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/environment.rb
config.middleware.use "ForceSSL"
关于ruby-on-rails - 在 Rails 2 应用程序中使用 ssl_requirement 强制使用 SSL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3861772/