我想在登台服务器上实现HTTP基本身份验证,但仅适用于本地网络之外的身份验证.我有一个Rails 3.1应用程序.在application.rb中,我有以下内容:
class ApplicationController << ActionController::Base http_basic_authenticate_with :realm => "Staging",:name => "user",:password => "password" if :need_authentication? private def need_authentication? Rails.env == "staging" && request.remote_addr !~ /^192.168.0.\d{1,3}$/ end end
这就是问题:即使是need_authentication?方法显式返回false,应用程序仍然要求我进行身份验证,就好像它完全忽略了最后的if子句一样.
那么,有没有办法只在某些条件下要求身份验证?
解决方法
在Rails 4中,:if条件有效.例如,
class ApplicationController < ApplicationController::Base http_basic_authenticate_with name: "user",password: "password" if Rails.env == 'staging' end
class ApplicationController < ApplicationController::Base http_basic_authenticate_with name: "user",password: "password",if: :need_authentication? private def need_authentication? Rails.env == 'staging' end end
原文地址:https://www.jb51.cc/ruby/267470.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。