我想使用ApiPlatform和OpenApi V3实现这种结果:
我在一个古老的SO问题上发现了这个问题:How to add Login to swagger UI with API PLATFORM (symfony 4)?
该路由从security.yaml插入我防火墙中的LexikJWT处理程序。
我设法在我的resources.yaml的ItemOperations键中添加了一个自定义内容,但是该内容似乎无法正确映射到OpenApi。
我误解了吗?
我应该放弃Lexik JWT捆绑包并以其他方式进行身份验证吗?
我是否缺少有关方案或YAML配置的内容?
答案 0 :(得分: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
{
// ...
}
P.S。我正在使用Symfony V5,但我认为这没什么
答案 1 :(得分:0)
我发现文档已更新,解决方案是检查工作人员装饰器端。 https://api-platform.com/docs/core/jwt/
答案 2 :(得分:0)
如果您将 Nelmio Api Doc Bundle 与 Symfony 一起使用可能会有所帮助
我最终通过手动更新 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
...
最终版本如下
const { Client } = require('pg');
const db = new Client({
connectionString: process.env.DATABASE_URL,
ssl: {
rejectUnauthorized: false
}
});