Rails仅限密码登录

use*_*511 5 authentication passwords ruby-on-rails

密码保护视图的最佳方法是什么?我已经在生成密码,但我不想输入用户名和密码,只需输入密码.我目前将密码存储为File类中的属性,并使用此:

before_filter :restrict, :only => :show    

authenticate_or_request_with_http_basic do |password|
  password == @file.password
end
Run Code Online (Sandbox Code Playgroud)

但是,它仍然会提示用户输入用户名,但由于缺少用户名而无法正确登录.有没有办法使用这种方法,只有提示只询问密码?如果不是这样做的最佳方式是什么?

Yur*_*akh 3

authenticate_or_request_with_http_basic 是使用 HTTP 基本身份验证实现的,需要输入用户名和密码的组合。如果您想启用仅密码身份验证,则必须编写自己的身份验证方法

before_filter :restrict, :only=>:show

def restrict(password)
  render :status=>401, :text=>"Unathorized" unless password == @file.password
end 
Run Code Online (Sandbox Code Playgroud)