2008-09-16 81 views

回答

29

在用於設置cookie的散列中設置'http_only'選項。

cookies["user_name"] = { :value => "david", :httponly => true }

,或者在軌道2:

例如

cookies["user_name"] = { :value => "david", :http_only => true }

+7

Heads up - in rails 3`:http_only`選項重命名爲`:httponly` – WTK 2012-01-25 09:06:56

6

只需按照changelog中所述將http_only設置爲true即可。

1

我還寫了一個包含在Rails 2.2中的補丁,該補丁默認CookieStore會話爲http_only。

不幸的是,會話cookie仍然是默認的常規cookie。

11

再勞裏的回答是:

注意,選項從:http_only:httponly(無下劃線)在某一時刻重新命名。

在actionpack 3.0.0,也就是Ruby on Rails 3中,所有對:http_only的引用都沒有了。

這讓我有一段時間。

2

如果你有一個名爲config/session_store.rb的文件,包括這一行(Rails 3+),那麼它已經自動設置了。 config/initializers/session_store.rb

# Be sure to restart your server when you modify this file. 
Rails.application.config.session_store :cookie_store, key: "_my_application_session" 

而且Rails允許你設置以下鍵:

:到期 - 在此cookie過期,作爲一個Time對象的時間。

:secure - 此cookie是否僅傳輸到HTTPS服務器。默認爲false。