Search code examples
phpsymfonycsrfmiddleware

Creating a custom csrf security with Symfony 3


Actually, I am trying to add a csrf protection to my Symfony application, and what I am looking for is something similar to Laravel Csrf Middleware. That is to say, i would like to protect some specific route from csrf attack but the symfony doc doesn't explain it enough. Oh and also, I want to use it as a hash/token for my application api to prevent from not allowed person to access to any content.

Someone could help me please or just give me some advice about what should I do ? Thanks.


Solution

  • You can manually generate CSRF tokens with the security.csrf.token_managerservice:

    $token = $this->get('security.csrf.token_manager')->getToken($tokenId);
    

    Where tokenId is a specific id for a feature you want to secure. (in forms it is the form name).