elixir - 如何在Phoenix框架中有选择地禁用CSRF检查

标签 elixir csrf phoenix-framework csrf-protection

我正在尝试创建一个指向我的网站的Facebook页面选项卡。
Facebook将HTTP POST请求发送到我的网站的网址。
这里的问题是服务器具有内置的CSRF检查,并且它返回以下错误:

(Plug.CSRFProtection.InvalidCSRFTokenError) invalid CSRF (Cross Site  Forgery Protection) token, make sure all requests include a '_csrf_token' param or an 'x-csrf-token' header`

服务器需要Facebook不能拥有的CSRF token 。因此,我想有选择地禁用路径www.mywebsite.com/facebook的CSRF。

我如何在Phoenix Framework中做到这一点?

最佳答案

路由器中已使用Plug.CSRFProtection启用了protect_from_forgery。默认在browser管道中设置。一旦添加了插件,便无法禁用它,而不必首先将其设置。您可以通过将其移出browser并仅在需要时将其包括在内来做到这一点。

defmodule Foo.Router do
  use Foo.Web, :router

  pipeline :browser do
    plug :accepts, ["html"]
    plug :fetch_session
    plug :fetch_flash
    #plug :protect_from_forgery - move this
  end

  pipeline :csrf do
    plug :protect_from_forgery # to here
  end

  pipeline :api do
    plug :accepts, ["json"]
  end

  scope "/", Foo do
    pipe_through [:browser, :csrf] # Use both browser and csrf pipelines

    get "/", PageController, :index
  end

  scope "/", Foo do
    pipe_through :browser # Use only the browser pipeline

    get "/facebook", PageController, :index #You can use the same controller and actions if you like
  end

end

关于elixir - 如何在Phoenix框架中有选择地禁用CSRF检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32580974/

相关文章:

elixir - 找不到相对于目录 "es2015"的预设 "web/static/js"

elixir - 在 Elixir 中部署时更改后端/模块?

elixir - 使用 Ecto 一次插入多行。 "protocol Enumerable not implemented for #Ecto.Changeset"

security - 匿名评论/帖子表单是否需要 csrf token ?如果不是,为什么要使用它以及如何实现它?

elixir - Elixir Phoenix 中的包罗万象/通配符路线?

laravel - 获取新的 csrf token ,即使当前 token 已过期

ruby-on-rails - 有时禁用 CSRF 保护是合理的吗?

elixir - Phoenix 这么慢?请帮助提高其性能

elixir - List.flatten 返回意外值

regex - Elixir 标点符号替换正则表达式