Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 对单个路由使用HTTP摘要身份验证_Ruby_Sinatra_Rack_Digest Authentication_Http Digest - Fatal编程技术网

Ruby 对单个路由使用HTTP摘要身份验证

Ruby 对单个路由使用HTTP摘要身份验证,ruby,sinatra,rack,digest-authentication,http-digest,Ruby,Sinatra,Rack,Digest Authentication,Http Digest,我想在我的模块化Sinatra应用程序中对特定路由使用HTTP摘要身份验证 中列出的示例简单介绍了如何为整个应用程序启用摘要身份验证。为使此功能适用于特定路由而提出的解决方案是创建两个单独的应用程序(一个具有摘要身份验证,另一个不具有摘要身份验证),将受保护和未受保护的路由放置在各自的应用程序中 这需要如下内容: require 'sinatra/base' class Protected < Sinatra::Base use Rack::Auth::Basic, "Protect

我想在我的模块化Sinatra应用程序中对特定路由使用HTTP摘要身份验证

中列出的示例简单介绍了如何为整个应用程序启用摘要身份验证。为使此功能适用于特定路由而提出的解决方案是创建两个单独的应用程序(一个具有摘要身份验证,另一个不具有摘要身份验证),将受保护和未受保护的路由放置在各自的应用程序中

这需要如下内容:

require 'sinatra/base'

class Protected < Sinatra::Base
  use Rack::Auth::Basic, "Protected Area" do |username, password|
    username == 'foo' && password == 'bar'
  end

  get '/' do
    "secret"
  end
end

class Public < Sinatra::Base
  get '/' do
    "public"
  end
end

有人想过如何做到这一点吗?

使用before。同一路线是否需要不同的行为?

这不能使用sinatra appOh Shot Miss上的中间件来完成吗?完全没有注意到您已经在做了
def authorized?
  @auth ||=  Rack::Auth::Basic::Request.new(request.env)
  @auth.provided? && 
    @auth.basic? && 
    @auth.credentials &&
    @auth.credentials == ['admin', 'admin']
end