if auth.provided? && auth.basic? && auth.credentials && USERS[auth.credentials.first] == auth.credentials.last @app.call(env) else if uri == "/metrics" unauthorized_response else @app.call(env) end end end
defunauthorized_response [401, {"Content-Type" => "text/plain","WWW-Authenticate"=>"Basic realm='metrics'"}, ["Unauthorized\n"]] end end
引入中间件
在config/application.rb中做一下修改:
1 2 3 4 5 6 7 8 9 10 11
classApplication < Rails::Application #.... some more