Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony2 behind ELB is redirecting to http instead of https

Issue:

  • User logs in with https://example.com/login
  • Authentication is approved
  • As configured in security.yml Symfony2 redirects user to profile page after login.
  • But it redirects them to the wrong url http://example.com/homepage

security.yml:

security:      encoders:         FOS\UserBundle\Model\UserInterface: sha512      role_hierarchy:         ROLE_ADMIN:       ROLE_USER         ROLE_SUPER_ADMIN: [ROLE_USER, ROLE_ADMIN, ROLE_ALLOWED_TO_SWITCH]      providers:         fos_userbundle:             id: fos_user.user_provider.username_email      firewalls:         main:             pattern:    ^/             form_login:                 check_path: /login_check                 login_path: /login                 default_target_path: /profile                 provider: fos_userbundle             logout:                 path:   /logout                 target: /splash             anonymous: ~      access_control:         - { roles: ROLE_USER, requires_channel: https }         - { path: ^/login, roles: IS_AUTHENTICATED_ANONYMOUSLY, requires_channel: https }      acl:         connection: default 

Environment Architecture:

enter image description here

The Server1 and Server2 holds Symfony2 application.

Question:

How to force Symfony to generate redirect URL with https protocol instead http?

So far I have looked at these docs, and the solution didn't work work in my case:

  • http://symfony.com/doc/current/cookbook/routing/scheme.html
like image 936
TeaCupApp Avatar asked May 13 '14 00:05

TeaCupApp


1 Answers

Take a look at

vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/Request.php

AWS ELB's use HTTP_X_FORWARDED_PROTO and HTTP_X_FORWARDED_PORT while Symfony looks the X_FORWARDED_PROTO and X_FORWARDED_PORT headers to judge the connection and its secure status.

You can try changing those keys in the trustedHeaders although I would not recommend directly changing them but finding a way to override those.

protected static $trustedHeaders = array(         self::HEADER_CLIENT_IP    => 'X_FORWARDED_FOR',         self::HEADER_CLIENT_HOST  => 'X_FORWARDED_HOST',         self::HEADER_CLIENT_PROTO => 'HTTP_X_FORWARDED_PROTO',         self::HEADER_CLIENT_PORT  => 'HTTP_X_FORWARDED_PORT',     ); 

Reference - http://docs.aws.amazon.com/ElasticLoadBalancing/latest/DeveloperGuide/TerminologyandKeyConcepts.html#x-forwarded-for

like image 154
A23 Avatar answered Sep 21 '22 05:09

A23