我正在尝试创建一个指向我的网站的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/