code202 / security-bundle
为Symfony项目提供用户管理。
Requires
- php: >8.1.0
- doctrine/orm: ^2.15||^3.0
- pagerfanta/core: ^4.3.1
- pagerfanta/doctrine-orm-adapter: ^4.3.1
- ramsey/collection: ^2.0
- symfony/console: 6.4.*||^7.0
- symfony/doctrine-bridge: 6.4.*||^7.0
- symfony/expression-language: 6.4.*||^7.0
- symfony/form: 6.4.*||^7.0
- symfony/polyfill-uuid: ~1.27
- symfony/property-access: 6.4.*||^7.0
- symfony/routing: 6.4.*||^7.0
- symfony/security-bundle: 6.4.*||^7.0
- symfony/serializer: 6.4.*||^7.0
- symfony/validator: 6.4.*||^7.0
- symfony/yaml: 6.4.*||^7.0
- zircote/swagger-php: ^4.7
Suggests
- lexik/jwt-authentication-bundle: This bundle provides JWT (Json Web Token) authentication for your Symfony API.
- nelmio/api-doc-bundle: Generates documentation for your REST API from annotations
- ramsey/uuid: A PHP library for generating and working with universally unique identifiers (UUIDs).
This package is auto-updated.
Last update: 2024-09-21 23:16:03 UTC
README
为Symfony项目提供用户管理。
配置
安全
在config/security.yaml
文件中为您的安全防火墙配置最小化配置
main:
pattern: ^/api/login
code202-login:
check_path: /api/login
您还可以禁用或配置认证方式,例如
main:
pattern: ^/api/login
code202-login:
check_path: /api/login
success_handler: lexik_jwt_authentication.handler.authentication_success
failure_handler: lexik_jwt_authentication.handler.authentication_failure
username_password_json:
check_path: /username
username_parameter: username
password_parameter: pass
remember_me_parameter: remember
username_password_form:
check_path: /username
token_by_email_json:
check_path: /email
token_by_email_form: false
用户提供者
要完全使用此扩展包,请务必使用我们专用的用户提供者
security:
providers:
code202_user:
id: Code202\Security\User\Provider
firewalls:
main:
provider: code202_user
路由
将以下路由添加到您的routing.yaml
security:
resource: '@Code202SecurityBundle/Resources/config/routing.yml'
name_prefix: api.security
prefix: api/security
前缀必须与您的防火墙模式相对应!
UUID
您必须选择如何生成和验证UUID。
# config/code202_security.yaml
code202_security:
uuid:
generator: 'your service name here'
validator: 'your service name here'
您的服务名称必须相应地实现Code202\Security\Uuid\UuidGeneratorInterface和Code202\Security\Uuid\UuidValidatorInterface
如果您使用相应的软件包,您还可以使用这些值 ramsey/uuid
或 symfony/polyfill-uuid
(默认)
会话TTL
您可以为不同的认证方式提供不同的Time To Live值,默认值为3600秒。
# config/code202_security.yaml
code202_security:
sessionTTL:
username_password: 7200
token_by_email: 1800
通过电子邮件令牌
您可以配置'token_by_email'认证行为
# config/code202_security.yaml
code202_security:
token_by_email:
refresher:
token_generator: 'your_generator_service_name'
#The service have to implement Code202Security\Service\Common\TokenGeneratorInterface
# Default value : 'number_base' to use our generator
minimal_refresh_interval: 'time_interval' # Default '1 minute'
lifetime_interval: 'time_interval' #Default '5minutes'
令牌生成器
如果您选择使用默认的令牌生成器,您可以按以下方式配置它
# config/code202_security.yaml
code202_security:
token_generator:
number_base:
size: 6 # The size of the generated token
角色策略
角色策略说明了哪些角色可以在哪些条件下授予或撤销。例如
# config/code202_security.yaml
code202_security:
role_strategies:
- roles:
- 'ROLE_1'
- 'ROLE_5'
to_grant: 'is_granted("ROLE_SUPER_ADMIN")'
- roles:
- 'ROLE_1'
to_grant: 'is_granted("ROLE_ADMIN")'
to_revoke: 'is_granted("ROLE_SUPER_ADMIN")'
- roles:
- 'ROLE_2'
to_grant: 'is_granted("ROLE_1")'
默认情况下,如果note中定义了to_revoke选项,则to_grant选项将应用于撤销条件。
桥梁
Nelmio/ApiDocBundle
此扩展包已经使用OpenApi属性。最佳做法是将我们的配置导入到您的nelmio-apièdoc.yaml
文件中
imports:
- { resource: '@Code202SecurityBundle/Resources/config/nelmio_api_doc.yaml' }
nelmio_api_doc:
documentation:
info:
title: My Project
description: Test application
version: 1.0.0
areas: # to filter documented areas
path_patterns:
- ^/api(?!/(doc|security)) # Accepts routes under /api except /api/doc
在更改认证器中的密钥、密码或remember_me参数的情况下,您可以像这样覆盖此配置
documentation:
info:
title: My Project
description: Test application
version: 1.0.0
components:
schemas:
LoginUsernameRequest:
properties:
login:
type: string
pass:
type: string
rememberMe:
type: boolean
areas: # to filter documented areas
path_patterns:
- ^/api(?!/(doc|security)) # Accepts routes under /api except /api/doc
您可以在配置中添加以下路由,以在API文档中显示登录路由
security-login:
resource: '@Code202SecurityBundle/Resources/config/routing-loging.yml'
name_prefix: api.security
开发笔记
使用Docker容器
将.home-developer路径导出到$DEV
export DEV ~/srv
启动容器
make console
清理代码
tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src
您可以使用--dry
选项