简体   繁体   English

如何针对某些路径在Rails 4中有选择地启用SSL?

[英]How do I selectively enable SSL in Rails 4 for certain paths?

How do I turn SSL HTTPS off for a given path? 如何为给定路径关闭SSL HTTPS? I saw Enable SSL for certain resource actions but that was to enable HTTPS for a single path. 我看到为某些资源操作启用SSL,但这是为单个路径启用HTTPS。 My config has 我的配置有

config.force_ssl = true

However when I show a page that has an iframe and embeds an external source 但是,当我显示一个包含iframe的页面并嵌入外部源时

<iframe src='http://www.

Then it doesn't even appear (Chrome) because it is mixed content. 然后它甚至没有出现(Chrome),因为它是混合内容。 So I want to disable SSL for that single route that has an iframe. 所以我想为具有iframe的单一路由禁用SSL。 (I really want to turn it on & off depending on the content.) (我真的想根据内容打开和关闭它。)

I tried adding this to my routes.rb 我尝试将其添加到我的routes.rb

get 'frame', :constraints => { :protocol => 'http' }

but that gave an error http://127.0.0.1:3000/posts/136/frame 但是这给了一个错误http://127.0.0.1:3000/posts/136/frame

No route matches [GET] "/posts/136/frame"

even though the route shows up 即使路线出现了

frame_post GET  /posts/:id/frame(.:format)  posts#frame {:protocol=>"http"}

I also saw Force SSL for specific routes in Rails 3.1 and tried 我还看到了Rails 3.1中特定路由的强制SSL并尝试过

gem 'rack-ssl-enforcer' # Gemfile
config.middleware.use Rack::SslEnforcer, :except => [ /\/frame$/ ], :strict => true # application.rb

but it still always redirects to https. 但它仍然总是重定向到https。 I also tried 我也试过了

force_ssl :except => :frame # posts_controller.rb

But that didn't work either. 但这也不起作用。

I'm using Rails 4.1. 我正在使用Rails 4.1。

Rack ssl enforcer is a great solution for this - your regular expression is wrong. Rack ssl enforcer是一个很好的解决方案 - 你的正则表达式是错误的。 In your form, the regex would only exclude a path that is entirely '/frame', and would not exclude a path that had anything before /frame 在您的表单中,正则表达式只会排除完全为“/ frame”的路径,并且不会排除在/ frame之前有任何内容的路径

If you want to exclude any path that ends with /frame , The correct regular expression would be: 如果要排除 / frame 结尾的任何路径,正确的正则表达式将是:

\.*\/frame$/

Resulting in a config: 导致配置:

config.middleware.use Rack::SslEnforcer, :except => [ \.*\/frame$/ ], :strict => true 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM