【问题标题】:Symfony 6 - lexik JWT - Authenticator does not support the requestSymfony 6 - lexik JWT - Authenticator 不支持请求
【发布时间】:2022-02-07 04:54:22
【问题描述】:

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

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

有趣的是,当我使用 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 }

【问题讨论】:

    标签: symfony jwt nelmioapidocbundle


    【解决方案1】:

    发现 api_platform swagger 和 nelmio 都在以某种方式进行交互,因为我将其添加到 api_platform.yaml 中,并且标头在 nelmio 中也可用,现在可以使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-07-14
      • 1970-01-01
      • 2015-03-13
      • 2021-12-31
      • 2017-12-10
      • 1970-01-01
      • 2016-02-14
      相关资源
      最近更新 更多