【发布时间】:2016-02-06 20:18:40
【问题描述】:
使用Phoenix框架,如何在用户注销并按下浏览器返回按钮后阻止用户访问之前的页面?
【问题讨论】:
标签: logout elixir phoenix-framework
使用Phoenix框架,如何在用户注销并按下浏览器返回按钮后阻止用户访问之前的页面?
【问题讨论】:
标签: logout elixir phoenix-framework
浏览器可以访问页面,因为默认允许缓存响应。如果您想防止这种情况发生,您需要在需要身份验证的页面上设置适当的 HTTP 标头,如this similar question:
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
然后在您的路由器(或控制器)中,您可以使用插件在所有需要身份验证的页面上设置标题
plug MyApp.PreventCaching
【讨论】:
Cache-Control: no-store 而不是Cache-Control: private,no-cache,no-store,max-age=0,must-revalidate,pre-check=0,post-check=0
conn 作为 first_parameter 进行管道传输。因此,无需再次在 put_resp_header 内部传递。