symfony/security-core
Symfony Security Core provides the foundation for authentication tokens, roles, voters, role hierarchies, and access decision management. Use it to build flexible authorization logic decoupled from user providers and integrate fine-grained access checks into apps.
symfony/security-core package provides a standalone authentication/authorization framework that aligns well with Laravel’s middleware-based security model. Its voter-based access control (e.g., RoleVoter, AuthenticatedVoter) can be mapped to Laravel’s gates/policies or custom middleware.RoleHierarchy class enables inherited roles (e.g., ROLE_ADMIN → ROLE_USER), which can replace Laravel’s manual role checks or extend its built-in Gate system.UsernamePasswordToken and AuthenticationTrustResolver abstractions allow for custom token implementations, useful for API tokens, OAuth, or session-based auth in Laravel.AccessDecisionManager aggregates voters, enabling composite authorization rules (e.g., "Allow if authenticated OR has role ROLE_AUDITOR").Auth facade already uses token-based auth; symfony/security-core can replace or augment Laravel’s UserProvider/Guard system.PersistentToken (for "remember me") can integrate with Laravel’s session driver.http-foundation (Laravel already includes this).AccessDecisionManager vs. implicit gates). Requires rewriting authorization logic or creating adapters.Auth system entirely would require rewiring middleware, session handling, and user providers.symfony/security-core only for authorization (e.g., voters) is lower-risk.PersistentToken with custom user classes).User model map to Symfony’s UserInterface? (May need a decorator.)PersistentToken conflict with Laravel’s session driver?AuthenticationTrustResolver interact with Laravel’s AuthenticatingMiddleware?symfony/http-foundation, symfony/options-resolver, or symfony/dependency-injection, this will feel native.| Phase | Action | Risk |
|---|---|---|
| 1. Proof of Concept | Replace one authorization layer (e.g., gates for admin routes) with Symfony voters. | Low |
| 2. Hybrid Integration | Use Symfony voters alongside Laravel gates (via middleware). | Medium |
| 3. Full Auth Rewrite | Replace Auth facade, guards, and session handling with Symfony’s system. |
High |
| 4. Testing | Validate token serialization, role hierarchy, and edge cases (e.g., impersonation). | Medium |
AuthenticationToken ↔ Auth::user().// app/Providers/AuthServiceProvider.php
public function boot(): void
{
$symfonyAuth = new SymfonyAuthAdapter();
Auth::shouldUse($symfonyAuth);
}
PersistentToken can store in Laravel’s session, but may need custom serialization.auth middleware can be replaced with Symfony’s AuthenticationListener.// Before (Laravel)
Gate::define('edit-post', function (User $user, Post $post) {
return $user->isAdmin();
});
// After (Symfony)
$accessDecisionManager->addVoter(new CallbackVoter(
fn (TokenInterface $token, string $attribute, mixed $subject) =>
$token->getUser()->isAdmin()
));
AuthenticationProvider alongside Laravel’s guards.PersistentToken for "remember me" cookies.Auth facade in favor of Symfony’s AuthenticationManager.ROLE_SUPER_ADMIN → ROLE_ADMIN → ROLE_USER).AccessDecisionManager can be mocked easily in PHPUnit.UserInterface).UsernamePasswordToken plays well with Laravel’s session driver.RoleHierarchy caches roles; clear cache if roles change dynamically.PersistentToken is cookie-based; ensure it doesn’t bloat session storage.RoleHierarchy and AccessDecisionManager can be cached for high-traffic apps.| Failure Scenario | Impact | Mitigation |
|---|---|---|
| Token Deserialization Failure | Users logged out unexpectedly. | Validate PersistentToken serialization. |
| Role Hierarchy Misconfiguration | Incorrect permissions granted. | Test with RoleHierarchy::getReachableRoleNames(). |
| Middleware Conflict | Auth short-circuits incorrectly. | Use AuthenticationListener after Laravel’s auth middleware. |
| PHP 8.1+ Incompatibility | App crashes on older PHP. | Upgrade or use a polyfill (e.g., symfony/polyfill). |
| Voter Deadlock | Circular dependencies in voters. | Limit voter recursion depth. |
UserInterface, VoterInterface).How can I help you explore Laravel packages today?