Rails-authenticate_or_request_with_http_basic自定义“拒绝访问"信息 [英] Rails - authenticate_or_request_with_http_basic custom "access denied" message

查看:137
本文介绍了Rails-authenticate_or_request_with_http_basic自定义“拒绝访问"信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当您使用authenticate_or_request_with_http_basic 将无效的凭据作为username:password插入轨道时,我正努力尝试更改默认消息.

I'm struggling myself trying to change that default message once you insert invalid credentials as username:password on rails using authenticate_or_request_with_http_basic.

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

For example if I make a curl request on a method that need this authentication, once I insert the wrong username and password it returns HTTP Basic: Access denied.

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

So, in this case I would like to be able to customize this message with a specific XML formatted string, (just like twitter API does). Is that possible?

预先感谢

推荐答案

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

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

如果要自定义最终错误消息,则根据Rails 2.3.4源代码,只能对HTTP Digest身份验证执行此操作.

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

这篇关于Rails-authenticate_or_request_with_http_basic自定义“拒绝访问"信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆