Search code examples
authenticationroutessymfony4

How to Restrict a post Request with basic auth in symfony 4.4


I need to protect a POST request with basic auth like http://host/import/myfile in Symfony 4.4

In security.yaml, I tried this but It doesn't work

  providers:
   authorized_users:
         http_basic:
            - identifier: '%env(HTTP_BASIC_AUTH_USERNAME)%'
            - password: '%env(HTTP_BASIC_AUTH_PASSWORD)%'
   firewalls:
         secured_area:
            methods: [POST]
            pattern: ^/import/myfile
            provider: authorized_users


Solution

  • Here is the answer, hope that will help !

        providers:
            authorized_users:
                memory:
                    users:
                        "%env(HTTP_BASIC_AUTH_USERNAME)%":
                            password: '%env(HTTP_BASIC_AUTH_PASSWORD)%'
                            
         firewalls:
            secured_area:
                pattern: ^/import/myfile
                stateless: true
                http_basic:
                    provider: authorized_users