[英]Conditionally Implementing HSTS, SSL, and Secure Cookies in Rails Based on Domain
我運行一個應用程序,從單個應用程序和服務器托管來自多個域的網站。 我正在將其中一些域名移至SSL,但其他域名則保留在http。 我正在運行Rails 4.x. 我相信我不能只使用
config.force_ssl = true
因為那將為所有域實現它,我不想要。
我知道在ApplicationController中我可以做類似的事情
force_ssl if: :ssl_required?
def ssl_required?
return [secure_domain1, domain2, domain3].include? request.domain
end
但據我了解,這並沒有實現HSTS或安全cookie。 我的兩個問題是:
如果沒有簡單的方法來啟用HSTS或安全cookie,並且讓這些值得麻煩,我可以隨時拆分我的應用並將其托管在兩個不同的服務器上,其中一個實例包含所有https域,另一個實例僅包含http域。
謝謝你的想法
使用Rails實現這一點實際上並不是一個壞主意,但正如評論所指出的那樣,使用NGINX會更好。 如果你真的想要追求Rails解決方案,你可以在同樣的def
做類似的事情:
force_ssl if: :ssl_required?
def ssl_required?
if [secure_domain1, domain2, domain3].include? request.domain
#HSTS for Rails <=4
response.headers['Strict-Transport-Security'] = 'max-age=315360000; includeSubdomains; preload'
#HSTS for Rails >=5
response.set_header('Strict-Transport-Security', 'max-age=315360000; includeSubdomains; preload')
cookies[:secure] = true
true
else
false
end
end
您始終可以將HSTS標頭調整為所需的max-age
或者使用更加慣用的方法來放置#{365.days.to_i}
而不是簡單的字符串標頭。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.