使用authenticate_or_request_with_http_basic()时收到AbstractController::DoubleRenderError
我有一个正在工作的控制器操作,当我在块的开头添加对 authenticate_or_request_with_http_basic
的调用时,该操作会中断。这是不起作用的操作代码:
def index
authenticate_or_request_with_http_basic do |username, password|
username == "test1" and password == "test"
end
render layout: false, content_type: 'application/xml'
end
我在浏览器窗口中收到“AbstractController::DoubleRenderError”错误响应,并显示以下消息:
在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,并且每个操作最多调用一次。另请注意,重定向和渲染都不会终止操作的执行,因此如果您想在重定向后退出操作,则需要执行“redirect_to(...) and return”之类的操作。
当我将“authenticate_or_request_with_http_basic”逻辑放在单独的操作中,然后配置 before_filter 来为索引操作运行它时,一切都很好。但是,我不会将这个逻辑重复用于除索引之外的任何操作,并且我想知道为什么上面的代码不起作用。
解决方案
在 Tyler 和 RKB 的帮助下,我找到了解决方案。如下:
authenticated = authenticate_or_request_with_http_basic "Authentication Required" do |username, password|
@user = User.find_by_username(username)
@user != nil && password == "test"
end
return unless authenticated == true
如果身份验证成功,authenticate_or_request_with_http_basic
返回“true”。失败时返回 401。
I have a working controller action that breaks when I add a call to authenticate_or_request_with_http_basic
at the beginning of the block. Here is the non-functioning action code:
def index
authenticate_or_request_with_http_basic do |username, password|
username == "test1" and password == "test"
end
render layout: false, content_type: 'application/xml'
end
I am receiving a "AbstractController::DoubleRenderError" error response in my browser window with the following message:
Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action. Also note that neither redirect nor render terminate execution of the action, so if you want to exit an action after redirecting, you need to do something like "redirect_to(...) and return".
When I place the "authenticate_or_request_with_http_basic" logic in a separate action and then configure a before_filter to run it for the index action, all is well. However, I'm not reusing this logic for any actions other than index, and I'd like to know why the above code doesn't work.
Solution
I was able to find a solution with the help of Tyler and RKB. Here it is:
authenticated = authenticate_or_request_with_http_basic "Authentication Required" do |username, password|
@user = User.find_by_username(username)
@user != nil && password == "test"
end
return unless authenticated == true
authenticate_or_request_with_http_basic
returns "true" if authentication was successful. It returns a 401 on failure.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
听起来像authenticate_or_request_with_http_basic是渲染或重定向。我猜当你将它移动到 before_filter 时它会起作用,因为它返回 false ,这会取消回调链并导致第二次渲染不会发生?只是一个猜测...
It sounds like authenticate_or_request_with_http_basic is either render or redirecting. I'm guessing it works when you move it to a before_filter because it returns false which cancels the callback chain and causes the 2nd render to not occur? Just a guess...
authenticate_or_request_with_http_basic 调用 render 来发送进行 HTTP 基本身份验证所需的 HTTP 响应(状态代码 401 未经授权)。有关详细信息,请参阅 Rails 代码 。
authenticate_or_request_with_http_basic calls render to send the HTTP response needed to do HTTP basic authentication (status code 401 Unauthorized). See the Rails code for the details.