Search code examples
javaplayframeworkkeycloak

SocketTimeoutException with Keycloak and Play Framework


I am developping a Play! application using Keycloak for authentification purposes. I am moving my Keycloak instance from one server to another (Say server A to server B). Everything works fine with Keycloak on server A, but using Keycloak on server B, I get the following error each time I try to connect to the App :

play.api.UnexpectedException: Unexpected exception[RuntimeException: org.pac4j.core.exception.TechnicalException:
java.net.SocketTimeoutException: connect timed out]
        at play.api.http.HttpErrorHandlerExceptions$.throwableToUsefulException(HttpErrorHandler.scala:247)
        at play.api.http.HttpErrorHandlerExceptions.throwableToUsefulException(HttpErrorHandler.scala)
        at play.http.DefaultHttpErrorHandler.throwableToUsefulException(DefaultHttpErrorHandler.java:204)
        at play.http.DefaultHttpErrorHandler.onServerError(DefaultHttpErrorHandler.java:164)
        at play.core.j.JavaHttpErrorHandlerAdapter.$anonfun$onServerError$1(JavaHttpErrorHandlerAdapter.scala:22)
        at play.core.j.JavaHelpers.$anonfun$invokeWithContext$1(JavaHelpers.scala:273)
        at play.core.j.JavaHelpers.withContext(JavaHelpers.scala:284)
        at play.core.j.JavaHelpers.withContext$(JavaHelpers.scala:280)
        at play.core.j.JavaHelpers$.withContext(JavaHelpers.scala:293)
        at play.core.j.JavaHelpers.invokeWithContext(JavaHelpers.scala:272)

Interestingly, when I launch the app locally in dev mode, everything works just fine, however, the error happens when I launch the App on dev mode (on server B).

I cannot find anything on the net relating to this issue. Has anyone ever encountered it ?

I am using Keycloak 6.0.1


Solution

  • It turns out that the problem came from the TimeZone configuration of Java on my server. It was different from the system's TimeZone (which incidentally was also Keycloak's time zone).

    This difference in time zones created an artificial lag between the request from the client and the response from the server, leading it to believe that the socket has been waiting for an hour (or more, depending on what gap there is between the time zones).

    The solution I found is to set the TimeZone parameter to the system's time zone when launching my app by setting -Duser.timezone=CET in the JAVA_OPTS.