【问题标题】:How does devise's sign_in_and_redirect method stores location and where does stored location comes from?devise 的 sign_in_and_redirect 方法是如何存储位置的,存储的位置是从哪里来的?
【发布时间】:2023-04-09 10:05:02
【问题描述】:

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

这个想法是,一旦用户点击我在 LMS 工具上发布的课程,他将立即使用 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. devise 的 sign_in_and_redirect 方法如何存储位置?根据方法解释,它“..尝试首先重定向到存储的位置”。
  2. 存储位置从何而来?我想它来自 IdP(一个 GET 请求?)。
  3. 如何检查 IdP 是否正在发送要存储的位置?我的日志中没有看到任何 GET 请求。只是带有 SAML 消息的 POST 请求。

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

【问题讨论】:

    标签: ruby-on-rails redirect devise omniauth


    【解决方案1】:

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

    【讨论】:

    • 我可以在那个方法中传递 SAML 协议的 RelayState 参数吗?怎么样?
    • 对不起,我不明白 :) 重定向后你在哪里需要那个参数?您可以将任何 url 作为路径,所以是的,您可以将查询参数放在 url
    • IDP 使用 RelayState 向 SP 发出信号,指示 SP 在成功登录后应重定向到哪个 URL。它包含在 SAML 消息中。我想找到一种方法来告诉设计,在登录后,用户必须被重定向到该 URL,该 URL 会逐个更改。
    • 您是否在omniauth_callback_controller 方法中获得了RelayState 参数?如果你明白了 - 你只需将 sign_in_and_redirect 更改为 sign_in 然后将用户重定向到你想要的任何链接。
    • 是的!我刚刚发现了如何使用 params[:RelayState] 从 SAML 中提取 RelayState。现在我会按照你说的去做。非常感谢。
    猜你喜欢
    • 2020-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-26
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多