简体   繁体   English

Rails设计,通过session_store键找到current_user

[英]Rails devise, find current_user by session_store key

We have a production system (main app) running on Rails with devise gem configured (using Redis session store)(session is enabled for all subdomain) 我们在Rails上运行了一个生产系统(主应用程序),并配置了devise gem(使用Redis会话存储)(为所有子域启用了会话)

I need to build a custom authentication service for one of our subdomain based microservice. 我需要为我们的基于子域的微服务之一构建自定义身份验证服务。 For each request, this microservice will be calling our main app with session id (the cookie value of rails session store key) (which is configured in config/initializers/session_store.rb). 对于每个请求,此微服务将使用会话ID(rails会话存储密钥的cookie值)(在config / initializers / session_store.rb中配置)调用我们的主应用。

So the question is, can I find the user just by session id? 所以问题是,我可以仅通过会话ID找到用户吗? (Using Devise or Warden) (使用设计或管理员)

You can do this with a method Devise's Authenticatable module adds to your resource called serialize_from_session . 您可以使用Devise的Authenticateable模块添加到资源中的方法来实现这一目的,该方法称为serialize_from_session You can pass in an expanded Warden session variable, which includes both the key and salt ( serialize_from_session accepts both of those as parameters). 您可以传入一个扩展的Warden会话变量,该变量同时包含密钥和盐( serialize_from_session接受这两个参数)。 For example, if you are using Devise on a User model: 例如,如果您在用户模型上使用Devise:

user = User.serialize_from_array(*session['warden.user.user.key'])

The session['warden.user.user.key'] variable is an array of the record ID (as an array) and a salt. session['warden.user.user.key']变量是记录ID的数组(作为数组)和盐。 It would look something like [[1], "$2f$23$2DIUF7Vr.J5sbfGwHTew"] . 看起来像[[1], "$2f$23$2DIUF7Vr.J5sbfGwHTew"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM