New in Symfony 6.2: Security Improvements (Part 1)

Simpler Programmatic Login

Contributed by
Arnaud Frézet and Robin Chalas
in #41274.

Logging in users programmatically is a common need in many applications. That's why in Symfony 6.2 we're adding a login() method to the Security service. On any service or controller, you can now do this:

use Symfony\Component\Security\Core\Security;
// ...

class SomeService
{
    public function __construct(
        private Security $security,
    ) {
    }

    public function someMethod()
    {
        // fetch a UserInterface object somehow (e.g. from a database)
        $user = ...

        // login the user programmatically
        $this->security->login($user);

        // if you have many authenticators associated to the current firewall,
        // you must pass explicitly the name of authenticator to use
        $this->security->login($user, 'form_login');
        $this->security->login($user, SomeApiKeyAuthenticator::class);

        // ...
    }
}

Custom Target URL When Impersonating Users

Contributed by
Antoine Makdessi
in #46338.

Similar to the feature that allows to configure the target URL after login, in Symfony 6.2 we're adding a new feature to allow you configure the target URL after impersonating a user. To do so, define the new target_url option under the switch_user option of your firewall:

# config/packages/security.yaml
security:
    # ...

    firewalls:
        main:
            # ...
            switch_user:
                # ...
                target_url: https://example.com/...

Contributed by
Mathias Brodala
in #46567.

When using login links to implement passwordless authentication, the lifetime of those links is configured globally for all. In Symfony 6.2 we're adding a feature so you can configure the lifetime per link. Use the third optional argument of createLoginLink() to override the global lifetime with a new custom value (in seconds):

// this login link will have a lifetime of 60 seconds
$loginLinkDetails = $loginLinkHandler->createLoginLink($user, null, 60);
$loginLink = $loginLinkDetails->getUrl();

Multiple User Checkers per Firewall

Contributed by
Michael Babker
in #46064.

User checkers allow you to define additional checks performed during the authentication of a user, to verify if the identified user is allowed to log in. You can only apply one user checker per firewall, which makes it harder to share logic.

Imagine an application that has two firewalls (e.g. API and traditional web login) and needs to apply these checkers: for both firewalls, check that the user account is not disabled; for the API firewall, check also that user has API access.

In Symfony 6.2 we're introducing a new "chained user checker" feature so you can call multiple user checkers for a firewall. To do so, apply to each user checker the tags corresponding to the firewall where it applies (tags follow the pattern security.user_checker.).

In Symfony 6.2, the previous example can be solved as follows:

namespace App\Security\User;

use Symfony\Component\DependencyInjection\Attribute\Autoconfigure;
use Symfony\Component\Security\Core\User\UserCheckerInterface;

#[Autoconfigure(tags: [['security.user_checker.main' => ['priority' => 10]]])]
#[Autoconfigure(tags: [['security.user_checker.api' => ['priority' => 10]]])]
final class DisabledAccountUserChecker implements UserCheckerInterface
{
    // ...
}

#[Autoconfigure(tags: [['security.user_checker.api' => ['priority' => 5]]])]
final class ApiAccessAllowedUserChecker implements UserCheckerInterface
{
    // ...
}
            <hr style="margin-bottom: 5px" />
            <div style="font-size: 90%">
                <a href="https://symfony.com/sponsor">Sponsor</a> the Symfony project.
            </div>

https://symfony.com/blog/new-in-symfony-6-2-security-improvements-part-1?utm_source=Symfony%20Blog%20Feed&utm_medium=feed

Creado 2y | 23 nov. 2022 17:20:06


Inicia sesión para agregar comentarios

Otros mensajes en este grupo.

SymfonyLive Paris 2025 : Tirez profit de Messenger pour améliorer votre architecture

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here.

Al

28 feb. 2025 10:31:15 | Symfony
SymfonyLive Berlin 2025: Building really fast applications

SymfonyLive Berlin 2025, conference held in English, will take place from April 1 to 4! The schedule is being revealed gradually. More details are available here. 🚨 Enjoy the last few days bef

27 feb. 2025 16:10:03 | Symfony
SymfonyLive Paris 2025 :  Async avec Messenger, AMQP et Mercure

SymfonyLive Paris 2025, conference in French language only, will take place from March 27 to 28! The schedule is currently being revealed as we go along. More details are available here. 🚨 Tod

26 feb. 2025 14:31:31 | Symfony
Symfony 6.4.19 released

Symfony 6.4.19 has just been released. Here is the list of the most important changes since 6.4.18:

bug #59198 [Messenger] Filter out non-consumable receivers when registering ConsumeMessagesComm
26 feb. 2025 12:20:03 | Symfony
Symfony 7.2.4 released

Symfony 7.2.4 has just been released. Here is the list of the most important changes since 7.2.3:

bug #59198 [Messenger] Filter out non-consumable receivers when registering ConsumeMessagesComman
26 feb. 2025 12:20:03 | Symfony
Just one month to go before SymfonyLive Paris 2025 workshops begin!

SymfonyLive Paris 2025, conference in French language only, will already start in 1 month with the workshops! Have a look on the topics and join us! Schedule details are available here.

📣

25 feb. 2025 15:20:33 | Symfony
New Core Team Members, 2025 Edition

A few weeks ago, I had the pleasure of announcing the formation of the Symfony UX Core Team, a dedicated group working to enhance the frontend development experience within the Symfony ecosystem. Toda

24 feb. 2025 16:20:03 | Symfony