2

一旦您使用authenticate_or_request_with_http_basic.

例如,如果我对需要此身份验证的方法发出 curl 请求,一旦我插入错误的用户名和密码,它就会返回HTTP Basic: Access denied.

因此,在这种情况下,我希望能够使用特定的 XML 格式字符串自定义此消息(就像 twitter API 一样)。那可能吗?

提前致谢

4

1 回答 1

7

如果要自定义登录提示中的消息,只需将消息传递给方法调用即可。

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

如果您想自定义最终的错误消息,根据 Rails 2.3.4 源代码您可以只为 HTTP Digest 身份验证执行此操作。

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

基本身份验证将错误消息硬编码到方法中。

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
于 2009-11-17T08:16:28.227 回答