对rails中的特定控制器跳过/禁用force_ssl

Raj*_*Raj 13 ruby ssl ruby-on-rails ruby-on-rails-4

我需要在我的应用程序中的所有路由上强制使用SSL,除了message#new控制器.

config/environments/production.rb,我有:

config.force_ssl = true
Run Code Online (Sandbox Code Playgroud)

现在所有路由都重定向到https,现在我想为message#new控制器禁用它.

有谁知道如何在Rails 4+应用程序中禁用特定控制器的强制SSL?

kuc*_*hbe 20

根据以下文档应该工作(但仅适用于rails> 5版本):

config.ssl_options = {
  redirect: {
    exclude: -> request { request.path =~ /healthcheck/ }
  }
}
Run Code Online (Sandbox Code Playgroud)


Raj*_*Raj 5

skip_before_action :verify_authenticity_token
force_ssl except: [:index,:create]
Run Code Online (Sandbox Code Playgroud)

它对我有用.

  • 弃用警告:控制器级 `force_ssl` 已弃用,将从 Rails 6.1 中删除。请在您的环境配置中启用“config.force_ssl”,以使 ActionDispatch::SSL 中间件更全面地强制您的应用程序通过 HTTPS 进行通信。如果需要,您可以使用 config.ssl_options 来避免匹配端点被重定向到 HTTPS。 (3认同)