防止用户在注销后使用后退按钮访问上一页

Prevent user from accessing previous page using back button after logout

使用 Phoenix 框架,如何在 he/she 注销并按下浏览器后退按钮后阻止用户访问之前的页面?

浏览器可以访问该页面,因为它默认允许缓存响应。如果您想防止这种情况发生,您需要根据 this similar question:

在需要身份验证的页面上设置适当的 HTTP headers
Cache-Control: no-cache, no-store, must-revalidate
Pragma: no-cache
Expires: 0

你可以在插件中做到这一点

defmodule MyApp.PreventCaching do
  import Plug.Conn

  def init(options) do
    options
  end

  def call(conn, _opts) do
    conn
    |> put_resp_header("cache-control", "no-cache, no-store, must-revalidate")
    |> put_resp_header("pragma", "no-cache")
    |> put_resp_header("expires", "0")
  end
end

然后在你的路由器(或控制器)中,你可以使用插件在所有需要认证的页面上设置headers

plug MyApp.PreventCaching