mlukman/security-helper-bundle

一套简化Symfony安全性的类

安装: 6

依赖者: 0

建议者: 0

安全性: 0

星星: 0

关注者: 1

分支: 0

开放问题: 0

类型:symfony-bundle

1.0.0 2024-09-09 01:26 UTC

This package is auto-updated.

Last update: 2024-09-09 01:29:45 UTC


README

关于

Security Helper Bundle 是一个简化 Web 应用 AAA(认证、授权和审计)实现的 Symfony 7.x 扩展包。它是在核心 Symfony Security 扩展包之上的一个层。

安装

确保已全局安装 Composer,如 Composer 文档中的 安装章节 所述。

使用 Symfony Flex 的应用程序

打开命令行控制台,进入您的项目目录,然后执行以下命令

composer require mlukman/security-helper-bundle

不使用 Symfony Flex 的应用程序

步骤 1:下载扩展包

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

composer require mlukman/security-helper-bundle:1.*

步骤 2:启用扩展包

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

// config/bundles.php

return [
    // ...
    MLukman\SecurityHelperBundle\SecurityHelperBundle::class => ['all' => true],
];

激活

虽然 Composer 在安装此扩展包方面有很大帮助,但还需要进一步步骤来在您的 Web 应用程序中激活此扩展包。

创建子类 UserEntity 的 Doctrine 实体

大部分用于认证目的的列配置已经由 UserEntity 类实现,除了故意留给子类实现的 #[ORM\Id] 字段。您可以根据数据库设计需要添加关系。

示例

#[ORM\Entity]
#[ORM\Table]
class User extends UserEntity {
    
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column]
    protected ?int $id = null;

    /**
     * You might want to override this method to handle the scenario where a user is already logged in
     * proceeds to login with a different method and/or credentials. Applicable forlogin using OAuth only.
     * Example: make both user entities share the same profile or account
     */
    public function merge(UserEntity $tobemerged)
    {
        
    }    
}

实现 AuthenticationRepositoryInterface

实现类需要实现以下方法

getDefaultRedirectRoute() : string

此方法应返回在无法获取先前路由信息时要重定向到的路由。返回的路由也将用于用户登出后的重定向。

newUserEntity(string $method, string $credential, string $username = null): UserEntity

创建新的用户实体对象。此方法不应将对象保存到数据库中。

queryUserEntity(string $method, string $criteriaField, string $criteriaValue): ?UserEntity

根据方法、条件字段和条件值查询用户实体。如果没有找到此类实体,此方法可能返回 null。

queryUserEntityFromSecurityUser(UserInterface $securityUser): ?UserEntity

根据传递的 UserInterface 对象查询用户实体。如果没有找到此类实体,此方法可能返回 null。

saveUserEntity(UserEntity $user): void

保存传递的新/修改后的用户实体对象。

sendResetPasswordEmail(UserEntity $user): void

向用户发送重置密码邮件。

实现 LoginControllerInterface

实现类需要实现以下方法

login(Request $request, ClientRegistry $clientRegistry): Response

显示登录页面,应包含以下内容之一,具体取决于您要实现的认证方法

  • 用户名和密码输入字段
  • 用于使用 OAuth2 提供者登录的按钮

注册 AuthenticationRepositoryInterface 和 LoginControllerInterface 的实现

将以下内容添加到您的 services.yaml

services:
    # existing settings here
   
   
    MLukman\SecurityHelperBundle\Authentication\AuthenticationRepositoryInterface:
        class: 'App\Service\AuthenticationRepository'
    
    MLukman\SecurityHelperBundle\Controller\LoginControllerInterface:
        class: 'App\Controller\AuthController'
   

注册扩展包路由

将一个名为 security_helper.yaml 的 YAML 文件添加到您的 config/routes 文件夹中,内容如下(修改 prefix 参数以符合您的偏好)

security_helper:
    resource: '@SecurityHelperBundle/src/Resources/config/routes.xml'
    prefix: /@auth

注册主 Symfony Security 扩展包

将以下设置合并到您的 config/packages/security.yaml 文件中

security:
    providers:
        app_user_provider:
            entity:
                class: App\Entity\User # follow your UserEntity subclass name
                property: username
    firewalls:
        main:
            provider: app_user_provider
            custom_authenticators: # remove authenticators you don't need
                - MLukman\SecurityHelperBundle\Authentication\PasswordAuthenticator
                - MLukman\SecurityHelperBundle\Authentication\LDAPAuthenticator
                - MLukman\SecurityHelperBundle\Authentication\OAuth2Authenticator
            entry_point: MLukman\SecurityHelperBundle\Authentication\AuthenticationListener
            logout:
                path: security_logout
    access_control:
        # ensure the routing prefix you defined in security_helper.yaml has PUBLIC_ACCESS access control
        - { path: ^/@auth/, roles: PUBLIC_ACCESS }
        # adjust based on your sitemap
        - { path: ^/admin/, roles: ROLE_ADMIN }
        - { path: ^/, roles: PUBLIC_ASSESS }