首页 > 解决方案 > 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

最后我得出的结论是,要了解发生了什么,我必须了解:

  1. devise 的 sign_in_and_redirect 方法如何存储位置?根据方法解释,它“..尝试首先重定向到存储的位置”。
  2. 存储位置从何而来?我想它来自 IdP(一个 GET 请求?)。
  3. 如何检查 IdP 是否正在发送要存储的位置?我的日志中没有看到任何 GET 请求。只是带有 SAML 消息的 POST 请求。

谁能帮我解决这些问题?非常感谢。

标签: ruby-on-railsredirectdeviseomniauth

解决方案


您可以使用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)


推荐阅读