{"action":"created","issue":{"url":"https://api.github.com/repos/symfony/symfony/issues/11836","labels_url":"https://api.github.com/repos/symfony/symfony/issues/11836/labels{/name}","comments_url":"https://api.github.com/repos/symfony/symfony/issues/11836/comments","events_url":"https://api.github.com/repos/symfony/symfony/issues/11836/events","html_url":"https://github.com/symfony/symfony/issues/11836","id":41805195,"number":11836,"title":"Sharing security context across multiple firewalls","user":{"login":"seb0zz","id":1436535,"avatar_url":"https://avatars.githubusercontent.com/u/1436535?v=3","gravatar_id":"","url":"https://api.github.com/users/seb0zz","html_url":"https://github.com/seb0zz","followers_url":"https://api.github.com/users/seb0zz/followers","following_url":"https://api.github.com/users/seb0zz/following{/other_user}","gists_url":"https://api.github.com/users/seb0zz/gists{/gist_id}","starred_url":"https://api.github.com/users/seb0zz/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/seb0zz/subscriptions","organizations_url":"https://api.github.com/users/seb0zz/orgs","repos_url":"https://api.github.com/users/seb0zz/repos","events_url":"https://api.github.com/users/seb0zz/events{/privacy}","received_events_url":"https://api.github.com/users/seb0zz/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/symfony/symfony/labels/Security","name":"Security","color":"DDDDDD"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2014-09-03T11:14:56Z","updated_at":"2015-01-16T18:21:02Z","closed_at":null,"body":"Analogous to http://symfony.com/doc/current/reference/configuration/security.html#reference-security-firewall-context I tried to use the same security context across two firewalls. The problem is if I login behind the `api_secured` firewall I am still not allowed to visit sites that are secured by the `main` firewall. \r\n\r\nI also tried to check stackoverflow. Seems that other people already had the same problem: \r\nhttp://stackoverflow.com/questions/11801622/how-to-authenticate-on-2-different-symfony2-firewalls-at-the-same-time\r\n\r\nMy configuration looks like:\r\n\r\n    firewalls:\r\n        api_secured:\r\n            context: my_security_context\r\n            pattern: ^/api/.*\r\n            api:\r\n                check_path: /api/security/login\r\n        main:\r\n            context: my_security_context\r\n            pattern: ^/\r\n            remember_me:\r\n                key: %secret%\r\n                name: MYREMEMBERME_%kernel.environment%\r\n                remember_me_parameter: remember_me\r\n                always_remember_me: true\r\n            form_login:\r\n                provider: fos_userbundle_provider\r\n                csrf_provider: form.csrf_provider\r\n                login_path: /login\r\n                use_forward: false\r\n                check_path: /login_check\r\n                default_target_path: /\r\n                failure_path: null\r\n                success_handler: my.security.authentication_success_handler\r\n                failure_handler: my.security.authentication_failure_chain_handler\r\n                remember_me: true\r\n            logout:\r\n                path: /logout\r\n                target: /\r\n                csrf_provider: form.csrf_provider\r\n                csrf_parameter: token\r\n            anonymous: true\r\n\r\n    access_control:\r\n        - { path: ^/api/security/login$, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/admin, roles: ROLE_AUTHENTICATED_REMEMBERED }\r\n        ..."},"comment":{"url":"https://api.github.com/repos/symfony/symfony/issues/comments/70297456","html_url":"https://github.com/symfony/symfony/issues/11836#issuecomment-70297456","issue_url":"https://api.github.com/repos/symfony/symfony/issues/11836","id":70297456,"user":{"login":"marcoshoya","id":6484905,"avatar_url":"https://avatars.githubusercontent.com/u/6484905?v=3","gravatar_id":"","url":"https://api.github.com/users/marcoshoya","html_url":"https://github.com/marcoshoya","followers_url":"https://api.github.com/users/marcoshoya/followers","following_url":"https://api.github.com/users/marcoshoya/following{/other_user}","gists_url":"https://api.github.com/users/marcoshoya/gists{/gist_id}","starred_url":"https://api.github.com/users/marcoshoya/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/marcoshoya/subscriptions","organizations_url":"https://api.github.com/users/marcoshoya/orgs","repos_url":"https://api.github.com/users/marcoshoya/repos","events_url":"https://api.github.com/users/marcoshoya/events{/privacy}","received_events_url":"https://api.github.com/users/marcoshoya/received_events","type":"User","site_admin":false},"created_at":"2015-01-16T18:21:02Z","updated_at":"2015-01-16T18:21:02Z","body":"Hi all,\r\n\r\nI've followed the configuration above to develop my system and did some adjusts (that I do not know if that is fine). In my case, I have four firewalls (adm, customer, provider, website) and I would like to share just with two: customer and website.\r\n\r\nit is working when I executes the program! However, my Unit test does not work.\r\nFollowing instructions from http://symfony.com/doc/current/cookbook/testing/simulating_authentication.html, when I try do request to URL \"/cliente\", my token was returned \"Anonymous\"\r\n\r\nCould you guys help me?\r\n\r\nThis is my security yml file:\r\n```\r\nsecurity:\r\n    encoders:\r\n        Marcoshoya\\MarquejogoBundle\\Entity\\AdmUser:\r\n            algorithm:        sha1\r\n            encode_as_base64: false\r\n            iterations:       1\r\n\r\n    role_hierarchy:\r\n        ROLE_ADMIN:       ROLE_USER\r\n\r\n    providers:\r\n        admin:\r\n            entity: { class: MarcoshoyaMarquejogoBundle:AdmUser, property: username }\r\n        provider:\r\n            entity: { class: MarcoshoyaMarquejogoBundle:Provider, property: email }\r\n        customer:\r\n            entity: { class: MarcoshoyaMarquejogoBundle:Customer, property: email }\r\n\r\n    firewalls:\r\n        site:\r\n            context: website\r\n            pattern: ^/(?!(fornecedor|cliente|adm)).*$\r\n            anonymous: true\r\n            \r\n        admin:\r\n            anonymous: ~\r\n            pattern:    ^/adm/.*\r\n            form_login:\r\n                provider: admin\r\n                check_path: /adm/login_check\r\n                login_path: /adm/login\r\n                default_target_path: /adm\r\n\r\n        customer:\r\n            anonymous: ~\r\n            context: website\r\n            pattern:    ^/cliente/.*\r\n            form_login:\r\n                provider: customer\r\n                check_path: /cliente/login_check\r\n                login_path: /cliente/login\r\n                default_target_path: /cliente\r\n                \r\n        provider:\r\n            anonymous: ~\r\n            pattern:    ^/fornecedor/.*\r\n            form_login:\r\n                provider: provider\r\n                check_path: /fornecedor/login_check\r\n                login_path: /fornecedor/login\r\n                default_target_path: /fornecedor\r\n\r\n    access_control:\r\n        - { path: ^/adm/doLogin, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/adm/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/adm, roles: ROLE_ADMIN }\r\n        - { path: ^/cliente/cadastrar, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/cliente/doRegister, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/cliente/doLogin, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/cliente/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/cliente, roles: [ROLE_ADMIN, ROLE_CUSTOMER] }\r\n        - { path: ^/fornecedor/doLogin, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/fornecedor/login, roles: IS_AUTHENTICATED_ANONYMOUSLY }\r\n        - { path: ^/fornecedor, roles: [ROLE_ADMIN, ROLE_PROVIDER] }\r\n```\r\nThis is my unit test\r\n\r\n```\r\n<?php\r\n\r\nnamespace Marcoshoya\\MarquejogoBundle\\Tests\\Controller\\Customer;\r\n\r\nuse Symfony\\Bundle\\FrameworkBundle\\Test\\WebTestCase;\r\nuse Symfony\\Component\\BrowserKit\\Cookie;\r\nuse Symfony\\Component\\Security\\Core\\Authentication\\Token\\UsernamePasswordToken;\r\n\r\nclass DashboardTest extends WebTestCase\r\n{\r\n    /**\r\n     * @var \\Symfony\\Bundle\\FrameworkBundle\\Client\r\n     */\r\n    protected $client = null;\r\n    \r\n    /**\r\n     * @var \\Doctrine\\ORM\\EntityManager\r\n     */\r\n    protected $em = null;\r\n    \r\n    /**\r\n     * Setup functions\r\n     */\r\n    public function setUp()\r\n    {\r\n        $this->client = static::createClient();\r\n        \r\n        $this->em = static::$kernel->getContainer()\r\n            ->get('doctrine')\r\n            ->getManager()\r\n        ;\r\n    }\r\n    \r\n    /**\r\n     * Make login on admin\r\n     */\r\n    protected function logIn()\r\n    {\r\n        $session = $this->client->getContainer()->get('session');\r\n\r\n        $firewall = 'customer';\r\n        $token = new UsernamePasswordToken('customer', null, $firewall, array('ROLE_CUSTOMER'));\r\n        $this->client->getContainer()->get('security.context')->setToken($token);\r\n        \r\n        $session->set('_security_'.$firewall, serialize($token));\r\n        $session->save();\r\n\r\n        $cookie = new Cookie($session->getName(), $session->getId());\r\n        $this->client->getCookieJar()->set($cookie);\r\n    }\r\n    \r\n    /**\r\n     * General test for dashboard\r\n     */\r\n    public function testDashboard()\r\n    {\r\n        $this->logIn();\r\n        $crawler = $this->client->request('GET', '/cliente/');\r\n        \r\n        var_dump($this->client->getContainer()->get('security.context')->getToken());\r\n\r\n    }\r\n}\r\n```"}}