|
21 | 21 | import java.util.Collection; |
22 | 22 | import java.util.List; |
23 | 23 |
|
| 24 | +import org.jspecify.annotations.Nullable; |
24 | 25 | import org.junit.jupiter.api.Test; |
25 | 26 |
|
26 | 27 | import org.springframework.context.MessageSource; |
@@ -162,6 +163,20 @@ void detailsAreSetOnAuthenticationTokenIfNotAlreadySetByProvider() { |
162 | 163 | assertThat(result.getDetails()).isSameAs(details); |
163 | 164 | } |
164 | 165 |
|
| 166 | + // gh-18027 |
| 167 | + @Test |
| 168 | + void authenticationIsSameWhenDetailsSetAndAuthenticationToBuilderIsDefault() { |
| 169 | + Authentication customAuthentication = new DefaultToBuilderAuthentication(); |
| 170 | + AuthenticationProvider provider = mock(AuthenticationProvider.class); |
| 171 | + given(provider.supports(any())).willReturn(true); |
| 172 | + given(provider.authenticate(any())).willReturn(customAuthentication); |
| 173 | + TestingAuthenticationToken request = createAuthenticationToken(); |
| 174 | + request.setDetails(new Object()); |
| 175 | + ProviderManager authMgr = new ProviderManager(provider); |
| 176 | + Authentication result = authMgr.authenticate(request); |
| 177 | + assertThat(result).isSameAs(customAuthentication); |
| 178 | + } |
| 179 | + |
165 | 180 | @Test |
166 | 181 | void authenticationExceptionIsIgnoredIfLaterProviderAuthenticates() { |
167 | 182 | Authentication result = new TestingAuthenticationToken("user", "pass", "FACTOR"); |
@@ -356,4 +371,48 @@ public boolean supports(Class<?> authentication) { |
356 | 371 |
|
357 | 372 | } |
358 | 373 |
|
| 374 | + /** |
| 375 | + * Represents a custom {@link Authentication} that does not override |
| 376 | + * {@link #toBuilder()}. We should remain passive to previous versions of Spring |
| 377 | + * Security and not change the {@link Authentication} type. |
| 378 | + */ |
| 379 | + private static final class DefaultToBuilderAuthentication implements Authentication { |
| 380 | + |
| 381 | + @Override |
| 382 | + public Collection<? extends GrantedAuthority> getAuthorities() { |
| 383 | + return List.of(); |
| 384 | + } |
| 385 | + |
| 386 | + @Override |
| 387 | + public @Nullable Object getCredentials() { |
| 388 | + return null; |
| 389 | + } |
| 390 | + |
| 391 | + @Override |
| 392 | + public @Nullable Object getDetails() { |
| 393 | + return null; |
| 394 | + } |
| 395 | + |
| 396 | + @Override |
| 397 | + public @Nullable Object getPrincipal() { |
| 398 | + return null; |
| 399 | + } |
| 400 | + |
| 401 | + @Override |
| 402 | + public boolean isAuthenticated() { |
| 403 | + return false; |
| 404 | + } |
| 405 | + |
| 406 | + @Override |
| 407 | + public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { |
| 408 | + |
| 409 | + } |
| 410 | + |
| 411 | + @Override |
| 412 | + public String getName() { |
| 413 | + return ""; |
| 414 | + } |
| 415 | + |
| 416 | + } |
| 417 | + |
359 | 418 | } |
0 commit comments