Spring Cloud Gateway: Combined security? (X.509 + HTTP headers)

336 Views Asked by At

I'm building an API gateway using Spring Cloud Gateway and I'd like to secure it.

In our current architecture the API gateway will be hidden behind a portal which authenticates using a client certificate (issued for a system user) and sends the name of the real user calling the service in an HTTP header.

I'm looking for a way to configure Spring Security to both verify the certificate (so that nobody else can call the GW) but at the same time construct the principal from the provided header (after the cert check succeeds).

If I use the standard x509 from Spring Security (with provided subjectPrincipalRegex) the user service receives just the matching part as the user ID so I can construct the Principal from the username in the cert (which is still the same).

I.e. I'm looking for something like this

  • No client cert or invalid client cert => anonymous user (still can have access to some routes)
  • Valid certificate => obtain UserDetails based on the HTTP header (which can still result in user not found in the DB, i.e. anonymous access).

Note: Since Spring Cloud Gateway is reactive I'm looking for solution applicable to Spring WebFlux Security but I believe that the concepts are the same as in "standard non-WebFlux" security

0

There are 0 best solutions below