Symfony 6 - lexik JWT - 验证器不支持请求

Symfony 6 - lexik JWT - Authenticator does not support the request

我在使用 lexik JWT 捆绑包和 Symfony 6.0 时遇到了一些问题,为了大摇大摆,我使用了 NelmioApiDocBundle。

问题是,在我决定应用从 lexik JWT 生成的授权令牌(不记名令牌)之前,每个人都大摇大摆地工作。但是,一旦我通过 /api/sign/in 端点生成令牌并将其放入现场,突然所有端点都停止工作。就像 swagger 有这个加载动画,但没有请求来(用 xDebug 测试,还有 symfony 分析器)。

有趣的是,当我使用 Postman 并在那里应用令牌时,我立即得到了正确的响应。所以我不确定问题出在哪里或什么,但是当从 Swagger 调用时,我可以看到 docker 调试消息说:PHP message: [debug] Authenticator does not support the request.

我将我的配置放在下面。提前致谢。

lexik_jwt_authentication.yaml:

lexik_jwt_authentication:
    secret_key: '%env(resolve:JWT_SECRET_KEY)%'
    public_key: '%env(resolve:JWT_PUBLIC_KEY)%'
    pass_phrase: '%env(JWT_PASSPHRASE)%'
    token_ttl: 8640000
    user_id_claim: id
    user_identity_field: email

security.yaml

security:
    enable_authenticator_manager: true

    password_hashers:
        App\Entity\User:
            algorithm: bcrypt
            cost: 10

    providers:
        app_user_provider:
            entity:
                class: App\Entity\User
                property: email

    firewalls:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false

        login:
            pattern: ^/api/sign
            stateless: true
            provider: app_user_provider
            json_login:
                check_path: /api/sign/in
                username_path: email
                password_path: password
                success_handler: lexik_jwt_authentication.handler.authentication_success
                failure_handler: lexik_jwt_authentication.handler.authentication_failure

        api:
            provider: app_user_provider
            pattern: ^/api
            stateless: true
            jwt: ~


    access_control:
        - { path: ^/api/sign/, roles: PUBLIC_ACCESS }
        - { path: ^/api/(doc|doc.json), roles: PUBLIC_ACCESS }
        - { path: ^/api, roles: IS_AUTHENTICATED_FULLY }

nelmio_api_doc.yaml

nelmio_api_doc:
    documentation:
        servers:
            - url: http://bp.project

        info:
            title: BP PROJECT
            description: This is an awesome app!
            version: 1.0.0
        components:
            securitySchemes:
                Bearer:
                    type: http
                    scheme: bearer
                    bearerFormat: JWT
        security:
            Bearer: [ ]
    areas: # to filter documented areas
        path_patterns: # an array of regexps
            - ^/api(?!/(doc|doc.json|docs.{_format}|{index}.{_format}|contexts/{shortName}.{_format})$) # Accepts routes under /api except ...

    models: { use_jms: false }

发现 api_platform swagger 和 nelmio 都以某种方式相互作用,因为我将其添加到 api_platform.yaml 并且 header 在 nelmio 中也可用,现在可以使用了。

swagger:
    versions: [3]
    api_keys:
        apiKey:
            name: Authorization
            type: header