Creating a simple custom Keycloak HTTP REST endpoint

I am trying to implement a custom Keycloak endpoint using the Java SPI.

There is an example, created by @dasniko, that seems a good starting point. Thanks @dasniko !

The code compiles, starting Keycloak with this provider also works. But accessing the new REST HTTP GET endpoint results in an HTTP 405.

curl -X GET http://localhost:8080/realms/master/my-rest-resource/hello

{"error":"HTTP 405 Method Not Allowed"}

Is this URL right? It should definitly be a HTTp GET endpoint: keycloak-extensions-demo/rest-endpoint/src/main/java/dasniko/keycloak/resource/MyResourceProvider.java at main · dasniko/keycloak-extensions-demo · GitHub

Enabling Keycloak Debug logs with env var KC_LOG_LEVEL: DEBUG reveals more information:

keycloak-extensions-demo  | 2023-12-18 14:53:55,786 DEBUG [org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext] (executor-thread-1) Restarting handler chain for exception exception: jakarta.ws.rs.WebApplicationException: HTTP 405 Method Not Allowed
keycloak-extensions-demo  | 	at org.jboss.resteasy.reactive.server.handlers.ResourceLocatorHandler.handle(ResourceLocatorHandler.java:71)
keycloak-extensions-demo  | 	at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:150)
keycloak-extensions-demo  | 	at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:145)
keycloak-extensions-demo  | 	at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:576)
keycloak-extensions-demo  | 	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
keycloak-extensions-demo  | 	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
keycloak-extensions-demo  | 	at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
keycloak-extensions-demo  | 	at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
keycloak-extensions-demo  | 	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
keycloak-extensions-demo  | 	at java.base/java.lang.Thread.run(Thread.java:840)
keycloak-extensions-demo  | 
keycloak-extensions-demo  | 2023-12-18 14:53:55,786 DEBUG [org.keycloak.services.error.KeycloakErrorHandler] (executor-thread-1) Error response 405: jakarta.ws.rs.WebApplicationException: HTTP 405 Method Not Allowed
keycloak-extensions-demo  | 	at org.jboss.resteasy.reactive.server.handlers.ResourceLocatorHandler.handle(ResourceLocatorHandler.java:71)
keycloak-extensions-demo  | 	at io.quarkus.resteasy.reactive.server.runtime.QuarkusResteasyReactiveRequestContext.invokeHandler(QuarkusResteasyReactiveRequestContext.java:150)
keycloak-extensions-demo  | 	at org.jboss.resteasy.reactive.common.core.AbstractResteasyReactiveContext.run(AbstractResteasyReactiveContext.java:145)
keycloak-extensions-demo  | 	at io.quarkus.vertx.core.runtime.VertxCoreRecorder$14.runWith(VertxCoreRecorder.java:576)
keycloak-extensions-demo  | 	at org.jboss.threads.EnhancedQueueExecutor$Task.run(EnhancedQueueExecutor.java:2513)
keycloak-extensions-demo  | 	at org.jboss.threads.EnhancedQueueExecutor$ThreadBody.run(EnhancedQueueExecutor.java:1538)
keycloak-extensions-demo  | 	at org.jboss.threads.DelegatingRunnable.run(DelegatingRunnable.java:29)
keycloak-extensions-demo  | 	at org.jboss.threads.ThreadLocalResettingRunnable.run(ThreadLocalResettingRunnable.java:29)
keycloak-extensions-demo  | 	at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30)
keycloak-extensions-demo  | 	at java.base/java.lang.Thread.run(Thread.java:840)


I am using Keycloak 23 as defined in keycloak-extensions-demo/docker-compose.yml at main · dasniko/keycloak-extensions-demo · GitHub


This can be reproduced with:

  1. git clone git@github.com:dasniko/keycloak-extensions-demo.git
  2. cd keycloak-extensions-demo
  3. Enabling Keycloak logs in docker compose with KC_LOG_LEVEL: DEBUG
  4. ./mvnw clean package -DskipTests && docker compose up

Does anyone know what’s going wrong here?

Is anyone here able to successfully run this example rest endpoint code?

There was an error in the code, I just pushed a fix, should work now.

1 Like

Works now perfectly, thanks!

@dasniko - I am seeing this issue on Keycloak 23.0.3, could you confirm which version has your fix please?

My fix was not related to any Keycloak version, it was a bug in my code. It works properly in current versions!

ahh…ok, thanks for confirming!