简体   繁体   English

http_basic_authenticate_with不起作用。 为什么?

[英]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.

相关问题 RoR:未定义的方法`http_basic_authenticate_with&#39; - RoR: undefined method `http_basic_authenticate_with' rails 4 多站点 http_basic_authenticate_with - rails 4 multisite http_basic_authenticate_with 在http_basic_authenticate_with之后删除会话 - Deleting a session after http_basic_authenticate_with http_basic_authenticate_with不显示身份验证提示并将错误输出到服务器日志。 为什么? - http_basic_authenticate_with not showing authentication prompt and outputting error to server log. Why? 如果rails中未提供身份验证,请避免在http_basic_authenticate_with上使用HTTP 500 - Avoid HTTP 500 on http_basic_authenticate_with if no auth provided in rails 基于条件变量和实例变量使用http_basic_authenticate_with - Using http_basic_authenticate_with based on a conditional and instance variable 在RSpec测试中跳过Rails http_basic_authenticate_with - Skip Rails http_basic_authenticate_with in RSpec test 刷出gem添加http_basic_authenticate_with发生错误 - Wash out gem add http_basic_authenticate_with occur an error 即时更改带有凭据的http_basic_authenticate_ - Change http_basic_authenticate_with credentials on fly, rails Rails 3 http_basic_authenticate_with仅在生产环境中? - Rails 3 http_basic_authenticate_with only in production environment?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM