Lesson 7 of 9 · 28m
The Security Component in Practice
Authenticators, firewalls, password hashing, roles, voters, CSRF, and the authorization mistakes that make 'it worked in staging' famous.
Symfony Security is a pipeline for authentication plus a decision layer for authorization. Teams get hurt when they collapse those ideas into is_granted('ROLE_ADMIN') scattered across controllers and templates.
Authentication is pluggable, not magical
security:
password_hashers:
Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface: 'auto'
providers:
app_user_provider:
entity: { class: App\Entity\User, property: email }
firewalls:
main:
pattern: ^/
custom_authenticator: App\Security\LoginFormAuthenticator
logout: { path: app_logout }'auto' chooses modern password hashing and evolves with platform support. Do not pin weak legacy algorithms because an old integration document says sha512; migrate legacy hashes on login.
Roles are coarse claims, not business policy
Roles can describe broad populations. They cannot express object-level rules such as whether this finance user may approve this refund for this tenant right now. That is what voters are for:
final class RefundVoter extends Voter
{
protected function supports(string $attribute, mixed $subject): bool
{
return $attribute === 'REFUND_APPROVE' && $subject instanceof Refund;
}
}The full voteOnAttribute implementation should check authentication, tenant, object state, and role in one testable place:
#[IsGranted('REFUND_APPROVE', subject: 'refund')]
public function approve(Refund $refund): ResponseNow the rule is discoverable and hard to bypass accidentally in a template.
Deny by default, expose deliberately
Broad access_control entries can protect /admin, but prefixes do not know whether user A may edit tenant B's record. Enforce object-level authorization near the use case. Check version-specific constants too: modern configuration uses PUBLIC_ACCESS; older applications often used IS_AUTHENTICATED_ANONYMOUSLY.
Sessions and telemetry
Use secure, HTTPS-only cookies with appropriate SameSite; rotate session ids on privilege change; keep CSRF enabled. Log authorization failures with enough context to distinguish probing, confused users, and broken role data — without logging tokens or sensitive payloads.
Field notes
- Hiding links in Twig is usability, not authorization.
- Test negative cases: anonymous user, wrong tenant, inactive account, expired invite.
- Permission strings are contract data; renaming them is a migration problem.
- Firewall order matters: first matching firewall wins.
- The boring target state is narrow authenticators, explicit voters, and few magical role checks.
Was this lesson helpful?