Skip to content

UsageTrackingTokenStorage deprecation is Security.php #43042

Closed
@r3dge

Description

@r3dge

Symfony version(s) affected: 5.3.7

Description
After using $security->getUser() in a service as mentioned in the documentation (https://symfony.com/doc/5.4/security.html#a-fetching-the-user-object) i get a deprecation warning :

"Since symfony/security-core 5.3: Using "Symfony\Component\Security\Core\Authentication\Token\Storage\UsageTrackingTokenStorage" (service ID: "security.token_storage") outside the request-response cycle is deprecated, use the "Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorage" class (service ID: "security.untracked_token_storage") instead or disable usage tracking using "disableUsageTracking()""

How to reproduce

To reproduce this issue, create a service that use Security and call $security->getUser() :

`use Symfony\Component\Security\Core\Security;

class MyService{

private $security;

public function __construct(Security $security){
    $this->security=$security;
}

public function myFunction(){
    $user = $this->security->getUser();
}

}`

Possible Solution

I find out that Security.php of the core security component is using a deprecated service :

public function getToken(): ?TokenInterface { return $this->container->get('security.token_storage')->getToken(); }

As mentioned in the deprecation notice it seems that the service "security.token_storage" should be replaced by "security.untracked_token_storage".

Thanks for your feedback !

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions