phpguild / api-bundle
Symfony API Bundle
v1.0.17
2021-09-16 05:14 UTC
Requires
- php: >=7.2
- ext-json: *
- api-platform/api-pack: 1.3.*
- gesdinet/jwt-refresh-token-bundle: ^0.10.1
- lexik/jwt-authentication-bundle: 2.11.*
- phpguild/user-bundle: 1.0.*
- symfony/string: *
- dev-main
- v1.0.17
- v1.0.16
- v1.0.15
- v1.0.14
- v1.0.13
- v1.0.12
- v1.0.11
- v1.0.10
- v1.0.9
- v1.0.8
- v1.0.7
- v1.0.6
- v1.0.5
- v1.0.4
- v1.0.3
- v1.0.2
- v1.0.1
- v1.0.0
- dev-dependabot/composer/symfony/security-bundle-5.4.20
- dev-dependabot/composer/twig/twig-3.4.3
- dev-dependabot/composer/symfony/framework-bundle-5.4.4
- dev-dependabot/composer/symfony/serializer-5.3.12
- dev-dependabot/composer/lcobucci/jwt-3.4.6
This package is auto-updated.
Last update: 2024-08-29 22:48:12 UTC
README
特性
身份验证
- 端点
[POST] /oauth/authenticate
通过登录密码验证用户并返回JWT Bearer令牌 - 端点
[POST] /oauth/refresh_token
通过刷新令牌验证用户并返回JWT Bearer令牌 - 端点
[GET,PUT] /users/me
用于获取认证用户信息
过滤器
- 多字段搜索过滤器
_search=搜索词
- 地理距离过滤器,搜索用户经纬度周围的搜索
_distance[lat]=40.123&_distance[lng]=1.123&_distance[near]=30
安装
使用composer安装
composer req phpguild/api-bundle
配置API Platform
编辑 config/packages/api_platform.yaml
api_platform:
version: '1.0.0'
mapping:
paths: [ '%kernel.project_dir%/src/Entity' ]
patch_formats:
json: [ 'application/merge-patch+json' ]
formats:
jsonld:
mime_types: [ 'application/ld+json' ]
json:
mime_types: [ 'application/json' ]
html:
mime_types: [ 'text/html' ]
error_formats:
jsonld:
mime_types: [ 'application/ld+json' ]
json:
mime_types: [ 'application/json' ]
swagger:
versions: [ 3 ]
api_keys:
apiKey:
name: Authorization
type: header
defaults:
pagination_enabled: true
pagination_items_per_page: 10
pagination_maximum_items_per_page: 30
pagination_client_partial: true
pagination_client_items_per_page: true
collection:
exists_parameter_name: _exists
order_parameter_name: _order
pagination:
page_parameter_name: _page
items_per_page_parameter_name: _itemsPerPage
partial_parameter_name: _partial
配置使用JWT的用户身份验证
编辑 config/packages/security.yaml
security:
encoders:
App\Entity\User:
algorithm: auto
providers:
authentication_user_provider:
entity:
class: App\Entity\User
property: username
token_user_provider:
entity:
class: App\Entity\User
property: id
firewalls:
dev:
pattern: ^/(_(profiler|wdt)|css|images|js)/
security: false
oauth_refresh_token:
pattern: ^/oauth/refresh_token
stateless: true
anonymous: true
oauth_authenticate:
pattern: ^/oauth/authenticate
stateless: true
anonymous: true
user_checker: PhpGuild\UserBundle\Security\UserChecker
json_login:
provider: authentication_user_provider
check_path: api_users_authentication
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
api:
pattern: ^/
stateless: true
anonymous: true
provider: token_user_provider
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
main:
anonymous: true
lazy: true
access_control:
- { path: ^/oauth/authenticate, methods: [ POST ], roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/oauth/refresh_token, methods: [ POST ], roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/docs, roles: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api, roles: IS_AUTHENTICATED_FULLY }
编辑 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)%'
token_ttl: 3600
user_identity_field: id
token_extractors:
authorization_header:
enabled: true
prefix: Bearer
name: Authorization
编辑 config/routes.yaml
phpguild_api_oauth:
resource: '@PhpGuildApiBundle/Resources/config/routes/oauth.yaml'
prefix: /oauth
配置刷新令牌
gesdinet_jwt_refresh_token:
firewall: api
ttl: 2592000
ttl_update: true
user_identity_field: id
user_provider: security.user.provider.concrete.token_user_provider
多搜索过滤器
用法
use PhpGuild\ApiBundle\Doctrine\Orm\Filter\MultisearchFilter;
/**
* @ApiResource
* @ApiFilter(MultisearchFilter::class, properties={"name", "description", "postalcode":"exact", "categories.name"})
*/
class MyEntity
{
private string $name;
private string $description;
private string $postalcode;
private Collection $categories;
地理距离过滤器
配置
编辑 config/packages/doctrine.yaml
doctrine:
orm:
dql:
numeric_functions:
acos: DoctrineExtensions\Query\Mysql\Acos
cos: DoctrineExtensions\Query\Mysql\Cos
radians: DoctrineExtensions\Query\Mysql\Radians
sin: DoctrineExtensions\Query\Mysql\Sin
用法
use PhpGuild\ApiBundle\Doctrine\Orm\Filter\GeoDistanceFilter;
/*
* @ApiResource
* @ApiFilter(GeoDistanceFilter::class, attributes={"latPropertyName":"lat", "lngPropertyName":"lng"})
*/
class MyEntity
{
private float $lat;
private float $lng;