Rails Devise gem - >无需用户登录即可验证Web服务调用

时间:2016-06-22 18:37:02

标签: ruby-on-rails web-services authentication devise rubygems

我的rails应用程序是用于用户登录/身份验证的Devise gem。我需要使用webservice调用来检索数据以进行操作。有人可以建议如何在不使用前端登录的情况下完成此操作吗? 我需要一些方法来验证Web服务调用

溶液 - > 我最终使用了 https://github.com/gonzalo-bulnes/simple_token_authentication

另一种选择是 https://github.com/baschtl/devise-token_authenticatable

2 个答案:

答案 0 :(得分:0)

之前,设计使用包含称为令牌身份验证的东西,但它是继承不安全的并且已被删除。删除的代码和更好的替代方案由一名设计团队成员在gist中发布,之后由其他人写入gem devise-token_authenticatable

简单地说,您将在用户中存储身份验证令牌,并在每个请求上询问他们的电子邮件和令牌以查看它们是否匹配,但要安全地进行比较。通常不会将用户存储在此会话的会话中。

答案 1 :(得分:0)

您可以创建自己的会话创建方法并覆盖设计附带的方法。首先在您的路由器中,您将自定义设计

devise_for :users, controllers: {sessions: "sessions"}

然后,您可以使用自己的控制器实现它,并继承自Devise :: SessionsController

class SessionsController < Devise::SessionsController

  def new
    super
  end

  def create
    self.resource = AuthService.perform(auth_options) # do the custom check here with your service call
    sign_in(resource_name, resource) #this will set the devise session and login the user
    respond_with resource, :location => after_sign_in_path_for(resource)
  end

end

这应该为您提供了一种创建会话的自定义逻辑的方法。另请参阅github实现:

https://github.com/plataformatec/devise/blob/master/app/controllers/devise/sessions_controller.rb

希望有所帮助