bricre / micro-service-common
Bricre微服务常用的通用组件
0.0.2
2022-01-02 15:35 UTC
Requires
- lexik/jwt-authentication-bundle: ^2.8
- nixilla/php-jwt: ^0.1
- symfony/framework-bundle: ^5|^6
- symfony/http-client: ^5|^6
- symfony/serializer-pack: ^1.0
This package is auto-updated.
Last update: 2024-08-29 05:37:07 UTC
README
Symfony扩展 - micro-service-common
这是一个包含Bricre微服务常用组件的Symfony扩展。
安装
composer require bricre/micro-service-common
安全性
LexikJWTAuthenticationBundle 用于提供基于JWT的安全保护。
# security.yaml
security:
# https://symfony.ac.cn/doc/current/security.html#where-do-users-come-from-user-providers
providers:
# used to reload user from session & other features (e.g. switch_user)
app_user_provider:
entity:
class: App\Entity\User
property: username
firewalls:
main:
anonymous: true
lazy: true
provider: app_user_provider
stateless: true
json_login:
check_path: /api/user/login
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
guard:
authenticators:
- lexik_jwt_authentication.jwt_token_authenticator
要使用此扩展,您需要将 Kong 作为API网关可用。内部它会调用Kong的管理API创建 消费者 并检索JWT凭证。
请确保在您的 .env 中设置了SERVICE_KONG环境变量,指向Kong服务器
SERVICE_KONG=kong
安全工作流程
json_login
将使用app_user_provider
验证用户名和密码- 一旦登录凭证被接受,
lexik_jwt_authentication.handler.authentication_success
将返回一个JWT令牌,其中Bricre\MicroServiceCommonBundle\EventListener\JWTCreatedListener
会将当前用户信息注入到令牌中。 - 用户随后可以使用
Authentication: Bearer xxxxxxxx
标头进行请求 - 由于用户信息已经存在于令牌(JWT有效负载)中,
lexik_jwt_authentication.jwt_token_authenticator
将会自动解码令牌并验证用户。