簡體   English   中英

Rails 4.2使用secret_token嗎?

[英]Does Rails 4.2 use secret_token?

Rails 4.2中是否需要使用secret_key_base和secret_token? 設置都不會導致以下異常消息:

如果缺少“生產”環境的secret_tokensecret_key_base ,請在config/secrets.yml secret_key_base中設置這些值

4.2升級指南( http://railsapps.github.io/updating-rails.html )說明了這一點:

使用rails new命令創建新的Rails應用程序時,會生成一個唯一的密鑰並將其寫入config / initializers / secret_token.rb文件。

但是當我生成我的應用程序時沒有創建這樣的文件,並且config / secrets.yml中沒有對secret_token的引用

我假設錯誤消息是錯誤的,並且只需要secret_key_base。 當我在我的開發機器上運行我的應用程序時,它只從secret_key_base開始,但在Engineyard中,設置secret_key_base(通過環境變量)不起作用。 我仍然得到錯誤。

您在Engine Yard上看到的問題是因為默認情況下(還)存在secret_key_base環境變量。 這是我們正在努力的事情。 您可以使用自定義廚師自行安裝; 我建議與我們的支持團隊討論更多信息。

至於你得到的實際錯誤,我剛剛測試了一個全新的Rails 4.2應用程序(“rails new foo”),看看它是否正在生成secret_token.rb,但事實並非如此。 我認為你需要的是創建config / secrets.yml,該文件應如下所示:

development:
  secret_key_base: somekey

test:
  secret_key_base: someotherkey

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

現在,當你看到ENV [“SECRET_KEY_BASE”]時,這就是Engine Yard有點扭曲的地方 - 我們還沒有開箱即用。 只要你的repo是私有的,你就可以自己硬編碼。 否則,使用自定義廚師可以通過創建一個秘密密鑰庫並將其放入負責啟動應用程序工作進程的包裝器腳本(例如,在我們的平台上配置config / env.custom)來使您平倉。

希望這可以幫助。

4.2確實使用了密鑰,您發布的鏈接具有您正在尋找的解決方案。

在沒有以密鑰活動結束的環境中,您需要使用rake secret生成它,然后將控制台的輸出放入config/initializers/secret_token.rb文件中(如果沒有,您可以創建一個)一)。

您可以選擇避免使用secrets.yml 許多人更喜歡使用另一個寶石/程序(例如figaro )來處理秘密信息。 為了簡化您的生活,您可以將此信息放入secret_token.rb文件並繼續 - 或者您可以了解處理這種情況的各種其他慣用方法。

至少Rails 4.2.2給了我同樣的錯誤,但在rails用戶的.bash_profile文件中設置環境變量SECRET_KEY_BASE解決了我的問題,所以關於secret_token的位似乎是假的 - 可能是早期版本的保留。

通過命令rake secret生成rake secret ,然后使用文件.bash_profile生成的字符串,如下所示:

export SECRET_KEY_BASE='the_output_of_the_rake_secret_command'

我建議使用最新版本的Rails重新生成一個新的應用程序。

這個文件是我上一個項目中自動生成的:

# config/secrets.yml
# Be sure to restart your server when you modify this file.

# Your secret key is used for verifying the integrity of signed cookies.
# If you change this key, all old signed cookies will become invalid!

# Make sure the secret is at least 30 characters and all random,
# no regular words or you'll be exposed to dictionary attacks.
# You can use `rake secret` to generate a secure secret key.

# Make sure the secrets in this file are kept private
# if you're sharing your code publicly.

development:
  secret_key_base: fooooo

test:
  secret_key_base: fooooo

# Do not keep production secrets in the repository,
# instead read values from the environment.
production:
  secret_key_base: <%= ENV["SECRET_KEY_BASE"] %>

我還建議您通過railsdiff站點比較生成的文件(例如: httprailsdiff ),因為它聽起來像是從舊版本升級。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM