首页 > 解决方案 > Authenticate_with_http_basic 块未执行

问题描述

我在我的 Rails 5.2 应用程序中观察到一些奇怪的行为,其中基本身份验证部分被跳过并且总是错误的。

此代码在本地开发环境下运行得非常好,但authenticate_with_http_basic在 kubernetes 部署下永远不会遇到块。

Kubernetes 应用程序部署由 nginx 代理前置

     class ApplicationController < ActionController::Base
       before_action :authenticate

       def authenticate
         if _valid_credentials?
           # never gets to this part
           true
         else
           # enters the else block
           request_http_basic_authentication
        end
      end

      def _valid_credentials?
        Rails.logger.debug("Function is entered")

        authenticate_with_http_basic do |username, password|
          cred = "#{username}|#{password}"
          # Nothing is printed with Rails.logger.debug
          Rails.logger.debug("Received credentials: #{cred}")
          # Running rails console reveals the correct ::Configuration.credentials
          SecureCompare.compare(cred, ::Configuration.credentials)
        end
      end
    end

Kubernetes 日志:


nginx xxx.xxx.xx.xx 0.004 0.002930 - test_user [18/Jun/2019:20:11:26 +0000]  "GET / HTTP/1.1" 401 38 "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3829.0 Safari/537.36" b9af3f83-b2b8-43e1-bee7-1603f5ada6c2 -
app-deployment-name Processing by XXXController#index as HTML
app-deployment-name Function is entered
app-deployment-name Filter chain halted as :authenticate rendered or redirected
app-deployment-name Completed 401 Unauthorized in 0ms (ActiveRecord: 0.0ms)

欢迎任何想法或建议。

谢谢。

标签: rubykubernetesruby-on-rails-5

解决方案


推荐阅读