diff --git a/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandler.java b/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandler.java index d843aa7d41..27178ca2ad 100644 --- a/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandler.java +++ b/integration/spring-security/src/main/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandler.java @@ -32,8 +32,11 @@ public class KeycloakLogoutHandler implements LogoutHandler { @Override public void logout(HttpServletRequest request, HttpServletResponse response, Authentication authentication) { - - if (!KeycloakAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { + if (authentication == null) { + log.warn("Cannot log out without authentication"); + return; + } + else if (!KeycloakAuthenticationToken.class.isAssignableFrom(authentication.getClass())) { log.warn("Cannot log out a non-Keycloak authentication: {}", authentication); return; } diff --git a/integration/spring-security/src/test/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandlerTest.java b/integration/spring-security/src/test/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandlerTest.java index 2ee32af85e..6a035b5c78 100644 --- a/integration/spring-security/src/test/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandlerTest.java +++ b/integration/spring-security/src/test/java/org/keycloak/adapters/springsecurity/authentication/KeycloakLogoutHandlerTest.java @@ -88,6 +88,12 @@ public class KeycloakLogoutHandlerTest { verifyZeroInteractions(session); } + @Test + public void testLogoutNullAuthentication() throws Exception { + keycloakLogoutHandler.logout(request, response, null); + verifyZeroInteractions(session); + } + @Test public void testHandleSingleSignOut() throws Exception { keycloakLogoutHandler.handleSingleSignOut(request, response, keycloakAuthenticationToken);