From 229391d48b9612f9f4798e3b89f79e7732f71b4e Mon Sep 17 00:00:00 2001 From: mposolda Date: Mon, 3 Nov 2014 12:59:40 +0100 Subject: [PATCH] Fix servlet logout on wildfly/undertow --- .../undertow/ServletSessionTokenStore.java | 14 ++++++++------ .../keycloak/testsuite/adapter/AdapterTest.java | 2 +- .../adapter/CookieTokenStoreAdapterTest.java | 3 ++- .../testsuite/adapter/CustomerServlet.java | 2 +- .../testsuite/adapter/RelativeUriAdapterTest.java | 1 + 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletSessionTokenStore.java b/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletSessionTokenStore.java index 3dccf8c1dd..70f0beebeb 100644 --- a/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletSessionTokenStore.java +++ b/integration/undertow/src/main/java/org/keycloak/adapters/undertow/ServletSessionTokenStore.java @@ -89,12 +89,14 @@ public class ServletSessionTokenStore implements AdapterTokenStore { req.removeAttribute(KeycloakSecurityContext.class.getName()); HttpSession session = req.getSession(false); if (session == null) return; - KeycloakUndertowAccount account = (KeycloakUndertowAccount)session.getAttribute(KeycloakUndertowAccount.class.getName()); - if (account == null) return; - session.removeAttribute(KeycloakSecurityContext.class.getName()); - session.removeAttribute(KeycloakUndertowAccount.class.getName()); - if (account.getKeycloakSecurityContext() != null) { - account.getKeycloakSecurityContext().logout(deployment); + try { + KeycloakUndertowAccount account = (KeycloakUndertowAccount) session.getAttribute(KeycloakUndertowAccount.class.getName()); + if (account == null) return; + session.removeAttribute(KeycloakSecurityContext.class.getName()); + session.removeAttribute(KeycloakUndertowAccount.class.getName()); + } catch (IllegalStateException ise) { + // Session may be already logged-out in case that app has adminUrl + log.debugf("Session %s logged-out already", session.getId()); } } diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java index cca4389041..e17208dc50 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/AdapterTest.java @@ -215,7 +215,7 @@ public class AdapterTest { // test logout driver.navigate().to("http://localhost:8081/customer-portal/logout"); - + Assert.assertTrue(driver.getPageSource().contains("servlet logout ok")); driver.navigate().to("http://localhost:8081/customer-portal"); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CookieTokenStoreAdapterTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CookieTokenStoreAdapterTest.java index e76d892173..24b04c92ed 100644 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CookieTokenStoreAdapterTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CookieTokenStoreAdapterTest.java @@ -113,6 +113,7 @@ public class CookieTokenStoreAdapterTest { assertLogged(); driver.navigate().to("http://localhost:8081/customer-portal/logout"); + Assert.assertTrue(driver.getPageSource().contains("servlet logout ok")); driver.navigate().to("http://localhost:8081/customer-portal"); Assert.assertTrue(driver.getCurrentUrl().startsWith(LOGIN_URL)); @@ -167,7 +168,7 @@ public class CookieTokenStoreAdapterTest { private void logoutFromCustomerCookiePortal() { driver.navigate().to("http://localhost:8081/customer-cookie-portal/logout"); - Assert.assertTrue(driver.getPageSource().contains("ok")); + Assert.assertTrue(driver.getPageSource().contains("servlet logout ok")); Assert.assertNull(driver.manage().getCookieNamed(AdapterConstants.KEYCLOAK_ADAPTER_STATE_COOKIE)); driver.navigate().to("http://localhost:8081/customer-cookie-portal"); Assert.assertTrue(driver.getCurrentUrl().startsWith(LOGIN_URL)); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CustomerServlet.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CustomerServlet.java index a32bc1711c..8a57d2f3f8 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CustomerServlet.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/CustomerServlet.java @@ -28,7 +28,7 @@ public class CustomerServlet extends HttpServlet { PrintWriter pw = resp.getWriter(); if (req.getRequestURI().toString().endsWith("logout")) { resp.setStatus(200); - pw.println("ok"); + pw.println("servlet logout ok"); // Call logout before pw.flush req.logout(); diff --git a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/RelativeUriAdapterTest.java b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/RelativeUriAdapterTest.java index f1e1c0b956..32b34a3958 100755 --- a/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/RelativeUriAdapterTest.java +++ b/testsuite/integration/src/test/java/org/keycloak/testsuite/adapter/RelativeUriAdapterTest.java @@ -173,6 +173,7 @@ public class RelativeUriAdapterTest { // test logout driver.navigate().to("http://localhost:8081/customer-portal/logout"); + Assert.assertTrue(driver.getPageSource().contains("servlet logout ok")); driver.navigate().to("http://localhost:8081/customer-portal"); String currentUrl = driver.getCurrentUrl();