我希望我的http_basic_authenticate_with能够在admin子域上工作。 我尝试像这样宣布它
http_basic_authenticate_with :name => ENV["WEBSITE_USERNAME"],
:password => ENV["WEBSITE_PASSWORD"],
:subdomain => 'admin',
:only => ['edit', 'destroy', 'new', 'index']
甚至尝试过
http_basic_authenticate_with :name => ENV["WEBSITE_USERNAME"],
:password => ENV["WEBSITE_PASSWORD"],
:only => ['edit', 'destroy', 'new', 'index'] if request.subdomain == "admin"
但请求变量在那时不存在。
首次访问管理员子域时,是否有人知道如何获取身份验证?
答案 0 :(得分:1)
authenticate_or_request_with_http_basic
更多信息Here
我的结束代码是
before_action :index, :authenticate #this can be a filter as well should you need multiple actions
def authenticate
if request.subdomain == 'admin'
authenticate_or_request_with_http_basic("Administration") do |user,pass| user == ENV["WEBSITE_USERNAME"] && pass = ENV["WEBSITE_PASSWORD"] end
end
end
答案 1 :(得分:0)
在较新版本的ruby&rails中,您可以使用lambda做到这一点
http_basic_authenticate_with name: ENV["HTTP_BASIC_AUTH_USER"], password: ENV["HTTP_BASIC_AUTH_PASSWORD"], if: -> { request.subdomain == "admin" }