如何使用JWT symfony 4为两个类生成令牌?

时间:2019-08-19 21:10:21

标签: symfony authentication jwt

我有两个实体UserMerch

User使用表单并使用其用户名和密码进行身份验证。

Merch使用iPad应用程序并使用其merchCode(整数:例如11)和密码进行身份验证。

通过JWT身份验证,我可以为User生成令牌。 问题是我也不知道如何为Merch生成令牌。

我想使用两个不同的路径:

  • /api/login_check_user
  • /api/login_check_merch

对于Merch,我想返回包含令牌+ marchId的响应

我的security.yaml

security:
    encoders:
        App\Entity\User:
            algorithm: argon2i
        App\Entity\Merch:
                algorithm: auto


    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)
    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern:  ^/api/login
            stateless: true
            anonymous: true
            json_login:
                check_path:               /api/login_check_user
                success_handler:          lexik_jwt_authentication.handler.authentication_success
                failure_handler:          lexik_jwt_authentication.handler.authentication_failure

        refresh:
          pattern:  ^/api/token/refresh
          stateless: true
          anonymous: true

        api:
            pattern:   ^/api
            stateless: true
            guard:
                authenticators:
                    - lexik_jwt_authentication.jwt_token_authenticator
        main:
            anonymous: true

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

    access_control:
        - { path: ^/admin, roles: ROLE_ADMIN }
        - { path: ^/validator, roles: ROLE_VALIDATOR }
        - { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api/token/refresh, roles: IS_AUTHENTICATED_ANONYMOUSLY }
        - { path: ^/api,       roles: IS_AUTHENTICATED_FULLY }

我该如何解决?

1 个答案:

答案 0 :(得分:1)