ROR – HTTP基本身份验证

2019-03-20  本文已影响0人  TorvardsDB

参考<Rails 高级编程> Page100

module Backend
  class BaseController < ActionController::Base
    before_action :authenticate

    private

    def authenticate
      authenticate_or_request_with_http_basic do |username, password|
        username == "#{Rails.application.secrets[:name]}" && password == "#{Rails.application.secrets[:password]}"
      end
    end
  end
end

另外一种写法:

module Backend
  class BaseController < ActionController::Base
    http_basic_authenticate_with name: "#{Rails.application.secrets[:name]}", 
      password: "#{Rails.application.secrets[:password]}", if: :need_authentication?

    private

      def need_authentication?
        !Rails.env.development?
      end
  end
end
上一篇下一篇

猜你喜欢

热点阅读