如何使用 API 平台(symfony 4)将登录添加到 swagger UI? [英] How to add Login to swagger UI with API PLATFORM (symfony 4)?
问题描述
我安装了 &配置 LexikJWTAuthenticationBundle,它工作正常,但我遇到了一个小问题.
I installed & configured LexikJWTAuthenticationBundle, it works fine but I've got a small problem.
我已经包含了用于放置 JWT 令牌的授权按钮,问题是我可以获得令牌的唯一方法是使用以下命令:
I have include the Authorization button for put the JWT token, the problem is the only way I can have my token is to use this commands:
curl -X POST -H "Content-Type: application/json" http://localhost:8000/api/login_check -d '{"username":"johndoe","password":"test"}'
它发送给我的令牌,然后我把它放入 API 中,OK.
It send my the token and I put it in the API, OK.
第一个问题:当我使用 POSTMAN 尝试此请求时,出现错误:
First problem: When I try this request with POSTMAN I get an error :
无法找到路径/api/login_check"的控制器.路由配置错误.(404 未找到)
Unable to find the controller for path "/api/login_check". The route is wrongly configured. (404 Not Found)
请求:localhost:8000/api/login_check?username=johndoe&password=test with POST 方法
The request : localhost:8000/api/login_check?username=johndoe&password=test with POST method
所以使用 curl 它可以工作但不适用于邮递员,为什么?这是我的 security.yaml:
So with curl it's working but not with POSTMAN, why ? Here is my security.yaml:
security:
encoders:
App\Entity\User:
algorithm: argon2i
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
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
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
require_previous_session: false
register:
pattern: ^/api/register
stateless: true
anonymous: true
api:
pattern: ^/api
stateless: true
anonymous: true
provider: app_user_provider
main:
anonymous: true
access_control:
- { path: ^/api/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/register, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_ANONYMOUSLY }
role_hierarchy:
ROLE_DELEGATION: [ROLE_USER]
ROLE_EXPORT: [ROLE_USER]
ROLE_USER_ADMIN: [ROLE_USER]
ROLE_LIST_ADMIN: [ROLE_USER]
ROLE_IMPORT: [ROLE_USER]
ROLE_MOBILE: [ROLE_USER]
ROLE_ADMIN: [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]
ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ALLOWED_TO_SWITCH]
第二个问题,我想在 Swagger 中添加登录名.像这样:
Second problem, I'd like to add the login inside the Swagger. Like this:
我不知道该怎么做,尝试在 LoginCheckController 中添加 @ApiRessource() 但它什么也没做.
I have no idea how to doing this, tryed to add @ApiRessource() inside the LoginCheckController but it's doing nothing.
需要一些帮助/提示,提前致谢
Need some help / tips, thanks by advance
推荐答案
404 消息告诉您没有定义 /api/login_check
的路由,或者它存在配置问题并且路由不是成立您应该首先检查和调试您的路由,看看这是否已经定义
The 404 message tells you there's either no route defining /api/login_check
or there's a configuration problem with it and the route is not found
You should check and debug your routes first and see if this is already defined
login_check:
path: /login_check
您可能还想查看此 GitHub 问题
后期经过更多研究后,我在在 Stackoverflow 上
Later After researching a bit more, I found a possible solution to your problem here on Stackoverflow
这篇关于如何使用 API 平台(symfony 4)将登录添加到 swagger UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!