简体   繁体   中英

Symfony: Retrieve user from a different firewall than the firewall being used in the current request

I have a Symfony application that is utilising 2 firewalls. I have logged into both within the same session. When I access a backend firewall page, I can access the user provider object (in my case an Admin entity) but I can't work out how to get access to the frontend firewall's user provider object too from the backend route (so in this case a Customer entity).

An example of a use case is as follows - I have built a CMS style application and I am logged into the backend as and ADMIN to complete some work on specific pages related to the frontend of the website. I then change to the frontend of the website and login as a CUSTOMER to test my work (NOTE: at this stage, I am actually still logged into the backend of the website, but because I am not using an "^/admin" route, it returns to me the CUSTOMER user provider). Now, from this frontend page, I would like to get the ADMIN entity that is logged into the backend of the website so I can check if some frontend page editing can be completed on the page WHILE I am still logged in as the CUSTOMER.

My firewalls look as follows:

security:
    firewalls:
        backend:
            pattern: ^/admin
            anonymous: lazy
            provider: backend_user_provider
            guard:
                authenticators:
                    - App\Security\BackendAuthenticator
            logout:
                path: backend_logout
        frontend:
            anonymous: lazy
            provider: frontend_user_provider
            guard:
                authenticators:
                    - App\Security\FrontendAuthenticator
            logout:
                path: frontend_logout

I cannot find anything on the web to explain how to do this. I have found info on the following, but none of them relate to what I am wanting:

  • Programmatically authenticating to a different firewall
  • Sharing context

It is not the right way to use firewalls.

All you need is to use just one firewall in this case and you will not have any problems.

According to the docs you can share auth between firewalls:

Can I Share Authentication Between Firewalls? Yes, but only with some configuration. If you're using multiple firewalls and you authenticate against one firewall, you will not be authenticated against any other firewalls automatically. Different firewalls are like different security systems. To do this you have to explicitly specify the same Firewall Context for different firewalls. But usually for most applications, having one main firewall is enough.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM