Rails - authenticate_or_request_with_http_basic custom “access denied” message

佐手、 提交于 2019-12-06 09:17:02

If you want to customize the message in the login prompt, just pass the message to the method call.

authenticate_or_request_with_http_basic "My custom message" do |user_name, password|
  user_name == USER_NAME && password == PASSWORD
end

If you want to customize the final error message, according to Rails 2.3.4 source code you can do this only for the HTTP Digest authentication.

def authentication_request(controller, realm, message = nil)
  message ||= "HTTP Digest: Access denied.\n"
  authentication_header(controller, realm)
  controller.__send__ :render, :text => message, :status => :unauthorized
end

The Basic Authentication has the error message hard-coded into the method.

def authentication_request(controller, realm)
  controller.headers["WWW-Authenticate"] = %(Basic realm="#{realm.gsub(/"/, "")}")
  controller.__send__ :render, :text => "HTTP Basic: Access denied.\n", :status => :unauthorized
end
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!