无法在SwaggerUI中显示/authentication_token JWT端点问题求助
解决Swagger UI中不显示JWT令牌端点的问题
技术栈
- Symfony 6.1
- PHP 8.1
- API Platform 3.0
问题分析与修复步骤
1. 服务配置顺序错误
在service.yaml中,_defaults是全局服务配置规则,必须放在所有具体服务定义之前,否则装饰器等服务无法正确继承自动注入、自动配置属性。
修正后的service.yaml:
# This file is the entry point to configure your own services. # Files in the packages/ subdirectory configure your dependencies. # Put parameters here that don't need to change on each machine where the app is deployed # https://symfony.com/doc/current/best_practices.html#use-parameters-for-application-configuration parameters: services: _defaults: autowire: true # Automatically injects dependencies in your services. autoconfigure: true # Automatically registers your services as commands, event subscribers, etc. App\: resource: '../src/' exclude: - '../src/DependencyInjection/' - '../src/Entity/' - '../src/Kernel.php' App\OpenApi\JwtDecorator: decorates: 'api_platform.openapi.factory' arguments: ['@.inner'] App\State\UserProcessor: tags: ['api_platform.state_processor'] bind: $decorated: '@api_platform.doctrine.orm.state.persist_processor' arguments: $passwordHasher: '@Symfony\Component\PasswordHasher\Hasher\UserPasswordHasherInterface' ApiPlatform\State\ProcessorInterface: ~
2. OpenApi装饰器中SecuritySchemes赋值错误
原代码中重新赋值$schemas变量,导致JWT安全方案未被添加回OpenApi组件。需直接操作getSecuritySchemes返回的对象,而非覆盖变量。
修正后的JwtDecorator.php:
<?php // api/src/OpenApi/JwtDecorator.php namespace App\OpenApi; use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface; use ApiPlatform\OpenApi\OpenApi; use ApiPlatform\OpenApi\Model; final class JwtDecorator implements OpenApiFactoryInterface { public function __construct( private OpenApiFactoryInterface $decorated ) {} public function __invoke(array $context = []): OpenApi { $openApi = ($this->decorated)($context); $schemas = $openApi->getComponents()->getSchemas(); $schemas['Token'] = new \ArrayObject([ 'type' => 'object', 'properties' => [ 'token' => [ 'type' => 'string', 'readOnly' => true, ], ], ]); $schemas['Credentials'] = new \ArrayObject([ 'type' => 'object', 'properties' => [ 'email' => [ 'type' => 'string', 'example' => 'johndoe@example.com', ], 'password' => [ 'type' => 'string', 'example' => 'apassword', ], ], ]); // 直接操作SecuritySchemes对象添加JWT配置 $securitySchemes = $openApi->getComponents()->getSecuritySchemes() ?? new \ArrayObject(); $securitySchemes['JWT'] = new \ArrayObject([ 'type' => 'http', 'scheme' => 'bearer', 'bearerFormat' => 'JWT', ]); $openApi->getComponents()->setSecuritySchemes($securitySchemes); $pathItem = new Model\PathItem( ref: 'JWT Token', post: new Model\Operation( operationId: 'postCredentialsItem', tags: ['Token'], responses: [ '200' => [ 'description' => '获取JWT令牌', 'content' => [ 'application/json' => [ 'schema' => [ '$ref' => '#/components/schemas/Token', ], ], ], ], ], summary: '获取JWT令牌用于登录', requestBody: new Model\RequestBody( description: '生成新的JWT令牌', content: new \ArrayObject([ 'application/json' => [ 'schema' => [ '$ref' => '#/components/schemas/Credentials', ], ], ]), ), security: [], ), ); $openApi->getPaths()->addPath('/authentication_token', $pathItem); return $openApi; } }
3. 验证防火墙与权限配置
确保security.yaml核心配置正确:
main防火墙的json_login.check_path指向/authentication_tokenaccess_control允许匿名用户访问该端点
核心配置片段:
security: # ...其他配置 firewalls: main: stateless: true lazy: true provider: app_user_provider json_login: check_path: /authentication_token username_path: email password_path: password success_handler: lexik_jwt_authentication.handler.authentication_success failure_handler: lexik_jwt_authentication.handler.authentication_failure jwt: ~ access_control: - { path: /api/docs, roles: PUBLIC_ACCESS } - { path: ^/authentication_token, roles: PUBLIC_ACCESS } # ...其他权限规则
最后操作
修改完成后清除缓存:
php bin/console cache:clear
刷新Swagger UI页面,即可看到/authentication_token端点。
内容的提问来源于stack exchange,提问作者Anthony Yah
相关产品推荐
相关产品推荐

