symfony/security-http
Symfony Security HTTP integrates the Security Core with HTTP: firewalls, authenticators, and request/response handling to protect parts of your app and authenticate users. Install via composer require symfony/security-http.
security-http offers fine-grained control over authentication flows (e.g., OAuth, CAS, LDAP, JWT) that may not be natively available in Laravel’s default stack.#[IsGranted], #[CurrentUser]).security-http relies on kernel-level middleware (firewalls), which can be emulated in Laravel using:
web, api).AuthenticateMiddleware, CsrfTokenMiddleware).auth middleware can be extended to delegate to Symfony’s AuthenticatorManager.UserProvider can be wrapped to integrate with Symfony’s UserProviderInterface.league/oauth2-client for OAuth).#[IsGranted]) can be mapped to Laravel’s policy system or custom middleware.| Risk Area | Assessment | Mitigation |
|---|---|---|
| Middleware Conflicts | Laravel’s middleware pipeline may conflict with Symfony’s firewall logic. | Use priority-based middleware ordering and decorator pattern for seamless integration. |
| Authentication Overhead | Symfony’s AuthenticatorManager is more complex than Laravel’s AuthManager. |
Abstract Symfony’s logic behind a Laravel-compatible facade (e.g., SymfonyAuthManager). |
| CSRF & Session Handling | Symfony’s CsrfTokenManager differs from Laravel’s CsrfGuard. |
Implement a dual-layer CSRF system (Laravel’s for forms, Symfony’s for API). |
| Performance Impact | Symfony’s security layer adds request overhead (e.g., token validation, firewall checks). | Cache firewall configurations and optimize authenticator pipelines. |
| Deprecation Risks | Symfony 8.x deprecates some features (e.g., RememberMeDetails FQCN). |
Audit Laravel’s auth stack for compatibility and plan migrations (e.g., replace FQCN with UID). |
AuthenticatorManager in Laravel’s test suite?| Laravel Component | Symfony security-http Equivalent |
Integration Strategy |
|---|---|---|
| Laravel Middleware | Firewall Middleware (FirewallContext) |
Wrap Symfony middleware in Laravel’s Handle class. |
| Laravel Auth Controller | AbstractAuthenticator |
Extend Symfony’s AuthenticatorInterface for Laravel’s LoginController. |
| Laravel Policies | VoterInterface |
Map #[IsGranted] to Laravel’s authorize() method. |
| Laravel Sanctum/JWT | OidcAuthenticator, JwtAuthenticator |
Use Symfony’s token handlers for JWT/OIDC validation. |
| Laravel Session | SessionAuthenticationStrategy |
Delegate session handling to Symfony’s SessionStorage. |
| Laravel CSRF | CsrfTokenManager |
Hybrid approach: Use Laravel’s CSRF for forms, Symfony’s for APIs. |
Phase 1: Authentication Layer
AuthenticatesUsers trait with a Symfony Authenticator.use Symfony\Component\Security\Http\Authenticator\AbstractAuthenticator;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
class LaravelSymfonyAuthenticator extends AbstractAuthenticator {
public function supports(Request $request): ?bool {
return $request->isMethod('POST') && $request->request->has('_symfony_token');
}
public function authenticate(Request $request): Passport {
$credentials = $request->request->all();
$user = LaravelUserProvider::findByCredentials($credentials);
return new Passport(new UserToken($user), ['ROLE_USER']);
}
}
auth.attempt to trigger Symfony’s authenticator.Phase 2: Authorization Layer
AccessControlList.use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;
class SymfonyPolicyDecorator implements CanAuthorize {
public function authorize(Request $request, $ability, $model) {
return $this->authorizationChecker->isGranted($ability, $model);
}
}
Phase 3: Firewall & Middleware
FirewallMap in Laravel’s Kernel.php:
protected function registerFirewalls(): array {
return [
'api' => [
new Firewall('api', [
new FormLoginAuthenticator(),
new CsrfTokenManager(),
]),
],
];
}
middlewareGroups to route requests to Symfony’s firewalls.| Compatibility Check | Status | Notes |
|---|---|---|
| Laravel 10.x + Symfony 8.x | ✅ Compatible | Both use PHP 8.1+, PSR-15 middleware. |
Laravel’s auth() Helper |
⚠️ Partial | Requires custom facade to delegate to Symfony’s SecurityContext. |
Laravel’s Session |
✅ Compatible | Symfony’s SessionStorage can integrate with Laravel’s session driver. |
Laravel’s Request Object |
✅ Compatible | Symfony’s RequestStack can use Laravel’s Request. |
Laravel’s Event System |
⚠️ Custom Binding | Symfony’s SecurityEvents must be mapped to Laravel’s events (e.g., auth.attempted). |
DebugListener for auth events.FormLoginAuthenticator.OidcAuthenticator.#[IsGranted] attributes.AuthManager in favor of Symfony’s AuthenticatorManager.| Aspect | Impact | Mitigation |
|---|---|---|
| Dependency Updates | Symfony’s **security patches |
How can I help you explore Laravel packages today?