Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/64.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails-使用“基本自定义”对“U或“U请求”进行身份验证;“拒绝访问”;消息_Ruby On Rails_Ruby_Basic Authentication_Access Denied - Fatal编程技术网

Ruby on rails Rails-使用“基本自定义”对“U或“U请求”进行身份验证;“拒绝访问”;消息

Ruby on rails Rails-使用“基本自定义”对“U或“U请求”进行身份验证;“拒绝访问”;消息,ruby-on-rails,ruby,basic-authentication,access-denied,Ruby On Rails,Ruby,Basic Authentication,Access Denied,当您使用身份验证\u或\u请求\u与\u http\u basic在rails上插入无效凭据作为用户名:密码时,我正在努力尝试更改默认消息 例如,如果我对需要此身份验证的方法发出curl请求,一旦插入错误的用户名和密码,它将返回httpbasic:Access denied. 因此,在本例中,我希望能够使用特定的XML格式字符串自定义此消息(就像twitter API一样)。可能吗 提前感谢如果要在登录提示中自定义消息,只需将消息传递给方法调用即可 authenticate_or_request

当您使用
身份验证\u或\u请求\u与\u http\u basic
在rails上插入无效凭据作为用户名:密码时,我正在努力尝试更改默认消息

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

因此,在本例中,我希望能够使用特定的XML格式字符串自定义此消息(就像twitter API一样)。可能吗


提前感谢

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

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摘要身份验证进行自定义

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

谢谢weppos,我以前也检查过,但在本例中,我使用BCrypt作为密码加密,所以我不确定是否可以使用Http Digest Auth,另一件事是,有没有办法从Http basic重写此函数?我尝试了,但没有成功,因为这有点奇怪,因为Twitter API似乎也使用Http Basic,并且他们能够配置最终的错误消息。是的,通过monkey补丁,你可以覆盖几乎所有内容。。。但这并不总是一个好主意。