security core
v7.2.0
安全性為複雜的授權系統提供了基礎設施,這使得可以輕鬆地將實際授權邏輯與保存使用者憑證的所謂使用者提供者分開。
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 的開發來幫助它!