Rails 3.1 强制常规 HTTP

发布于 2024-12-05 13:24:05 字数 488 浏览 1 评论 0原文

之前,我一直在使用 ssl_requirement 来对哪些页面通过 ssl 提供服务以及哪些页面通过 ssl 进行精细控制。通过纯 http 提供服务。

根据 ssl_requirement 自己的 wiki,它已被 Rails 3.1 的 强制 SSL。然而,情况似乎并非如此。 Force SSL 似乎没有提供相反方向的选项,没有办法强制页面通过常规 http 发送。

强制页面以纯 http 显示的正确 Rails 3.1 方法是什么? Force SSL 真的能取代 ssl_requirement 吗?

Previously, I had been using ssl_requirement to give us fine grained control over which pages were served over ssl and which were served over plain http.

According to the ssl_requirement's own wiki, it has been superseded by rails 3.1's Force SSL. However this does not seem to be the case. Force SSL doesn't seem to expose an option to go in the opposite direction, there is no way to force a page to sent via regular http.

What is the correct Rails 3.1 way to force a page to be displayed in plain http? Does Force SSL truly supersede ssl_requirement?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

回忆凄美了谁 2024-12-12 13:24:05

正义所说。有些人强烈推荐使用 SSL 浏览所有内容。现在,监听非 SSL 会话已经变得轻而易举,因此您应该竭尽全力为想要使用它的人提供便利。

然而。

使用 before_filter 应该很容易完成:

class ApplicationController < ActionController::Base
  before_filter do
    if request.ssl? && Rails.env.production?
      redirect_to :protocol => 'http://', :status => :moved_permanently
    end
  end
end

What Justice said. Some people feel strongly about browsing with SSL for everything. It's now trivial to snoop non-SSL sessions, so you should go out of your way to accomodate people who want to use it.

However.

It should be fairly easy to accomplish using a before_filter:

class ApplicationController < ActionController::Base
  before_filter do
    if request.ssl? && Rails.env.production?
      redirect_to :protocol => 'http://', :status => :moved_permanently
    end
  end
end
美羊羊 2024-12-12 13:24:05

Force SSL 的代码非常容易阅读。

https://github.com/rails/rails /blob/master/actionpack/lib/action_controller/metal/force_ssl.rb

它似乎没有做相反的事情并强制使用http。它提供了 only 和 except 选项来控制哪些操作和控制器需要 SSL,但没有提供强制使用 HTTP 而不是 https 的方法。

The code for Force SSL is pretty easy to read.

https://github.com/rails/rails/blob/master/actionpack/lib/action_controller/metal/force_ssl.rb

It doesn't seem to do the reverse and force http to be used. It provides the only and except options to control which actions and controllers SSL is to be required for, but doesn't provide a way to force HTTP to be used instead of https.

晌融 2024-12-12 13:24:05

要以与 force_ssl 完全相同的方式使用 force_non_ssl,请检查此 Rails 问题:https://gist.github.com/joost/6989118

接受以下选项:

force_non_ssl only: :show
force_non_ssl except: :show, notice: "Hi this is now insecure :)"

To use force_non_ssl the exact same way as force_ssl check this Rails Concern: https://gist.github.com/joost/6989118

Accepts options like:

force_non_ssl only: :show
force_non_ssl except: :show, notice: "Hi this is now insecure :)"
眼藏柔 2024-12-12 13:24:05

我将 joost 的代码简化为我需要的一切。谢谢乔斯特

if request.ssl?
  options = {
    :protocol => 'http://',
    :host     => request.host,
    :path     => request.fullpath,
    :status   => :moved_permanently
  }
  non_secure_url = ActionDispatch::Http::URL.url_for(options.slice(*URL_OPTIONS))
  redirect_to non_secure_url, options.slice(*REDIRECT_OPTIONS)
end

I simplified joost's code to all I needed. Thanks joost

if request.ssl?
  options = {
    :protocol => 'http://',
    :host     => request.host,
    :path     => request.fullpath,
    :status   => :moved_permanently
  }
  non_secure_url = ActionDispatch::Http::URL.url_for(options.slice(*URL_OPTIONS))
  redirect_to non_secure_url, options.slice(*REDIRECT_OPTIONS)
end
天邊彩虹 2024-12-12 13:24:05

对于那些像我一样只需要在本地计算机上短时间禁用整个应用程序的 force_ssl 行为的人,您可以简单地在您的 ApplicationController 中执行此操作:

def self.force_ssl *a
  warn "force_ssl disabled globally"
end

只需确保不要将其提交到您的代码库。

For those who just need to disable force_ssl behaviour on their whole app for a short time on local machine like me, you can simply do this in your ApplicationController:

def self.force_ssl *a
  warn "force_ssl disabled globally"
end

Just make sure not to commit it to your codebase.

-柠檬树下少年和吉他 2024-12-12 13:24:05

为什么您想要强制使用 HTTP 而不是 HTTPS?

我们中的许多人到处都使用 SSL 进行浏览。请不要仅仅因为您不喜欢帮助我们解决自身安全问题而将我们其他人置于危险之中。

对于我们大多数人来说,安全性很重要,即使我们大多数人不了解其重要性或不知道如何获得它。对于我们中的一些人来说,安全是生死攸关的。

某些页面必须通过 SSL 提供服务。尽管在我看来,如果您网站的任何部分需要通过 SSL 提供服务,那么整个网站都需要它(MITM 可以将 SSL 页面的链接更改为它在非 SSL 页面上呈现以指向 MITM 控制的非 SSL 代理)。 从来没有任何页面需要使用 SSL 来提供服务。

Why would you ever want to force HTTP over HTTPS?

A lot of us out here browse with SSL everywhere. Please don't put the rest of us at risk simply because you don't like helping us out with our own security.

For most of us, security is important, even if most of us don't understand its importance or know how to obtain it. For some of us, security is life and death critical.

Some pages must be served over SSL. Although, in my view, if any part of your site requires being served over SSL, then the entire site requires it (a MITM can change the link to the SSL page as it is rendered on the non-SSL page to point to a non-SSL proxy that the MITM controls). No page ever requires being served without SSL.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文