From 3d7e22a4e92be5d8bc91e3eb7628047223920b57 Mon Sep 17 00:00:00 2001 From: twosom Date: Wed, 8 Mar 2023 23:09:17 +0900 Subject: [PATCH 1/2] Add test to SimpleUrlAuthenticationSuccessHandlerTests --- ...eUrlAuthenticationSuccessHandlerTests.java | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java b/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java index 904b54eb34..29633dd494 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2023 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,11 +16,16 @@ package org.springframework.security.web.authentication; +import javax.servlet.http.HttpSession; + import org.junit.jupiter.api.Test; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; +import org.springframework.security.authentication.BadCredentialsException; import org.springframework.security.core.Authentication; +import org.springframework.security.core.AuthenticationException; +import org.springframework.security.web.WebAttributes; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; @@ -108,4 +113,20 @@ public class SimpleUrlAuthenticationSuccessHandlerTests { assertThatIllegalArgumentException().isThrownBy(() -> ash.setTargetUrlParameter(" ")); } + @Test + public void shouldRemoveAuthenticationAttributeWhenOnAuthenticationSuccess() throws Exception { + SimpleUrlAuthenticationSuccessHandler ash = new SimpleUrlAuthenticationSuccessHandler(); + MockHttpServletRequest request = new MockHttpServletRequest(); + MockHttpServletResponse response = new MockHttpServletResponse(); + HttpSession session = request.getSession(); + assertThat(session).isNotNull(); + session.setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, + new BadCredentialsException("Invalid credentials")); + assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).isNotNull(); + assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)) + .isInstanceOf(AuthenticationException.class); + ash.onAuthenticationSuccess(request, response, mock(Authentication.class)); + assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).isNull(); + } + } From 3fbb64db967d89df8ac48fd800a6c6d2c6e9053d Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Mon, 20 Mar 2023 16:28:52 -0600 Subject: [PATCH 2/2] Fix javax package --- .../SimpleUrlAuthenticationSuccessHandlerTests.java | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java b/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java index 29633dd494..347d3b3f5d 100644 --- a/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java +++ b/web/src/test/java/org/springframework/security/web/authentication/SimpleUrlAuthenticationSuccessHandlerTests.java @@ -16,8 +16,7 @@ package org.springframework.security.web.authentication; -import javax.servlet.http.HttpSession; - +import jakarta.servlet.http.HttpSession; import org.junit.jupiter.api.Test; import org.springframework.mock.web.MockHttpServletRequest;