30

I have two firewalls:

  1. api (for API calls)
  2. main (for everything else)

My client app login happens via the main firewall. However, it does interact with endpoints under the api firewall to fetch data. The problem here is that I don't want to force the user to log in a second time for authenticating against the second firewall.

How can I authenticate against both firewalls with just a single login form?

CSchulz
  • 10,882
  • 11
  • 60
  • 114
anushr
  • 3,342
  • 3
  • 29
  • 50

1 Answers1

60

Perhaps you could try the 'context' firewall property.

Say you have a configuration something like this (which presumably you do):

security:
    // providers etc ...

    firewall:
        main:
            pattern: # ...
            provider: my_users
            http_basic: ~
        api:
            pattern: # ...
            provider: my_users
            http_basic: ~

In this case the user's session will contain a '_security_main' property after authenticating against the 'main' firewall, and then when they attempt to access an 'api' location they will be prompted to re-auth and will then gain a '_security_api' session property.

To prevent this re-prompt, you can add the 'context' property to each firewall definition you wish to share the same authentication - so:

security:
    # providers etc ...

    firewall:
        main:
            pattern: # ...
            provider: my_users
            http_basic: ~
            context: primary_auth  # new
        api:
            pattern: # ...
            provider: my_users
            http_basic: ~
            context: primary_auth  # new

In this case, upon authentication with the 'main' firewall, a '_security_primary_auth' property will be set in the user's session. Any subsequent requests inside the 'api' firewill will then use the value of '_security_primary_auth' to establish authentication status (and so the user will appear authenticated).

Of course this authentication context sharing will work both ways around (whether they auth first with the 'main' or the 'api' firewall) - if you only wanted transience in one direction, things would be more complex.

Hope this helps.

jstephenson
  • 2,170
  • 14
  • 14
  • 1
    Nice, I was looking for this for long time ! – pmoubed Jun 22 '12 at 23:29
  • 1
    I have exactly the same problem, the only difference is "main" firewall authenticates using a log in form instead of http_basic, so, the 'api' firewall prompts the user to log in again even when the user authenticated via log in form. Is there any way to make it work? – ButterDog Aug 03 '12 at 18:27
  • Hi, perfect, it works, good new year start!!! (where could I have learned about _context_ in the docs?) – mario Jan 02 '17 at 12:23
  • 1
    What if the firewall `api` has `stateless` set to `true`? AFAIK the `context` property works only if `stateless` is set to `false`. One firewall might have `form_login` and the other `jwt`. How would you make this work? – cezar Dec 27 '21 at 20:57