ruby-on-rails - devise 的 sign_in_and_redirect 方法是如何存储位置的,存储的位置是从哪里来的?
问题描述
我最近将我的应用程序作为 SP(服务提供者)集成到充当 IdP(身份提供者)的 LMS(学习管理系统)。
这个想法是,一旦用户点击我在 LMS 工具上发布的课程,他将立即使用 IdP 用户身份验证服务 (SSO) 重定向到我的应用程序中的课程 URL。
它只是部分起作用。SSO 有效,但一旦用户通过身份验证,他就会被重定向到我的应用程序中的索引页面,而不是课程的 url。
我去我的代码检查通过 saml 消息登录是如何工作的。在 omniauth_callback_controller.rb 中,登录方法是:
def saml
student = Student.where(email: request.env["omniauth.auth"]['uid'].to_s).first
if student
sign_in_and_redirect student, event: :authentication
else
flash[:error] = t 'flash_msg.access_1'
redirect_to root_path
end
end
然后我去设计检查“sign_in_and_redirect”方法是如何工作的,它说:
# Sign in a user and tries to redirect first to the stored location and
# then to the url specified by after_sign_in_path_for. It accepts the same
# parameters as the sign_in method.
代码是:
def sign_in_and_redirect(resource_or_scope, *args)
options = args.extract_options!
scope = Devise::Mapping.find_scope!(resource_or_scope)
resource = args.last || resource_or_scope
sign_in(scope, resource, options)
redirect_to after_sign_in_path_for(resource)
end
最后我得出的结论是,要了解发生了什么,我必须了解:
- devise 的 sign_in_and_redirect 方法如何存储位置?根据方法解释,它“..尝试首先重定向到存储的位置”。
- 存储位置从何而来?我想它来自 IdP(一个 GET 请求?)。
- 如何检查 IdP 是否正在发送要存储的位置?我的日志中没有看到任何 GET 请求。只是带有 SAML 消息的 POST 请求。
谁能帮我解决这些问题?非常感谢。
解决方案
您可以使用store_location_for
设计 gem 中的方法。例如store_location_for(:user, request.full_path)
or store_location_for(:user, request.referer)
,Devise 将使用该位置作为after_sign_in_path_for(:user)
推荐阅读
- python - sklearn 交叉验证:y 中人口最少的类只有 1 个成员,小于 n_splits=10
- python - 一段时间后,scrapy 停止爬行并产生项目,但继续运行
- rust - 是否可以 #[wasm_bindgen] 在另一个 crate 中定义的公共结构和函数?
- python - 我收到一个错误“无法将关键字'组'解析到字段中。”试图在 admin.py 中过滤组模型
- ruby-on-rails - form_for 选择字段中的占位符?
- git - 如何获得“有效”的 Git 忽略模式列表?
- bash - grep 文件 bash 中最后一次匹配与 nlines 之后的匹配
- sql-server - .NET Core API 未连接到在 Docker 上运行的 SQL Server
- firebase - 让ExpansionPanelList在Streambuilder中工作
- java - Android Lint:是否可以在 Maven 上的 jar 文件(不是 aar)中发布 lint 检查