Table of Contents
Symfony 安全组件
安装和配置
现实世界的例子
用户类
数据库提供程序类
数据库身份验证提供程序类
整体工作原理
结论
Home Backend Development PHP Tutorial User authentication using Symfony security components

User authentication using Symfony security components

Sep 03, 2023 pm 11:29 PM

使用 Symfony 安全组件进行用户身份验证

在本文中,您将了解如何使用 Symfony 安全组件在 PHP 中设置用户身份验证。除了身份验证之外,我还将向您展示如何使用其基于角色的授权,您可以根据需要进行扩展。

Symfony 安全组件

Symfony 安全组件允许您轻松设置身份验证、基于角色的授权、CSRF 令牌等安全功能。事实上,它又分为四个子组件,您可以根据需要进行选择。

安全组件具有以下子组件:

  • symfony/安全核心
  • symfony/security-http
  • symfony/security-csrf
  • symfony/security-acl

在本文中,我们将探讨 symfony/security-core 组件提供的身份验证功能。

像往常一样,我们将从安装和配置说明开始,然后探索一些实际示例来演示关键概念。

安装和配置

在本节中,我们将安装 Symfony 安全组件。我假设您已经在系统上安装了 Composer — 我们需要它来安装 Packagist 上提供的安全组件。

因此,请继续使用以下命令安装安全组件。

$composer require symfony/security
Copy after login

在我们的示例中,我们将从 MySQL 数据库加载用户,因此我们还需要一个数据库抽象层。让我们安装最流行的数据库抽象层之一:Doctrine DBAL。

$composer require doctrine/dbal
Copy after login

这应该创建了 composer.json 文件,它应该如下所示:

{
    "require": {
        "symfony/security": "^4.1",
        "doctrine/dbal": "^2.7"
    }
}
Copy after login

让我们将 composer.json 文件修改为如下所示。

{
    "require": {
        "symfony/security": "^4.1",
        "doctrine/dbal": "^2.7"
    },
    "autoload": {
         "psr-4": {
             "Sfauth\\": "src"
         },
         "classmap": ["src"]
    }
}
Copy after login

由于我们添加了新的 classmap 条目,让我们继续通过运行以下命令来更新 Composer 自动加载器。

$composer dump -o
Copy after login

现在,您可以使用 Sfauth 命名空间来自动加载 src 目录下的类。

这就是安装部分,但是你应该如何使用它呢?事实上,只需将 Composer 创建的 autoload.php 文件包含在您的应用程序中即可,如以下代码片段所示。

<?php
require_once './vendor/autoload.php';

// application code
?>
Copy after login

现实世界的例子

首先,让我们了解一下 Symfony 安全组件提供的常规身份验证流程。

  • 第一件事是检索用户凭据并创建未经身份验证的令牌。
  • 接下来,我们会将未经身份验证的令牌传递给身份验证管理器进行验证。
  • 身份验证管理器可能包含不同的身份验证提供程序,其中之一将用于对当前用户请求进行身份验证。如何对用户进行身份验证的逻辑在身份验证提供程序中定义。
  • 身份验证提供商联系用户提供商以检索用户。用户提供商有责任从相应的后端加载用户。
  • 用户提供程序尝试使用身份验证提供程序提供的凭据加载用户。大多数情况下,用户提供程序返回实现 UserInterface 接口的用户对象。
  • 如果找到用户,身份验证提供程序会返回未经身份验证的令牌,您可以存储此令牌以供后续请求使用。

在我们的示例中,我们要将用户凭据与 MySQL 数据库进行匹配,因此我们需要创建数据库用户提供程序。我们还将创建处理身份验证逻辑的数据库身份验证提供程序。最后,我们将创建 User 类,它实现 UserInterface 接口。

用户类

在本节中,我们将创建 User 类,它代表身份验证过程中的用户实体。

继续创建包含以下内容的 src/User/User.php 文件。

<?php
namespace Sfauth\User;

use Symfony\Component\Security\Core\User\UserInterface;

class User implements UserInterface
{
    private $username;
    private $password;
    private $roles;

    public function __construct(string $username, string $password, string $roles)
    {
        if (empty($username))
        {
            throw new \InvalidArgumentException('No username provided.');
        }

        $this->username = $username;
        $this->password = $password;
        $this->roles = $roles;
    }

    public function getUsername()
    {
        return $this->username;
    }

    public function getPassword()
    {
        return $this->password;
    }

    public function getRoles()
    {
        return explode(",", $this->roles);
    }

    public function getSalt()
    {
        return '';
    }

    public function eraseCredentials() {}
}
Copy after login

重要的是 User 类必须实现 Symfony Security UserInterface 接口。除此之外,这里没有任何异常。

数据库提供程序类

从后端加载用户是用户提供者的责任。在本节中,我们将创建数据库用户提供程序,它从 MySQL 数据库加载用户。

让我们创建包含以下内容的 src/User/DatabaseUserProvider.php 文件。

<?php
namespace Sfauth\User;

use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Doctrine\DBAL\Connection;
use Sfauth\User\User;

class DatabaseUserProvider implements UserProviderInterface
{
    private $connection;

    public function __construct(Connection $connection)
    {
        $this->connection = $connection;
    }

    public function loadUserByUsername($username)
    {
        return $this->getUser($username);
    }

    private function getUser($username)
    {
        $sql = "SELECT * FROM sf_users WHERE username = :name";
        $stmt = $this->connection->prepare($sql);
        $stmt->bindValue("name", $username);
        $stmt->execute();
        $row = $stmt->fetch();

        if (!$row['username'])
        {
            $exception = new UsernameNotFoundException(sprintf('Username "%s" not found in the database.', $row['username']));
            $exception->setUsername($username);
            throw $exception;
        }
        else
        {
            return new User($row['username'], $row['password'], $row['roles']);
        }
    }

    public function refreshUser(UserInterface $user)
    {
        if (!$user instanceof User)
        {
            throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
        }

        return $this->getUser($user->getUsername());
    }

    public function supportsClass($class)
    {
        return 'Sfauth\User\User' === $class;
    }
}
Copy after login

用户提供者必须实现 UserProviderInterface 接口。我们使用 DBAL 学说来执行与数据库相关的操作。由于我们已经实现了 UserProviderInterface 接口,因此我们必须实现 loadUserByUsernamerefreshUsersupportsClass 方法。

loadUserByUsername 方法应通过用户名加载用户,这是在 getUser 方法中完成的。如果找到用户,我们返回对应的Sfauth\User\User对象,该对象实现了UserInterface接口。

另一方面, refreshUser 方法通过从数据库获取最新信息来刷新提供的 User 对象。

最后,supportsClass 方法检查 DatabaseUserProvider 提供程序是否支持提供的用户类。

数据库身份验证提供程序类

最后,我们需要实现用户身份验证提供程序,它定义身份验证逻辑 - 如何对用户进行身份验证。在我们的例子中,我们需要将用户凭据与 MySQL 数据库进行匹配,因此我们需要相应地定义身份验证逻辑。

继续创建包含以下内容的 src/User/DatabaseAuthenticationProvider.php 文件。

<?php
namespace Sfauth\User;

use Symfony\Component\Security\Core\Authentication\Provider\UserAuthenticationProvider;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserCheckerInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\AuthenticationServiceException;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

class DatabaseAuthenticationProvider extends UserAuthenticationProvider
{
    private $userProvider;

    public function __construct(UserProviderInterface $userProvider, UserCheckerInterface $userChecker, string $providerKey, bool $hideUserNotFoundExceptions = true)
    {
        parent::__construct($userChecker, $providerKey, $hideUserNotFoundExceptions);

        $this->userProvider = $userProvider;
    }

    protected function retrieveUser($username, UsernamePasswordToken $token)
    {
        $user = $token->getUser();

        if ($user instanceof UserInterface)
        {
            return $user;
        }

        try {
            $user = $this->userProvider->loadUserByUsername($username);

            if (!$user instanceof UserInterface)
            {
                throw new AuthenticationServiceException('The user provider must return a UserInterface object.');
            }

            return $user;
        } catch (UsernameNotFoundException $e) {
            $e->setUsername($username);

            throw $e;
        } catch (\Exception $e) {
            $e = new AuthenticationServiceException($e->getMessage(), 0, $e);
            $e->setToken($token);
            throw $e;
        }
    }

    protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
    {
        $currentUser = $token->getUser();
        
        if ($currentUser instanceof UserInterface)
        {
            if ($currentUser->getPassword() !== $user->getPassword())
            {
                throw new AuthenticationException('Credentials were changed from another session.');
            }
        }
        else
        {
            $password = $token->getCredentials();

            if (empty($password))
            {
                throw new AuthenticationException('Password can not be empty.');
            }

            if ($user->getPassword() != md5($password))
            {
                throw new AuthenticationException('Password is invalid.');
            }
        }
    }
}
Copy after login

DatabaseAuthenticationProvider 身份验证提供程序扩展了 UserAuthenticationProvider 抽象类。因此,我们需要实现 retrieveUsercheckAuthentication 抽象方法。

retrieveUser 方法的作用是从相应的用户提供程序加载用户。在我们的例子中,它将使用 DatabaseUserProvider 用户提供程序从 MySQL 数据库加载用户。

另一方面,checkAuthentication 方法执行必要的检查以验证当前用户的身份。请注意,我使用 MD5 方法进行密码加密。当然,您应该使用更安全的加密方法来存储用户密码。

整体工作原理

到目前为止,我们已经创建了身份验证所需的所有元素。在本节中,我们将了解如何将它们组合在一起来设置身份验证功能。

继续创建 db_auth.php 文件并使用以下内容填充它。

<?php
require_once './vendor/autoload.php';

use Sfauth\User\DatabaseUserProvider;
use Symfony\Component\Security\Core\User\UserChecker;
use Sfauth\User\DatabaseAuthenticationProvider;
use Symfony\Component\Security\Core\Authentication\AuthenticationProviderManager;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Exception\AuthenticationException;

// init doctrine db connection
$doctrineConnection = \Doctrine\DBAL\DriverManager::getConnection(
  array('url' => 'mysql://{USERNAME}:{PASSWORD}@{HOSTNAME}/{DATABASE_NAME}'), new \Doctrine\DBAL\Configuration()
);

// init our custom db user provider
$userProvider = new DatabaseUserProvider($doctrineConnection);

// we'll use default UserChecker, it's used to check additional checks like account lock/expired etc.
// you can implement your own by implementing UserCheckerInterface interface
$userChecker = new UserChecker();

// init our custom db authentication provider
$dbProvider = new DatabaseAuthenticationProvider(
    $userProvider,
    $userChecker,
    'frontend'
);

// init authentication provider manager
$authenticationManager = new AuthenticationProviderManager(array($dbProvider));

try {
    // init un/pw, usually you'll get these from the $_POST variable, submitted by the end user
    $username = 'admin';
    $password = 'admin';

    // get unauthenticated token
    $unauthenticatedToken = new UsernamePasswordToken(
        $username,
        $password,
        'frontend'
    );

    // authenticate user & get authenticated token
    $authenticatedToken = $authenticationManager->authenticate($unauthenticatedToken);

    // we have got the authenticated token (user is logged in now), it can be stored in a session for later use
    echo $authenticatedToken;
    echo "\n";
} catch (AuthenticationException $e) {
    echo $e->getMessage();
    echo "\n";
}
Copy after login

回想一下本文开头讨论的身份验证流程 - 上面的代码反映了该顺序。

第一件事是检索用户凭据并创建未经身份验证的令牌。

$unauthenticatedToken = new UsernamePasswordToken(
    $username,
    $password,
    'frontend'
);
Copy after login

接下来,我们将该令牌传递给身份验证管理器进行验证。

// authenticate user & get authenticated token
$authenticatedToken = $authenticationManager->authenticate($unauthenticatedToken);
Copy after login

当调用authenticate方法时,幕后会发生很多事情。

首先,身份验证管理器选择适当的身份验证提供程序。在我们的例子中,它是 DatabaseAuthenticationProvider 身份验证提供程序,将选择它进行身份验证。

接下来,它通过 DatabaseUserProvider 用户提供程序中的用户名检索用户。最后,checkAuthentication 方法执行必要的检查以验证当前用户请求。

如果您希望测试 db_auth.php 脚本,则需要在 MySQL 数据库中创建 sf_users 表。

CREATE TABLE `sf_users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  `roles` enum('registered','moderator','admin') DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;

INSERT INTO `sf_users` VALUES (1,'admin','21232f297a57a5a743894a0e4a801fc3','admin');
Copy after login

继续运行 db_auth.php 脚本,看看效果如何。成功完成后,您应该会收到一个经过身份验证的令牌,如以下代码片段所示。

$php db_auth.php
UsernamePasswordToken(user="admin", authenticated=true, roles="admin")
Copy after login

用户通过身份验证后,您可以将经过身份验证的令牌存储在会话中以供后续请求使用。

至此,我们就完成了简单的身份验证演示!

结论

今天,我们研究了 Symfony 安全组件,它允许您在 PHP 应用程序中集成安全功能。具体来说,我们讨论了 symfony/security-core 子组件提供的身份验证功能,并且我向您展示了如何在您自己的应用程序中实现此功能的示例。

请随意使用下面的提要发表您的想法!

The above is the detailed content of User authentication using Symfony security components. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1246
24
PHP and Python: Comparing Two Popular Programming Languages PHP and Python: Comparing Two Popular Programming Languages Apr 14, 2025 am 12:13 AM

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP in Action: Real-World Examples and Applications PHP in Action: Real-World Examples and Applications Apr 14, 2025 am 12:19 AM

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Explain secure password hashing in PHP (e.g., password_hash, password_verify). Why not use MD5 or SHA1? Apr 17, 2025 am 12:06 AM

In PHP, password_hash and password_verify functions should be used to implement secure password hashing, and MD5 or SHA1 should not be used. 1) password_hash generates a hash containing salt values ​​to enhance security. 2) Password_verify verify password and ensure security by comparing hash values. 3) MD5 and SHA1 are vulnerable and lack salt values, and are not suitable for modern password security.

How does PHP handle file uploads securely? How does PHP handle file uploads securely? Apr 10, 2025 am 09:37 AM

PHP handles file uploads through the $\_FILES variable. The methods to ensure security include: 1. Check upload errors, 2. Verify file type and size, 3. Prevent file overwriting, 4. Move files to a permanent storage location.

PHP: A Key Language for Web Development PHP: A Key Language for Web Development Apr 13, 2025 am 12:08 AM

PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

How does PHP type hinting work, including scalar types, return types, union types, and nullable types? How does PHP type hinting work, including scalar types, return types, union types, and nullable types? Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

The Enduring Relevance of PHP: Is It Still Alive? The Enduring Relevance of PHP: Is It Still Alive? Apr 14, 2025 am 12:12 AM

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP vs. Python: Understanding the Differences PHP vs. Python: Understanding the Differences Apr 11, 2025 am 12:15 AM

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

See all articles