Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony login via link into different firewall

Tags:

symfony

I have two firewalls in my Symfony (5.4) application.

What I want is to create a login link for another firewall (where the login_link is configured) while logged in into the other firewall. Currently the system doesn’t allow that (No Symfony\Bundle\SecurityBundle\LoginLink\LoginLinkHandler found for this firewall. Did you forget to add a "login_link" key under your "admin_area" firewall?)

Is there a way to tell the login link creator to create the link for a specific firewall? (I didn’t see it in the implementation so I don’t really know).

like image 773
Pointi Avatar asked Nov 15 '25 23:11

Pointi


1 Answers

UPDATE 2022-06-10: Beginning with Symfony 6.1, just use the new Autowire-Attribute like this:

public function myLoginLinkAction(
    User $myUser,
    #[Autowire(service: 'security.authenticator.login_link_handler.my_other_firewall')] 
    LoginLinkHandlerInterface $myOtherFirewallLoginLinkHandler,
): Response {
    // …
    $loginLinkDetails = $myOtherFirewallLoginLinkHandler->createLoginLink($myUser);
    // …
}

For older Symfony versions:

The solution is to inject the concrete link handler service for my other firewall using an alias defined in security.yaml (where the other firewall that we want to build login links for is named "my_other_firewall"):

services:
    # define a concrete alias for the login link handler of the
    # my_other_firewall firewall to avoid the FirewallAwareLoginLinkHandler
    # that always uses the current request's firewall
    Symfony\Component\Security\Http\LoginLink\LoginLinkHandlerInterface $myOtherFirewallLoginLinkHandler: '@security.authenticator.login_link_handler.my_other_firewall'
    

Then, when I inject the LoginLinkHandlerInterface to my login link building controller, I use the defined parameter name $myOtherFirewallLoginLinkHandler and get the correct LoginLinkHandler injected instead of the FirewallAwareLoginLinkHandler that only exists to use the LoginLinkHandler defined for the firewall of the current request.

This solves the problem the documented way to use concrete implementations, when more than one implements a certain interface.

like image 151
spackmat Avatar answered Nov 18 '25 20:11

spackmat



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!