code-colliders/basic-user-bundle

Symfony 中认证的基本集成

1.0.2 2020-05-11 13:59 UTC

This package is auto-updated.

Last update: 2024-09-26 10:47:54 UTC


README

请确保全局已安装 Composer,具体请参阅 Composer 文档中的安装章节

使用 Symfony Flex 的应用程序

打开命令行控制台,进入您的项目目录并执行

$ composer require code-colliders/basic-user-bundle

不使用 Symfony Flex 的应用程序

步骤 1: 下载 Bundle

打开命令行控制台,进入您的项目目录,并执行以下命令以下载此 Bundle 的最新稳定版本

$ composer require code-colliders/basic-user-bundle

步骤 2: 启用 Bundle

然后,通过将其添加到项目 config/bundles.php 文件中注册的 Bundle 列表来启用 Bundle

// config/bundles.php

return [
    // ...
    CodeColliders\BasicUserBundle\CodeCollidersBasicUserBundle::class => ['all' => true],
];

使用命令行工具配置 Bundle

使用 php bin/console basic-user:init 创建 Bundle 配置

使用 php bin/console basic-user:init -c User 创建 Bundle 配置和 User 实体

手动配置 Bundle

步骤 1: 创建 User 类

$ php bin/console make:entity User

该类必须实现 \Symfony\Component\Security\Core\User\UserInterface

或扩展 \CodeColliders\BasicUserBundle\Entity\UserBase

步骤 2: 创建配置

包配置

# config/packages/code_colliders_basic_user.yaml

code_colliders_basic_user:
  user_class: App\Entity\User # The fully qualified class for your user
  redirect_route: home # Default redirection after login (default: login page)
  user_identifier: email # Unique field used in your user entity to login 
  branding: # Optional part
    logo_url: null # Picture url to add a logo in login form  
    background_url: null # Picture url to add a background image in login form page
    form_title: "Log in" # The title of the form (default: Log in)
    catchphrase: "Using basic user bundle" # A catchphrase at the bottom of the form
    form_identifier_type: email # Type of field for user identifier 

您可以使用 php bin/console config:dump-reference code_colliders_basic_user 获取配置文件引用。

路由配置

# config/routes/code_colliders_basic_user.yaml

basic_user_login:
  resource: '@CodeCollidersBasicUserBundle/Resources/config/routes.xml'
  prefix: /user # The prefix for routes '/login' and '/logout'

配置安全 Bundle

# config/packages/security.yaml

security:
    # Roles configurations
    role_hierarchy:
        ROLE_ADMIN:       ROLE_USER
    # https://symfony.com.cn/doc/current/security.html#where-do-users-come-from-user-providers
    encoders:
        App\Entity\User:  # Your user class
            algorithm: auto

        # https://symfony.com.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:
        dev:
            pattern: ^/(_(profiler|wdt)|css|images|js)/
            security: false
        main:
            anonymous: true
            logout:
                path: code_colliders_basic_user_logout
            guard:
                authenticators:
                    - code_colliders_basic_user.authenticator



            # activate different ways to authenticate
            # https://symfony.com.cn/doc/current/security.html#firewalls-authentication

            # https://symfony.com.cn/doc/current/security/impersonating_user.html
            # switch_user: true

    # Easy way to control access for large sections of your site
    # Note: Only the *first* access control that matches will be used
    access_control:
        # - { path: ^/admin, roles: ROLE_ADMIN }
        # - { path: ^/profile, roles: ROLE_USER }

 

您可以使用 php bin/console config:dump-reference security 获取安全配置文件引用。