Recaptcha gem 错误“未指定站点密钥。”

bma*_*ets 4 ruby-on-rails recaptcha ruby-on-rails-5 invisible-recaptcha

我正在尝试按照文档中的描述在我的 Rails 5 应用程序中设置 Recaptcha,但失败了。

我使用这个 gem:recaptcha (4.6.6)ruby 2.5.0并​​且rails 5.1.4

在视图形式中:

<%= flash[:recaptcha_error] %>
<%= recaptcha_tags %>
Run Code Online (Sandbox Code Playgroud)

在设备注册控制器中:

  prepend_before_action :check_captcha, only: :create

  private

  def check_captcha
    unless verify_recaptcha
      self.resource = resource_class.new sign_up_params
      resource.validate # Look for any other validation errors besides Recaptcha
      respond_with_navigational(resource) { redirect_to new_user_registration_path }
    end 
  end
Run Code Online (Sandbox Code Playgroud)

在我的初始化程序/recaptcha.rb 中

Recaptcha.configure do |config|
  config.site_key   = Rails.application.config_for(:recaptcha)['site_key']
  config.secret_key = Rails.application.config_for(:recaptcha)['secret_key']
end
Run Code Online (Sandbox Code Playgroud)

在我的 recaptcha.yml 中:

default: &default
  site_key: <%= ENV["RECAPTCHA_SITE_KEY"] %>
  secret_key: <%= ENV["RECAPTCHA_SECRET_KEY"] %>

development:
  <<: *default

test:
  <<: *default

staging:
  <<: *default

production:
  <<: *default
Run Code Online (Sandbox Code Playgroud)

在 /etc/environments 中:

# RECAPTCHA
RECAPTCHA_SITE_KEY=6Lfg3ksUAAAAABOD_OXCtPO60*******
RECAPTCHA_SECRET_KEY=6Lfg3ksUAAAAAOmFGdAxdo8*******
Run Code Online (Sandbox Code Playgroud)

问题

将 ENV 变量添加到 后/etc/environments,我使用以下命令将其导出:

for line in $( cat /etc/environment ) ; do export $line ; done
Run Code Online (Sandbox Code Playgroud)

然后我检查 Recaptcha 模块是否配置正确:

/home/deploy/apps/app_name/current$ bundle exec rails c
Loading staging environment (Rails 5.1.4) 
2.5.0 :001 > Recaptcha::Configuration.new
 => #<Recaptcha::Configuration:0x0000000006601908 @skip_verify_env=["test", "cucumber"], @handle_timeouts_gracefully=true, @secret_key="6Lfg3ksUAAAAAOmFGdAxdo8H*************", @site_key="6Lfg3ksUAAAAABOD_OXCtPO*************"> 
2.5.0 :002 > Recaptcha::Configuration.new.site_key!
 => "6Lfg3ksUAAAAABOD_OXCtPO*************" 
Run Code Online (Sandbox Code Playgroud)

另外,当我运行printenv命令时,我看到这些 ENV 变量(所以它确实已加载)

之后我重新启动rails并出现错误

未指定站点密钥。

/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/recaptcha-4.6.6/lib/recaptcha/configuration.rb:47:in `site_key!'
/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/recaptcha-4.6.6/lib/recaptcha/client_helper.rb:79:in `recaptcha_components'
/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/recaptcha-4.6.6/lib/recaptcha/client_helper.rb:15:in `recaptcha_tags'
/home/deploy/apps/app_name/releases/20180310222304/app/views/users/registrations/new.html.erb:27:in `block in _app_views_users_registrations_new_html_erb___216558772140569572_69973306795360'
/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/actionview-5.1.4/lib/action_view/helpers/capture_helper.rb:39:in `block in capture'
/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/actionview-5.1.4/lib/action_view/helpers/capture_helper.rb:203:in `with_output_buffer'
/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/actionview-5.1.4/lib/action_view/helpers/capture_helper.rb:39:in `capture'
/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/actionview-5.1.4/lib/action_view/helpers/form_helper.rb:450:in `form_for'
/home/deploy/apps/app_name/releases/20180310222304/app/views/users/registrations/new.html.erb:21:in `_app_views_users_registrations_new_html_erb___216558772140569572_69973306795360'
/home/deploy/apps/app_name/shared/bundle/ruby/2.5.0/gems/actionview-5.1.4/lib/action_view/template.rb:157:in `block in render'
Run Code Online (Sandbox Code Playgroud)

小智 8

我在这里发帖是为了防止有人正在寻找 Rails 5.2 解决方案来设置 Recaptcha 密钥。该解决方案利用新的 config/master.key 和 config/credentials.yml.enc 加密文件。

  1. 通过在本地终端中编辑文件,将 Recaptcha 密钥添加到credentials.yml.enc 文件中:

    EDITOR="vim" rails credentials:edit

  2. 将密钥添加到凭据文件后(请参阅下面的示例),保存并退出该文件。退出后,credentials.yml.enc 文件将自动加密。master.key 对于您的应用程序解密是必需的。加密前:

    recaptcha_site_key: 6Lc6BAAAAAAAAChqRbQZcn_yyyyyyyyyyyyyyyyy recaptcha_secret_key: 6Lc6BAAAAAAAAKN3DRm6VA_xxxxxxxxxxxxxxxx


3. 在 Rails 应用程序中创建一个名为 config/recaptcha.rb 的文件,并向其中添加以下代码:
Recaptcha.configure do |config| config.site_key = Rails.application.credentials.dig(:recaptcha_site_key) config.secret_key = Rails.application.credentials.dig(:recaptcha_secret_key) end

该解决方案可以在本地运行,也可以在生产中的 Ubuntu/nginx 上运行。您不需要 gem 或环境变量即可使其工作。如果master.key无法解密,您可能需要删除credentials.yml.enc文件,甚至可能需要删除master.key文件,然后在本地重复此过程(EDITOR =“vim”rails凭证:编辑等)在将新的 master.key 复制到生产并重新部署之前。

  • 很好的解决方案,一个调整: ```config/recaptcha.rb``` --&gt; ```config/initializers/recaptcha.rb``` (2认同)