跳至内容 跳至搜索
命名空间
方法
R
U
包含的模块

常量

ILLEGAL_HEADER_VALUE_REGEX = /[\x00-\x08\x0A-\x1F]/
 

实例公共方法

redirect_back(fallback_location:, allow_other_host: _allow_other_host, **args)

软弃用的别名,用于 redirect_back_or_to,其中 fallback_location 被作为关键字参数而不是第一个位置参数提供。

# File actionpack/lib/action_controller/metal/redirecting.rb, line 169
def redirect_back(fallback_location:, allow_other_host: _allow_other_host, **args)
  redirect_back_or_to fallback_location, allow_other_host: allow_other_host, **args
end

redirect_back_or_to(fallback_location, allow_other_host: _allow_other_host, **options)

如果可能,将浏览器重定向到发出请求的页面(引荐来源),否则重定向到提供的默认备用位置。

引荐来源信息取自请求中的 HTTP Referer(sic)标头。这是一个可选标头,其在请求中的存在取决于浏览器的安全设置和用户偏好。如果请求缺少此标头,将使用 fallback_location

redirect_back_or_to({ action: "show", id: 5 })
redirect_back_or_to @post
redirect_back_or_to "http://www.rubyonrails.org"
redirect_back_or_to "/images/screenshot.jpg"
redirect_back_or_to posts_url
redirect_back_or_to proc { edit_post_url(@post) }
redirect_back_or_to '/', allow_other_host: false

选项

  • :allow_other_host - 允许或禁止重定向到与当前主机不同的主机,默认为 true。

可以传递给 redirect_to 的所有其他选项都可作为选项接受,行为相同。

# File actionpack/lib/action_controller/metal/redirecting.rb, line 196
def redirect_back_or_to(fallback_location, allow_other_host: _allow_other_host, **options)
  if request.referer && (allow_other_host || _url_host_allowed?(request.referer))
    redirect_to request.referer, allow_other_host: allow_other_host, **options
  else
    # The method level `allow_other_host` doesn't apply in the fallback case, omit
    # and let the `redirect_to` handling take over.
    redirect_to fallback_location, **options
  end
end

redirect_to(options = {}, response_options = {})

将浏览器重定向到 options 中指定的目標。此参数可以是以下任何一种:

  • Hash - 将通过使用 options 调用 url_for 来生成 URL。

  • Record - 将通过使用 options 调用 url_for 来生成 URL,该 URL 将引用该记录的命名 URL。

  • protocol://(如 http://)开头的 String 或协议相对引用(如 //)- 直接作为重定向目标传递。

  • 不包含协议的 String - 当前协议和主机将附加到该字符串前面。

  • Proc - 将在控制器的上下文中执行的代码块。应返回 redirect_to 接受的任何选项。

示例

redirect_to action: "show", id: 5
redirect_to @post
redirect_to "http://www.rubyonrails.org"
redirect_to "/images/screenshot.jpg"
redirect_to posts_url
redirect_to proc { edit_post_url(@post) }

重定向以 302 Found 标头发生,除非使用 :status 选项另有指定。

redirect_to post_url(@post), status: :found
redirect_to action: 'atom', status: :moved_permanently
redirect_to post_url(@post), status: 301
redirect_to action: 'atom', status: 302

状态码可以是整数形式的标准 HTTP 状态码,也可以是代表小写、下划线化和符号化的描述的符号。请注意,状态码必须是 3xx HTTP 码,否则重定向将不会发生。

如果您正在使用 GET 或 POST 以外的 XHR 请求,并在请求后重定向,那么某些浏览器将使用原始请求方法跟随重定向。这可能导致不希望的行为,例如双重 DELETE。为了解决这个问题,您可以返回一个 303 See Other 状态码,该状态码将通过 GET 请求进行跟随。

redirect_to posts_url, status: :see_other
redirect_to action: 'index', status: 303

也可以在重定向时分配一个 flash 消息。常用的 flash 名称 alertnotice 有两个特殊的访问器,还有一个通用的 flash 存储桶。

redirect_to post_url(@post), alert: "Watch it, mister!"
redirect_to post_url(@post), status: :found, notice: "Pay attention to the road"
redirect_to post_url(@post), status: 301, flash: { updated_post_id: @post.id }
redirect_to({ action: 'atom' }, alert: "Something serious happened")

在我们的控制器中 redirect_to 之后的语句会被执行,所以 redirect_to 不会停止函数的执行。要立即终止函数在 redirect_to 后的执行,请使用 return。

redirect_to post_url(@post) and return

开放重定向保护

默认情况下,Rails 会保护您的应用程序免受重定向到外部主机的影响,即所谓的开放重定向。

在这里,redirect_to 会自动验证潜在不安全的 URL。

redirect_to params[:redirect_url]

action_on_open_redirect 配置选项控制检测到不安全重定向时的行为:* :log - 记录警告但允许重定向 * :notify - 发送 ActiveSupport 通知进行监控 * :raise - 引发 UnsafeRedirectError

要允许任何外部重定向通过,请传递 allow_other_host: true,尽管在这种情况下使用用户提供的参数是不安全的。

redirect_to "https://rubyonrails.cn", allow_other_host: true

请参阅 url_from 以了解什么是内部且安全的 URL,或如何在不安全的情况下回退到备用重定向 URL。

路径相对 URL 重定向保护

Rails 还会保护您免受可能不安全的、不以斜杠开头的路径相对 URL 重定向的侵害。这些可能导致安全漏洞。

redirect_to "example.com"     # Creates http://yourdomain.comexample.com
redirect_to "@attacker.com"   # Creates http://yourdomain.com@attacker.com
                              # which browsers interpret as user@host

您可以使用以下方式配置 Rails 如何处理这些情况:

config.action_controller.action_on_path_relative_redirect = :log    # default
config.action_controller.action_on_path_relative_redirect = :notify
config.action_controller.action_on_path_relative_redirect = :raise
  • :log - 记录警告但允许重定向

  • :notify - 发送 ActiveSupport 通知但允许重定向(包括堆栈跟踪以帮助识别来源)

  • :raise - 引发 UnsafeRedirectError

# File actionpack/lib/action_controller/metal/redirecting.rb, line 150
def redirect_to(options = {}, response_options = {})
  raise ActionControllerError.new("Cannot redirect to nil!") unless options
  raise AbstractController::DoubleRenderError if response_body

  allow_other_host = response_options.delete(:allow_other_host)

  proposed_status = _extract_redirect_to_status(options, response_options)

  redirect_to_location = _compute_redirect_to_location(request, options)
  _ensure_url_is_http_header_safe(redirect_to_location)

  self.location      = _enforce_open_redirect_protection(redirect_to_location, allow_other_host: allow_other_host)
  self.response_body = ""
  self.status        = proposed_status
end

url_from(location)

验证传入的 location 是否是一个内部 URL,并且可以安全地重定向到它,如果不是则返回 nil。用于包装由 params 提供的重定向 URL,并在不安全的情况下回退到备用 URL 进行重定向。

redirect_to url_from(params[:redirect_url]) || root_url

如果 location 位于与 request.host 相同的服务器上,则该 location 被视为内部且安全。

# If request.host is example.com:
url_from("https://example.com/profile") # => "https://example.com/profile"
url_from("http://example.com/profile")  # => "http://example.com/profile"
url_from("http://evil.com/profile")     # => nil

子域被视为主机的一部分。

# If request.host is on https://example.com or https://app.example.com, you'd get:
url_from("https://dev.example.com/profile") # => nil

注意:这与 url_for 有相似之处,后者可以在应用内部从各种选项生成内部 URL,例如 url_for(@post)。然而,url_from 用于验证外部参数,例如 url_from(params[:redirect_url])

# File actionpack/lib/action_controller/metal/redirecting.rb, line 254
def url_from(location)
  location = location.presence
  location if location && _url_host_allowed?(location)
end