Can still use application after destroying session or logout

Viewed 320

I am new to keycloak, so there is a high chance I don't understand something going on here. Im currently trying to understand how to use Keycloak, so I have set up a small demo application:

  • Its the application from IntelliJ JavaEE tutorial which runs on the root context path /app
  • I use it dockerized on a Wildfly Server jboss/wildfly:17.0.0.Final, WILDFLY keycloak adapter already installed.
  • Traefik proxy is used to reach the app on the browser using http://localhost/app and keycloak using http://keycloak.localhost
  • Keycloak docker container is reachable from the network using alias keycloak, but I use environment variable KEYCLOAK_FRONTEND_URL=http://keycloak.localhost/auth to set another URL for frontend related stuff
  • I have created a realm javaee-realm and one client for my application called javaee-app. Also, one user called "user" with a role of "user" is configured in Keycloak.
  • When accessing the app in the browser using http://localhost/app, I get redirected to Keycloak where I can login as User "user". I get redirected back to the application afterwards.

The Problem:

When logging out by destroying the session inside Keycloak Dashboard or by accessing http://keycloak.localhost/auth/realms/javaee-realm/protocol/openid-connect/logout the session gets deleted, but I can still use the application. Usually, I would expect to be redirected to Keycloak again. After some time, an error is logged in the application container:

16:09:48,285 ERROR [org.keycloak.adapters.RefreshableKeycloakSecurityContext] (default task-1) Refresh token failure status: 400 {"error":"invalid_grant","error_description":"Stale token"}

Wildfly Adapter Configuration standalone.xml

[...]
<subsystem xmlns="urn:jboss:domain:keycloak:1.2">
      <secure-deployment name="javaee-application.war">
           <realm>javaee-realm</realm>
           <auth-server-url>http://keycloak:8080/auth</auth-server-url>
           <ssl-required>none</ssl-required>
           <resource>javaee-app</resource>
           <principal-attribute>preferred_username</principal-attribute>
           <public-client>true</public-client>
      </secure-deployment>
</subsystem>
[...]

My web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

    <module-name>javaee-application</module-name>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>All other resources</web-resource-name>
            <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
            <role-name>user</role-name>
        </auth-constraint>
    </security-constraint>

    <login-config>
        <auth-method>KEYCLOAK</auth-method>
    </login-config>

    <security-role>
        <role-name>user</role-name>
    </security-role>
</web-app>

Client Configuration in Keycloak

enter image description here

The expected behavior occurs when I delete the JSESSIONID cookie in my browser. So, after deletion, I get redirected to Keycloak when I try to access the application. But obv this is no solution...

Does anyone know what the problem here is precisely?

1 Answers

I'm gonna answer my own question:

In Keycloak, the access token is still valid after logging out even though the session is not active anymore.

It has a default lifetime of 5 minutes I think, so WILDFLY is only going to verify after 5 minutes if the user is still authenticated. As a consequence, the user can still use the application for 5 minutes.

There is probably a way to configure that tokens should be invalidated when the user logs out, but I havent found something like that just yet.

Related