Search code examples
springspring-securityoauth-2.0netflix-zuulgateway

Authorizing requests through spring gateway with zool via oauth server


My application has microservices behind (spring) gateway with zuul proxy. There is also internal (spring) oauth2 authorization server. I want to implement client_credentials grant type for my microservices calls from outside - for M2M communication.

When I configure for the gateway client_id and client_secret in its application.yml requests come through the gateway but there is no requester check - oauth authorizes the gateway itself, as a result there is no authorization at all. I could use authorization code grant type, but then it would require web-client authorization which (web client) user might not have.

If I request authentication token from the oauth microservice, I get correct token for this app.

How can I force the gateway use the requester's client_id and client_secret to get token from oauth? - e.g. I can provide them as basic authorization via header. Or can I provide to the gateway the token obtained by the requester from oauth?

The question is very similar to another one: Implementing authentication and authorization using Zuul Proxy, Oauth2 on REST Microservices except the thing that there might be no web client, but an external microservice.


Solution

  • I have answered the question Implementing authentication and authorization using Zuul Proxy, Oauth2 on REST Microservices.

    In my case the most important thing was to configure zuul proxy to forward authorization header to downstream services. Initially I thought about using zuul filters, but solution was much simpler - just configure sensitive headers for zuul:

    server:
      port: 8080
    zuul:
      sensitiveHeaders: Cookie,Set-Cookie # <--- this line
      routes:
        spring-security-oauth-resource:
          path: /spring-security-oauth-resource/**
          url: http://localhost:8081/spring-security-oauth-resource
        oauth:
          path: /oauth/**
          url: http://localhost:8083/spring-security-oauth-server/oauth
    
    

    After successful authentication of a client/user with oauth JWT token will be forwarded to downstream by the gateway. Certainly, for this gateway must allow unathenticated access to oauth resource and require authentication for all others.

    More details on the topics can be found in the article https://www.baeldung.com/spring-security-zuul-oauth-jwt