symfony/security-bundle
Symfony SecurityBundle integrates the Security component into the Symfony full-stack framework, providing authentication, authorization, and related security features with seamless configuration and framework tooling.
Installation:
composer require symfony/security-bundle
Add to config/bundles.php:
return [
// ...
Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
];
Basic Configuration (config/packages/security.yaml):
security:
enable_authenticator_manager: true
password_hashers:
App\Entity\User: 'auto'
firewalls:
main:
lazy: true
provider: app_user_provider
form_login:
login_path: app_login
check_path: app_login
logout:
path: app_logout
providers:
app_user_provider:
entity:
class: App\Entity\User
property: email
First Use Case:
User entity with UserInterface and PasswordAuthenticatedUserInterface.php bin/console make:auth
use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
#[IsGranted('ROLE_USER')]
public function secureAction(): Response { ... }
config/packages/security.yaml (main config)src/Security/LoginFormAuthenticator.php (default authenticator)src/Entity/User.php (user implementation)Form Login:
form_login under a firewall.login_path, check_path, and csrf_token_generator.form_login:
login_path: app_login
check_path: app_login_check
csrf_token_generator: security.csrf.token_manager
API Token Authentication:
stateless: true and jwt or api_key authenticators.firewalls:
api:
pattern: ^/api
stateless: true
jwt: ~
OAuth/OIDC:
security.yaml:
oauth:
providers:
github:
client_id: '%env(GITHUB_CLIENT_ID)%'
client_secret: '%env(GITHUB_CLIENT_SECRET)%'
scope: 'read:user'
firewalls:
main:
oauth: github
security.yaml:
role_hierarchy:
ROLE_ADMIN: ROLE_USER
ROLE_SUPER_ADMIN: [ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]
#[IsGranted('ROLE_ADMIN')]
public function adminAction() { ... }
{% if is_granted('ROLE_ADMIN') %}
Admin Dashboard
{% endif %}
Create a Custom Authenticator:
php bin/console make:authenticator
AbstractAuthenticator or AbstractFormLoginAuthenticator.supports(), getCredentials(), getUser(), and checkCredentials().Register in security.yaml:
firewalls:
main:
custom_authenticators:
- App\Security\CustomAuthenticator
lazy: true to defer authentication until needed.firewalls:
admin:
pattern: ^/admin
form_login: ~
api:
pattern: ^/api
jwt: ~
AuthenticationSuccess, AccessDenied):
use Symfony\Component\Security\Http\Event\AuthenticationSuccessEvent;
public function onAuthenticationSuccess(AuthenticationSuccessEvent $event) {
$user = $event->getUser();
// Custom logic (e.g., log, redirect)
}
services.yaml:
services:
App\EventListener\SecurityListener:
tags:
- { name: kernel.event_listener, event: security.authentication.success, method: onAuthenticationSuccess }
CSRF Token Mismatches:
csrf_token_generator matches your form setup.security.csrf.token_manager in the container.Lazy Firewall Issues:
lazy: true with remember_me on public routes (fixed in v8.0.6).context: main explicitly if needed:
remember_me:
context: main
Role Hierarchy Conflicts:
php bin/console debug:role-hierarchy
ROLE_ prefix consistently.OIDC/JWT Token Expiry:
security:oidc:jwks:cache (Symfony 8+).Deprecated Features:
hide_user_not_found (removed in v7.4+).Profiler:
config/packages/dev/security.yaml:
security:
profiler: true
Logging:
config/packages/dev/monolog.yaml:
handlers:
security:
type: stream
path: "%kernel.logs_dir%/security.log"
level: debug
Token Storage:
$this->container->get('security.token_storage')->setToken(null);
XML Configuration:
Provider Factories:
security:
providers:
custom_provider:
factory: [App\Security\UserProviderFactory, createUserProvider]
Trusted Hosts for CAS:
framework:
trusted_proxies: [192.168.0.1]
Custom User Checker:
UserCheckerInterface:
public function checkPostAuth(UserInterface $user) { ... }
tags: ['security.user_checker'].Voter Extensions:
use Symfony\Component\Security\Core\Authorization\Voter\Voter;
class CustomVoter extends Voter { ... }
services.yaml:
tags: ['security.voter']
Rate Limiting:
security:
firewalls:
main:
login_throttling:
max_attempts: 5
interval: 3600
Mermaid Role Hierarchy:
php bin/console debug:role-hierarchy --format=mermaid
How can I help you explore Laravel packages today?