I need to use https in Symfony 2.2 but only if user is fully authenticated.
How can I force authenticated user to HTTPS in Symfony2 after fully authentication only?
223 Views Asked by Rajesh Meniya At
2
There are 2 best solutions below
0
On
You'll need to create a RequestEventListener, fired after routing and security, to handle in it
1) the security context (see if user is fully authenticated)
2) the actual asked route in the router component (and match it with a configuration array)
to then decide or not to force redirect to https if not already the case.
Just got solution using onKernalRequest event listener.
My onKernelRequest event service :
Register service
services: kernel.listener.forcehttps: class: Acme\DemoBundle\Listener\ForceHttpsListner tags: - { name: kernel.event_listener, event: kernel.request, method: onKernelRequest } arguments: [ @service_container ]access_control in security.yml
security: access_control: - { path: ^/_wdt, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/_profiler, role: IS_AUTHENTICATED_ANONYMOUSLY } - { path: ^/login$, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https } - { path: ^/login_check$, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https } - { path: ^/register, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https } - { path: ^/resetting, role: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https } # Secured part of the site # This config requires being logged for the whole site and having the admin role for the admin part. # Change these rules to adapt them to your needs - { path: ^/admin, role: [ROLE_ADMIN, ROLE_SONATA_ADMIN, ROLE_SUPER_EDITOR] } - { path: ^/.*, role: IS_AUTHENTICATED_ANONYMOUSLY }