How to implement multiple authentication in Symfony?

送分小仙女□ 提交于 2020-02-08 11:09:05

问题


I am following following tutorial about Backend API AUthentication using JWT. https://rojas.io/building-a-jwt-authenticator-in-symfony-4/

I have two authentication in my API.

  • LoginAuthenticator
  • JwtAuthenticator.

Below is my security.yaml file.

security:
    encoders:
        App\Entity\User:
            algorithm: auto
    providers:
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true
            guard:
                authenticators:
                    - App\Security\LoginAuthenticator
                    - App\Security\JwtAuthenticator
                entry_point: App\Security\LoginAuthenticator
            logout:
                path: api_logout
                success_handler: App\Security\JwtLogoutHandler
            stateless: true

    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

UserController

/**
     * @Route("/profile", name="api_profile")
     * @IsGranted("ROLE_USER")
     */
    public function profile()
    {
        return $this->json(
            [
                "user" => $this->getUser(),
            ],
            Response::HTTP_OK,
            [],
            [
                "groups" => ["api"]
            ]
        );
    }

After I make the guard stateless I am not being able to access the profile page.

OnAuthenticationSucess of loginAuthenticator, it will create a cookie named jwt But when I try to access the profile page. It gives access denied.

Can anybody please tell why I am not being able to access profile page?

来源:https://stackoverflow.com/questions/60027486/how-to-implement-multiple-authentication-in-symfony

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!