简体   繁体   中英

Symfony2 firewall : keep user logged through subdomains

I created 3 tools for my customers. Each customer has to access to one, two, or three tools, depending on what he paid.

I'm now trying to connect these 3 tools to the same UserBundle. Each tool has its own subdomain : tool1.mysite.com ; tool2.mysite.com and tool3.mysite.com .

I defined 3 roles, 1 for each tool. I kept only one firewall, the main one from the FOSUserBundle, defined on the host .mysite.com in order to cover all subdomains.

My problem is : I can use the login page in any subdomains, but it seems that the logged user is kept only on the subdomains he logged. If I login like "User1" on "Tool1" I won't be logged on Tool2. And if I logged as User2 on Tool2, I'll still be as "User1" on "Tool1".

I don't know how to change this behaviour ?

Thank you so much !

My security.yml is the following :

    firewalls:
        main:
            pattern: ^/
            host: .mysite.com
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:
                path: /logout
                target: /login
            anonymous:    true

access_control:
    - { host: .mysite.com, path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY }
    - { host: .mysite.com, path: ^/admin/, role: ROLE_ADMIN }
    - { host: .mysite.com, path: ^/register, role: ROLE_ADMIN }
    - { host: .mysite.com, path: ^/resetting, role: ROLE_ADMIN }
    - { host: tool1.mysite.com, path: ^/tool1, role: ROLE_TOOL1 }
    - { host: tool2.mysite.com, path: ^/tool2, role: ROLE_TOOL2 }
    - { host: tool3.cospirit.com, path: ^/tool3, role: ROLE_TOOL3 }

role_hierarchy:
    ROLE_TOOL1:  [ROLE_USER]
    ROLE_TOOL2:  [ROLE_USER]
    ROLE_TOOL3:  [ROLE_USER]

It seems to work better by adding this in the config.yml file :

framework:
    session:
        name: SFSESSIDCSMT
        cookie_domain: .mysite.com

I don't know which solution is the better one ?

Because it's different subdomains, and symfony store the login data in cookies, you will have separate data for every subdomains. I would recommend you to create 3 firewalls, and add to all of them the context key, and just add a common value.

firewalls:
        main:
            pattern: ^/
            host: main.mysite.com
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:
                path: /logout
                target: /login
            anonymous:    true
            context: main_context
        second:
            pattern: ^/
            host: first.mysite.com
            form_login:
                provider: fos_userbundle
                csrf_provider: form.csrf_provider
            logout:
                path: /logout
                target: /login
            anonymous:    true
            context: main_context

For this to work, maybe you also have to change the cookie settings for symfony, but I would first try out this solution.

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