code202/security-bundle

为Symfony项目提供用户管理。

1.3.15 2024-02-21 22:12 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/uuidsymfony/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选项