Ruby on rails Rails:使用Authlogic的基本身份验证
我正在使用Authlogic,我想在我的控制器中实现基本的HTTP身份验证,以便定义需要身份验证的操作 我知道如何使用\u HTTP\u Basic before\u过滤器进行基本HTTP身份验证\u或\u请求\u,但我想在这里介绍如何使用Authlogic插件实现它Ruby on rails Rails:使用Authlogic的基本身份验证,ruby-on-rails,authentication,authlogic,Ruby On Rails,Authentication,Authlogic,我正在使用Authlogic,我想在我的控制器中实现基本的HTTP身份验证,以便定义需要身份验证的操作 我知道如何使用\u HTTP\u Basic before\u过滤器进行基本HTTP身份验证\u或\u请求\u,但我想在这里介绍如何使用Authlogic插件实现它 class ItemsController < ApplicationController before_filter :authenticate , :only => [:index, :create]
class ItemsController < ApplicationController
before_filter :authenticate , :only => [:index, :create]
...
end
class ItemsController[:index,:create]
...
结束
逐步解释如何在rails项目中使用authlogic
设置authlogic后,在应用程序控制器中定义以下有用的与身份验证相关的帮助器方法
def current_user_session
return @current_user_session if defined?(@current_user_session)
@current_user_session = UserSession.find
end
def current_user
return @current_user if defined?(@current_user)
@current_user = current_user_session && current_user_session.record
end
def require_user
unless current_user
store_location
flash[:notice] = "You must be logged in to access this page"
redirect_to new_user_session_url
return false
end
end
def require_no_user
if current_user
store_location
flash[:notice] = "You must be logged out to access this page"
redirect_to root_url
return false
end
end
定义这些方法后,可以指定要求用户登录的操作:
before_filter :require_user, :only => [:new, :edit]
我在以下方面取得了成功:
before_filter : require_http_auth_user
在application_controller.rb中定义筛选方法
def require_http_auth_user
authenticate_or_request_with_http_basic do |username, password|
if user = User.find_by_login(username)
user.valid_password?(password)
else
false
end
end
end
然后在控制器中,您可以执行以下操作:
before_filter : require_http_auth_user
然后,您可以使用:
(即http基本身份验证)
希望这能有所帮助。我这样做了,但这不是REST协议。我正在尝试创建一个基本HTTP身份验证,其中客户端必须发送身份验证头。此代码实际上不使用HTTP Basic,但下面的解决方案使用。这就是我们正在寻找的。谢谢你救了我一天!谢谢