3
votes

I'm using spring stack (Spring Boot 2.0.0.RELEASE) for creating a site that delegues user authentication/registration to Google via OAuth2. It is implemented as a few Spring Cloud microservices with a Zuul gateway running on port 8080.

Google Auth Server


     Zuul Gateway(:8080)
    /        \
   /          \
  /            \
  Other          OAuth2 Client Microservice(:8000)
Microservices   

I use Google as an OAuth2 server, and use spring-security-oauth2 as a client, which is implemented as a separate microservice. If all my cloud is deployed at localhost everything works fine. But if my microservices are deployed at different machines, e.g. Docker, OAuth2 login doesn't work. getting invalid redirect Uri parameter error.

Zuul configuration for Docker:

  zuul:
     ignoredServices: '*'
     host:
      connection-timeout-millis: 20000
      socket-timeout-millis: 20000   routes:
    authserver_oauth:
      path: /oauth2/**
      serviceId: authserver
      stripPrefix: false
      sensitiveHeaders:

Eureka Configuration for Docker:

eureka:
  instance:
    prefer-ip-address: false
  client:
   serviceUrl:
     defaultZone: http://eureka-server:8761/eureka/

Authserver configuration:

spring:
 security:
  oauth2:
    client:
      registration:
        google:
          clientId: ***
          clientSecret: ***
          redirectUriTemplate: "{baseUrl}/oauth2/callback/{registrationId}"
          scope:
            - email
            - profile

When I debug for Oauth2 condition I am getting unequal uri for request and response:

   authorizationResponse.getRedirectUri()
   (java.lang.String) http://authserver:8080/oauth2/callback/google
   authorizationRequest.getRedirectUri()
   (java.lang.String) http://localhost:8080/oauth2/callback/google
1
Hi @Ashok, did you already find a solution for this problem? I am currently struggling with the same situation. - GlabbichRulz

1 Answers

0
votes

I had a similar issue with Spring boot 2.2.2RELEASE with a cloud environment (NGINX, Docker, Cubernettes) where customising NGINX configuration was not an option at all. Tried methods like setting the x-forwarded-for property, mocking the prod with docker, NGINX setup (https://juplo.de/how-to-redirect-to-spring-security-oauth2-behind-a-gateway-proxy-hiding-the-app-behind-a-reverse-proxy-gateway/) but nothing works. Finally I updated my Spring boot version to 2.4.1 and it worked without any other configuration.