[英]http_basic_authenticate_with not working. Why?
I just started learning ruby-on-rails by using this stuff: http://guides.rubyonrails.org/getting_started.html 我刚刚通过使用以下内容开始学习ruby-on-rails: http : //guides.rubyonrails.org/getting_started.html
And I'm stopped on this step: 我停止了这一步:
10 Security 10安全
class PostsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :except => [:index, :show]
# GET /posts
# GET /posts.json
def index
@posts = Post.all
respond_to do |format|
[...]
class CommentsController < ApplicationController
http_basic_authenticate_with :name => "dhh", :password => "secret", :only => :destroy
def create
@post = Post.find(params[:post_id])
[...]
And it didn't work. 而且它没有用。 I mean, there is no prompt for a password and login, and there is no error. 我的意思是,没有提示输入密码和登录的信息,也没有错误。 I just did that and nothing changed. 我只是这样做而没有任何改变。
Can someone help me with that or at least point me in the right direction to solve the problem? 有人可以帮我解决这个问题,或者至少可以指出正确的方向来解决问题吗?
It depends on the version of Rails you have. 这取决于您使用的Rails的版本。 If it's 3.2 it should work properly, as you can see at the top of the page it says: 如果是3.2,则应该可以正常工作,如您在页面顶部看到的那样:
This Guide is based on Rails 3.2. 本指南基于Rails 3.2。 Some of the code shown here will not work in earlier versions of Rails. 此处显示的某些代码在早期版本的Rails中不起作用。
So you might wanna check that first, then if your version is an earlier one then try this: 因此,您可能想先检查一下,然后如果您的版本是较早的版本,请尝试以下操作:
class ApplicationController < ActionController::Base
USER, PASSWORD = 'dhh', 'secret'
before_filter :authentication_check, :except => :index
#Any other method
private
def authentication_check
authenticate_or_request_with_http_basic do |user, password|
user == USER && password == PASSWORD
end
end
end
It should work properly. 它应该可以正常工作。 Then, last option, you might have given the proper credentials once and now the app remembers that it's you and allows you to do everything, so (just to be sure) try to change the user name in anything else. 然后,最后一个选择是,您可能曾经给过适当的凭据,现在该应用程序会记住它是您自己,并允许您执行所有操作,因此(请确保)尝试更改其他任何用户名。 You wanna edit the line to make it look like this: 您想要编辑该行使其看起来像这样:
http_basic_authenticate_with :name => "anotherName", :password => "secret", :except => [:index, :show]
Obviously this line of code (as stated begore) will work only on Rails 3.2 (I actually think it should work on the 3.1 too) or more. 显然,这行代码(如前所述)仅在Rails 3.2(我实际上认为它也应在3.1)上运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.