symfonyjwtyamllexikjwtauthbundle

How to restrict json_login route with only POST method?


I use lexik JWT to secure my api and i can login with it. But the login route works with get and post request when i test with postman. I want to restrict with POST only.

To do so i tried to add - { path: ^/auth/login_check, roles: PUBLIC_ACCESS, methods:['POST'] } in the access control but it does not do the trick.

I have no error but i still can do get request and have my token back.

security:
    enable_authenticator_manager: true
    # https://symfony.com/doc/current/security.html#registering-the-user-hashing-passwords
    password_hashers:
        Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
    # https://symfony.com/doc/current/security.html#loading-the-user-the-user-provider
    providers:
        # used to reload user from session & other features (e.g. switch_user)
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
        # used to reload user from session & other features (e.g. switch_user)
        # used to reload user from session & other features (e.g. switch_user)
        # used to reload user from session & other features (e.g. switch_user)
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        api_login:
            pattern: ^/auth/login
            provider: app_user_provider
            stateless: true
            json_login:
                username_path: email
                check_path: /auth/login_check
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure
        api:
            pattern: ^/api
            stateless: true
            jwt: ~


            # activate different ways to authenticate
            # https://symfony.com/doc/current/security.html#the-firewall

            # https://symfony.com/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        - { path: ^/auth/login, roles: PUBLIC_ACCESS }
        - { path: ^/auth/login_check, roles: PUBLIC_ACCESS, methods:['POST'] }
        - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

Solution

  • So i found the answer myself. The methods option in access_control is a matching option and it does not restrict.

    To restrict a route to a specific option, it has to be set in the route like this

    #[Route('/my_route', name: 'my_route',methods: ['POST'])]