2015-09-15 13 views
16

Sto tentando di creare una scheda Pagina Facebook che punti al mio sito web. Facebook invia una richiesta HTTP POST all'URL del mio sito web. Il problema qui è che il server dispone di un controllo CSRF built-in, e restituisce il seguente errore:Come disabilitare selettivamente il controllo CSRF nel framework Phoenix

(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` 

Il server si aspetta un CSRF token che Facebook non può avere. Quindi, voglio disabilitare selettivamente CSRF per il percorso www.mywebsite.com/facebook.

Come posso farlo in Phoenix Framework?

risposta

24

Plug.CSRFProtection è abilitato nel router con protect_from_forgery. È impostato per impostazione predefinita nella pipeline browser. Una volta aggiunta una spina, non c'è modo di disabilitarla, invece non deve essere impostata in primo luogo. Puoi farlo spostandolo da browser e includendolo solo quando richiesto.

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 
+1

Grazie !! Funziona! :) –

Problemi correlati