如何在 Sinatra 中排除需要基本身份验证的路径

发布于 2024-08-17 15:26:23 字数 130 浏览 2 评论 0原文

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

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

I'm writing a smallish web service in Ruby using Sinatra. Access to pretty much everything is controlled using http basic auth (over https in production).

There is one particular directory that I want to exclude from requiring authorization. Is there an easy way to do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(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!" }
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!" }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文