【问题标题】:How do I exclude a path from requiring basic auth in Sinatra如何在 Sinatra 中排除需要基本身份验证的路径
【发布时间】:2010-04-09 00:23:55
【问题描述】:

我正在使用 Sinatra 在 Ruby 中编写一个小型 Web 服务。使用 http 基本身份验证(在生产中通过 https)控制对几乎所有内容的访问。

有一个特定的目录我想排除在需要授权的情况下。有没有简单的方法可以做到这一点?

【问题讨论】:

    标签: sinatra basic-authentication


    【解决方案1】:
    require 'sinatra'
    
    helpers do
      def protected!
        unless authorized?
          response['WWW-Authenticate'] = %(Basic realm="Testing HTTP Auth")
          throw(:halt, [401, "Not authorized\n"])
        end
      end
    
      def authorized?
        @auth ||=  Rack::Auth::Basic::Request.new(request.env)
        @auth.provided? && @auth.basic? && @auth.credentials && @auth.credentials == ['admin', 'admin']
      end
    end
    
    before { protected! unless request.path_info == "/public" }
    
    get('/public') { "I'm public!" }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-10
      • 1970-01-01
      • 2020-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多