【问题标题】:Getting AuthLogic to work with ActionCable让 AuthLogic 与 ActionCable 一起工作
【发布时间】:2016-10-06 21:24:38
【问题描述】:

我正在开发一个新的 Rails 5 (RC1) 应用程序。我使用 AuthLogic 进行用户身份验证,它一如既往地运行良好,直到我使用 ActionCable。

#app/channels/application_cable/connection.rb
module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user

    def connect
      self.current_user = UserSession.find
    end
  end
end

我收到错误:您必须在创建对象之前使用控制器对象激活 Authlogic::Session::Base.controller

我试过了:

Authlogic::Session::Base.controller = Authlogic::ControllerAdapters::RailsAdapter.new(self)

但这不起作用,因为 Connection 类不是 Controller。

我查看了 AuthLogic 代码,但不知道如何绕过它对控制器对象的依赖。我只需要加载用户的会话。有什么想法吗?

【问题讨论】:

    标签: ruby-on-rails ruby authlogic ruby-on-rails-5 actioncable


    【解决方案1】:

    我自己想出来的。我觉得这有点 hacky,基本上在我的 ApplicationController 中我使用 AuthLogic persistence_token 设置了一个安全 cookie,然后我可以读取这个令牌并在 ActionCable 中手动加载用户。

    class ApplicationController < ActionController::Base
      before_action :set_verify_cookie
    
      def set_verify_cookie
        #action cable needs a way outside of controller logic to lookup a user
       return unless current_user
        cookies.signed[:vvc] = current_user.persistence_token
      end
    end
    
    #app/channels/connection.rb
    module ApplicationCable
      class Connection < ActionCable::Connection::Base
        identified_by :current_user
    
    
        def connect
          self.current_user = find_verified_user
          logger.add_tags 'ActionCable', self.current_user.username unless self.current_user.nil?
        end
    
        protected
    
        def find_verified_user_or_guest
          User.find_by(:persistence_token => cookies.signed[:vvc])
        end
    end
    

    一个潜在的问题是,cookie 需要在注销时清除,否则 ActionCable 仍会在后续页面加载时找到用户。

    #app/controllers/user_sessions_controller.rb
    class UserSessionsController < ApplicationController
    
      def destroy
        cookies.signed[:vvc] = nil
        current_user_session.destroy
        flash[:success] = "Logout successful!"
        redirect_to root_url
      end
    end
    

    【讨论】:

    • 非常好的解决方案!很好的答案。请编辑此行:self.current_user = find_verified_user 对于:self.current_user = find_verified_user_or_guest
    【解决方案2】:

    假设您使用的是 Authlogic 默认值,持久性令牌存储在 cookie 中的“user_credentials”键下。

    所以你可以像这样查找你的用户:

    # app/channels/application_cable/connection.rb
    module ApplicationCable
      class Connection < ActionCable::Connection::Base
    
        def connect
          verify_user
        end
    
        private
        def verify_user
          reject_unauthorized_connection unless verified_user?
        end
    
        def verified_user?
          cookie_key && User.find_by_persistence_token(token)
        end
    
        def token
          cookie && cookie.include?('::') && cookie.split("::")[0]
        end
    
        def cookie
         cookies['user_credentials']
        end
    
      end
    end
    

    【讨论】:

      猜你喜欢
      • 2016-12-13
      • 2011-11-30
      • 2014-02-19
      • 2011-09-03
      • 2013-05-01
      • 2018-05-30
      • 2014-03-17
      • 2011-09-29
      • 2013-09-05
      相关资源
      最近更新 更多