|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2016 the original author or authors. |
| 2 | + * Copyright 2002-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.web.authentication;
|
18 | 18 |
|
| 19 | +import javax.servlet.http.HttpSession; |
| 20 | + |
19 | 21 | import org.junit.jupiter.api.Test;
|
20 | 22 |
|
21 | 23 | import org.springframework.mock.web.MockHttpServletRequest;
|
22 | 24 | import org.springframework.mock.web.MockHttpServletResponse;
|
| 25 | +import org.springframework.security.authentication.BadCredentialsException; |
23 | 26 | import org.springframework.security.core.Authentication;
|
| 27 | +import org.springframework.security.core.AuthenticationException; |
| 28 | +import org.springframework.security.web.WebAttributes; |
24 | 29 |
|
25 | 30 | import static org.assertj.core.api.Assertions.assertThat;
|
26 | 31 | import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
@@ -108,4 +113,20 @@ public void setTargetUrlParameterEmptyTargetUrlParameter() {
|
108 | 113 | assertThatIllegalArgumentException().isThrownBy(() -> ash.setTargetUrlParameter(" "));
|
109 | 114 | }
|
110 | 115 |
|
| 116 | + @Test |
| 117 | + public void shouldRemoveAuthenticationAttributeWhenOnAuthenticationSuccess() throws Exception { |
| 118 | + SimpleUrlAuthenticationSuccessHandler ash = new SimpleUrlAuthenticationSuccessHandler(); |
| 119 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 120 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 121 | + HttpSession session = request.getSession(); |
| 122 | + assertThat(session).isNotNull(); |
| 123 | + session.setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, |
| 124 | + new BadCredentialsException("Invalid credentials")); |
| 125 | + assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).isNotNull(); |
| 126 | + assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)) |
| 127 | + .isInstanceOf(AuthenticationException.class); |
| 128 | + ash.onAuthenticationSuccess(request, response, mock(Authentication.class)); |
| 129 | + assertThat(session.getAttribute(WebAttributes.AUTHENTICATION_EXCEPTION)).isNull(); |
| 130 | + } |
| 131 | + |
111 | 132 | }
|
0 commit comments