Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 基于JSON的API的管理员认证_Ruby_Json_Ruby On Rails 3_Rest - Fatal编程技术网

Ruby 基于JSON的API的管理员认证

Ruby 基于JSON的API的管理员认证,ruby,json,ruby-on-rails-3,rest,Ruby,Json,Ruby On Rails 3,Rest,我正在尝试使用warden对基于JSON的API(web服务)进行身份验证。我已经在config/initializers中将我的密码策略定义为password\u strategy.rb,它具有身份验证在其中定义的方法。当我调用此身份验证时方法来自我的会话控制器的创建方法,参数在验证中以某种方式变为空,即使我可以在会话控制器的create方法中看到我的参数。我不知道幕后发生了什么神奇的事情 由于我的参数为空,我的身份验证被检测为未经授权,并且我被重定向到会话新方法以再次登录,即使我的登录凭据是

我正在尝试使用warden对基于JSON的API(web服务)进行身份验证。我已经在
config/initializers
中将我的密码策略定义为
password\u strategy.rb
,它具有
身份验证在其中定义的方法。当我调用此
身份验证时方法来自我的会话控制器的创建方法,参数在
验证中以某种方式变为空,即使我可以在会话控制器的create方法中看到我的参数。我不知道幕后发生了什么神奇的事情

由于我的参数为空,我的身份验证被检测为未经授权,并且我被重定向到会话新方法以再次登录,即使我的登录凭据是正确的

这是我的密码:

我的POST请求URL:localhost:3000/login.json 参数:

{
  "emailID":"sangam.gupta@mobiloitte.com", 
  "encrypted_password":"XXXXXX"
}

# Sessions controller (sessions_controller.rb)
class SessionsController < ApplicationController
  skip_before_filter :authenticate!

  def new
    render :json => { :responseCode => "401",:responseMessage => "The email id or password you entered is incorrect. Please try again."}
  end

  def create
    authenticate!
    render :json => { :responseCode => "200",:responseMessage => "You have successfully logged in"}
  end
end

# Application Controller (application_controller.rb)
require 'globals_module.rb'

class ApplicationController < ActionController::Base
  include GlobalsModule

  prepend_before_filter :authenticate!

  helper_method :warden, :signed_in?, :current_user

  around_filter :wrap_in_transaction

  def wrap_in_transaction
    ActiveRecord::Base.transaction do
      yield
    end
  end

  def signed_in?
    !current_user.nil?
  end

  def current_user
    warden.user
  end

  def warden
    request.env['warden']
  end

  def authenticate!
    Rails.logger.info params[:emailID] # email ID is present
    Rails.logger.info params[:encrypted_password] #encrypted password is present
    warden.authenticate!
  end
end

# Password Strategy (password_strategy.rb in config/initializer)
class PasswordStrategy < Warden::Strategies::Base
  def authenticate!
    Rails.logger.info params # params are empty, although present before calling authenticate! method
    Rails.logger.info request.session["emailID"] # It's are empty either
    user = User.find_by_emailID(params[:emailID])
    # my authentication code goes here
  end 
end

Warden::Strategies.add(:password, PasswordStrategy)
{
“emailID”:“sangam。gupta@mobiloitte.com", 
“加密密码”:“XXXXXX”
}
#会话控制器(Sessions\u controller.rb)
类sessioncontroller{:responseCode=>“401”,:responseMessage=>“您输入的电子邮件id或密码不正确。请重试。”}
结束
def创建
证明…是真实的
render:json=>{:responseCode=>“200”,:responseMessage=>“您已成功登录”}
结束
结束
#应用程序控制器(Application_Controller.rb)
需要'globals_module.rb'
类ApplicationController

任何帮助都将不胜感激。提前感谢。

您需要显式调用您的
密码策略
,方法是传递您在
Warden::Strategies中定义的命名符号。添加
,作为
验证
的第一个参数,如下所示:

warden.authenticate!(:password)
或者在配置Warden::Manager(例如在config/application.rb中)时,将您的策略添加为默认策略:

有关完整的API,请参阅


另请阅读,其中详细解释了典狱长的各个部分是如何组合在一起的。

典狱长正在将所有内容写入会话中。没有任何会话不可知的方法吗?

您的策略真的被调用了吗?
config.middleware.insert_after(ActionDispatch::Flash, Warden::Manager) do |manager|
    manager.default_strategies :password
end