【问题标题】:Rails 6 ActionMailer previews and http basic authenticationRails 6 ActionMailer 预览和 http 基本身份验证
【发布时间】:2020-03-31 01:49:36
【问题描述】:

我在 production.rb 中有以下 ActionMailer 配置:

  unless ENV['APP_NAME'] == 'application-production'
    config.action_mailer.preview_path = Rails.root.join('test/mailers/previews')
    config.action_mailer.show_previews = true
  end

问题是,非生产应用程序,例如临时应用程序,应该对整个域进行 http 身份验证,包括邮件预览路径“/rails/mailers”现在无需授权。缺少什么配置?

编辑:我尝试按照这个答案添加一个初始化程序:https://stackoverflow.com/a/44923157/2116456。我想在初始化程序中使用 ActionController::Base.http_basic_authenticate_with,但它不可用

【问题讨论】:

    标签: actionmailer ruby-on-rails-6


    【解决方案1】:

    好的,我找到了一种方法来做我需要的事情:

    我设置了一个初始化程序:'initializers/mailer_previews.rb',内容如下:

    if Rails.application.config.action_mailer.show_previews
      Rails::MailersController.prepend_before_action do
        head :forbidden unless authenticate_or_request_with_http_basic('Plz login') do |username, password|
          username == ENV['AUTH_NAME'] && password == ENV['AUTH_PASSWORD']
        end
      end
    end
    

    感谢@prcu 为我指明了正确的方向 (see this post)

    【讨论】:

      猜你喜欢
      • 2013-09-01
      • 2015-09-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-10-06
      • 2015-10-01
      • 2014-11-06
      相关资源
      最近更新 更多