Rails 仅在指定的控制器上强制 ssl

编程入门 行业动态 更新时间:2024-10-26 09:23:01
本文介绍了Rails 仅在指定的控制器上强制 ssl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 ssl 证书,用于 secure.mydomain.我只想在某些控制器上使用 ssl,而不是在我的整个应用程序中.我在网上查看过,似乎找不到关于如何为指定控制器启用 ssl 的完整而准确的教程.

I have a ssl certificate which serves secure.mydomain. I would like to use ssl only on certain controllers and not throughout my entire application. I have looked online and can't seem to find a complete and accurate tutorial on how to enable ssl for specified controllers.

我知道我可以使用下面的配置 ssl,但它对我没有用,因为它启用了 ssl 站点范围

I am aware that I can use the config ssl below but it does not serve me a purpose since it enables ssl site wide

config.force_ssl = true

如何告诉 rails 只在特定控制器上使用 ssl 而不是我的整个应用程序?

How can I tell rails to only use ssl on a specific controller rather then my entire application?

推荐答案

您可以使用 routes.rb 中的约束来做到这一点:

You can do this with constraints in routes.rb:

resource :account, :constraints => { :protocol => "https", :subdomain => "secure" }

另外,如果你有很多安全路由,并且你想把事情弄干,你可以为你的安全路由创建一个scope:

Also, if you have many secure routes, and you want to DRY things up, you can create a scope for your secure routes:

scope :constraints => { :protocol => "https", :subdomain => "secure" } do ...[secure routes]... end

更多推荐

Rails 仅在指定的控制器上强制 ssl

本文发布于:2023-07-15 01:59:42,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1108362.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:器上   Rails   ssl

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!