【问题标题】:Adding auth route to generate a JWT in OpenApi 3在 OpenApi 3 中添加身份验证路由以生成 JWT
【发布时间】:2021-05-13 20:39:50
【问题描述】:

我想用 ApiPlatform 和 OpenApi V3 实现这种结果:

我在一个旧的 SO 问题上找到了这个:How to add Login to swagger UI with API PLATFORM (symfony 4)?

路由被插入到我的防火墙中的 LexikJWT 处理程序,来自 security.yaml。

我设法在我的 resources.yaml 的 ItemOperations 键中添加了一个自定义事物,但该事物似乎没有正确映射到 OpenApi。

我理解错了吗?

我应该放弃 Lexik JWT 捆绑包并以其他方式进行身份验证吗?

我是否遗漏了有关方案或 YAML 配置的内容?

【问题讨论】:

    标签: swagger symfony4 openapi api-platform.com


    【解决方案1】:

    我认为您对 config/routes.yaml 文件有疑问。我的配置看起来像:

    api_login_check:
      path: /api/users/login
      methods: [POST]
    

    你可能需要写成path: /authentication

    以防万一,我在这里写下我项目的完整 JWT 配置。

    config/packages/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)%'
        user_identity_field: phone <-- for auth user (username/login)
        token_ttl: 3600
    
    

    config/packages/security.yaml:

    security:
        encoders:
            App\Entity\User:
                algorithm: auto
    
        providers:
            app_user_provider:
                entity:
                    class: App\Entity\User
                    property: phone <-- it's my property that i use as username
    
        firewalls:
    
            login:
                pattern:  ^/api/
                stateless: true
                anonymous: true
                provider: app_user_provider
                guard:
                    authenticators:
                        - lexik_jwt_authentication.jwt_token_authenticator
                json_login:
                    check_path: ~
                    success_handler: lexik_jwt_authentication.handler.authentication_success
                    failure_handler: lexik_jwt_authentication.handler.authentication_failure
    
    

    src/Entity/User.php

    <?php
    declare(strict_types=1);
    
    namespace App\Entity;
    
    // use ...
    
    /**
     * @ApiResource(
     *     security="is_granted('ROLE_USER')",
     *     itemOperations={
     *         "get" = {"security" = "is_granted('ROLE_USER') and object == user"},
     *         // other operations ...
     *     },
     *     collectionOperations={
     *         "get",
     *         "post" = {
     *             "security" = "is_granted('IS_AUTHENTICATED_ANONYMOUSLY')"
     *         },
     *         "login" = {
     *             "security" = "is_granted('IS_AUTHENTICATED_ANONYMOUSLY')",
     *             "route_name" = "api_login_check",
     *             "method" = "POST",
     *             "openapi_context" = {
     *                 "summary" = "Login method",
     *                 "requestBody" = {
     *                     "description" = "Get token",
     *                     "content" = {
     *                         "application/json" = {
     *                             "schema" = {
     *                                 "type" = "object",
     *                                 "required" = {
     *                                     "username",
     *                                     "password"
     *                                 },
     *                                 "properties" = {
     *                                     "username" = {
     *                                         "type" = "string"
     *                                     },
     *                                     "password" = {
     *                                         "type" = "string"
     *                                     }
     *                                 }
     *                             }
     *                         }
     *                     }
     *                 }
     *             }
     *         },
     *         // other operations ...
     *     },
     *     // ...
     * )
     * // ...
     */
    class User implements UserInterface
    {
        // ...
    }
    

    附:我正在使用 Symfony V5,但我认为它没有什么不同

    【讨论】:

      【解决方案2】:

      我发现文档已更新,解决方案是检查 swagger 装饰器端。 https://api-platform.com/docs/core/jwt/

      【讨论】:

      • 大声笑)如果我理解正确的话。没问题,使用装饰器只更改 url :D
      【解决方案3】:

      如果你在 Symfony 中使用 Nelmio Api Doc Bundle 可能会有所帮助

      我最终通过手动更新 config/packages/nelmio_api_doc.yaml

      定义了 端点

      最终版本如下

      config/packages/nelmio_api_doc.yaml
      nelmio_api_doc:
          models:
              use_jms: false
          documentation:
              info:
                  title: My fancy API
                  description: Documentation
                  version: 1.0.0
              components:
                  securitySchemes:
                      Bearer:
                          type: http
                          scheme: bearer
                          bearerFormat: JWT
              security:
                  - Bearer: []
        
      # Begin of manual endpoint definition  
      #####################################
      
              paths:
                /api/auth/login:
                  post:
                    tags:
                      - Login
                    description: Login into the api.
                    requestBody:
                      description: Json body
                      required: true
                      content:
                        application/json:
                          schema:
                            type: object
                            properties:
                              username:
                                type: string
                              password:
                                type: string
                    responses:
                      '200':
                        description: Login successful
                        content:
                          application/json:
                            schema:
                              type: object
                              properties:
                                token:
                                  type: string
                                refresh_token:
                                  type: string
      ...
      

      【讨论】:

        猜你喜欢
        • 2021-04-20
        • 2019-07-20
        • 2017-11-21
        • 2019-09-18
        • 2021-11-02
        • 1970-01-01
        • 1970-01-01
        • 2018-03-01
        • 2017-12-29
        相关资源
        最近更新 更多