Login to Keycloak that is behind Spring Cloud Api Gateway

Viewed 1112

I have Keycloak and one Microservice running behind Spring Cloud API Gateway. I want to allow a client application to perform a /login HTTP request to let users login with their user name and password.

Because the client application will use the Password Grant Type, to authenticate with Keycloak via REST API, it will send HTTP Post request to a Keycloak /token endpoint

http://localhost:8080/auth/realms/<realm>/protocol/openid-connect/token

I want this request to go via Spring Cloud API Gateway. So I have configured a route but for some reason, I get 404 Not Found error even though the Keycloak is running and the endpoint is correct. Sending HTTP Post request to the above endpoint directly, without going through API gateway, works. I wonder why do I get a 404 error?

Below is my Spring Cloud API Gateway route configuration:

server.port=8085
spring.application.name=my-gateway-service  

spring.security.oauth2.client.provider.custom.issuer-uri = http://localhost:8080/auth/realms/myrealm
spring.security.oauth2.client.provider.custom.user-name-attribute = preferred_username
spring.security.oauth2.client.provider.custom2.user-info-uri = http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/userinfo

spring.security.oauth2.client.registration.custom.client-name=my-spring-cloud-api-gateway
spring.security.oauth2.client.registration.custom.client-id = my-spring-cloud-api-gateway
spring.security.oauth2.client.registration.custom.client-secret = 3c832be7-2950-4490-801e-276b874d3dd3
spring.security.oauth2.client.registration.custom.scope = profile, openid, roles
spring.security.oauth2.client.registration.custom.authorization-grant-type = password


spring.cloud.gateway.routes[0].id = token-service
spring.cloud.gateway.routes[0].uri = http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token
spring.cloud.gateway.routes[0].predicates[0]=Path=/token-service
spring.cloud.gateway.routes[0].predicates[1]=Method=POST

If I send HTTP Post request via API Gateway, I will get 404 Not found error.

curl --location --request POST 'http://localhost:8085/token-service' \
--header 'Content-Type: application/x-www-form-urlencoded' \
--data-urlencode 'username=username' \
--data-urlencode 'password=mypassword' \
--data-urlencode 'grant_type=password' \
--data-urlencode 'client_id=my-spring-cloud-api-gateway' \
--data-urlencode 'client_secret=3c832be7-2950-4490-801e-276b874d3dd3'

If I configure Spring Cloud API Gateway to route this HTTP Post request to another Microservice running on the localhost, then everything works. If I do not use API Gateway and send the above HTTP Post request directly to Keycloak endpoint http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token, then I get an access token. So Keycloak is running and the OAuth client application is configured to work with Password grant.

The 404 error code takes place only if send the above request via API Gateway and make the Gateway route the request to Keycloak endpoint http://localhost:8080/auth/realms/myrealm/protocol/openid-connect/token

What do I do wrong?

Thank you!

1 Answers

I use the SetPath filter:

spring.cloud.gateway.routes[0].id=authToken
spring.cloud.gateway.routes[0].uri=http://localhost:8080
spring.cloud.gateway.routes[0].predicates[0]=Path=/auth/token
spring.cloud.gateway.routes[0].predicates[0]=Method=POST
spring.cloud.gateway.routes[0].filters[0]=SetPath=/auth/realms/myrealm/protocol/openid-connect/token
Related