美文网首页
ROR – HTTP基本身份验证

ROR – HTTP基本身份验证

作者: TorvardsDB | 来源:发表于2019-03-20 17:52 被阅读0次

    参考<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
    

    相关文章

      网友评论

          本文标题:ROR – HTTP基本身份验证

          本文链接:https://www.haomeiwen.com/subject/zmptvqtx.html