symfony/security-core

Symfony 安全组件 - 核心库

v7.1.5 2024-09-20 13:35 UTC

This package is auto-updated.

Last update: 2024-09-21 06:12:57 UTC


README

安全提供了复杂授权系统的基础设施,这使得将实际的授权逻辑与所谓的用户提供者(持有用户凭证)轻松分离成为可能。

入门指南

composer require symfony/security-core
use Symfony\Component\Security\Core\Authentication\AuthenticationTrustResolver;
use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
use Symfony\Component\Security\Core\Authorization\AccessDecisionManager;
use Symfony\Component\Security\Core\Authorization\Voter\AuthenticatedVoter;
use Symfony\Component\Security\Core\Authorization\Voter\RoleVoter;
use Symfony\Component\Security\Core\Authorization\Voter\RoleHierarchyVoter;
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
use Symfony\Component\Security\Core\Role\RoleHierarchy;

$accessDecisionManager = new AccessDecisionManager([
    new AuthenticatedVoter(new AuthenticationTrustResolver()),
    new RoleVoter(),
    new RoleHierarchyVoter(new RoleHierarchy([
        'ROLE_ADMIN' => ['ROLE_USER'],
    ]))
]);

$user = new \App\Entity\User(...);
$token = new UsernamePasswordToken($user, 'main', $user->getRoles());

if (!$accessDecisionManager->decide($token, ['ROLE_ADMIN'])) {
    throw new AccessDeniedException();
}

赞助商

Symfony 7.1 的安全组件由 支持SymfonyCasts

通过观看真实项目的构建过程并积极参与编码,可以更快地学习 Symfony。SymfonyCasts 弥合了学习差距,为您提供了视频教程和编码挑战。继续编码!

通过 赞助 Symfony 的开发来帮助 Symfony。

资源