devise 的 sign_in_and_redirect 方法如何存储位置以及存储的位置从何而来?

How does devise's sign_in_and_redirect method stores location and where does stored location comes from?

我最近将我的应用程序作为 SP(服务提供商)集成到充当 IdP(身份提供商)的 LMS(学习管理系统)。

我的想法是,一旦用户点击了我在 LMS 工具上发布的课程,他将立即被重定向到我的应用程序中的课程 url,使用 IdP 用户身份验证服务 (SSO)。

它只是部分起作用了。 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. 如何设计 sign_in_and_redirect 方法来存储位置?根据方法解释,它“..首先尝试重定向到存储的位置”。
  2. 存储的位置从何而来?我想它来自 IdP(GET 请求?)。
  3. 如何检查 IdP 是否发送了要存储的位置?我在我的日志中没有看到任何 GET 请求。只是带有 SAML 消息的 POST 请求。

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

您可以使用设计 gem 中的 store_location_for 方法。例如 store_location_for(:user, request.full_path)store_location_for(:user, request.referer),Devise 将使用该位置作为 after_sign_in_path_for(:user)