|
33 | 33 | import org.springframework.mock.web.MockHttpSession;
|
34 | 34 | import org.springframework.security.authentication.AuthenticationManager;
|
35 | 35 | import org.springframework.security.authentication.BadCredentialsException;
|
| 36 | +import org.springframework.security.authentication.TestingAuthenticationToken; |
36 | 37 | import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
37 | 38 | import org.springframework.security.core.Authentication;
|
38 | 39 | import org.springframework.security.core.authority.AuthorityUtils;
|
|
55 | 56 | import static org.mockito.Mockito.never;
|
56 | 57 | import static org.mockito.Mockito.spy;
|
57 | 58 | import static org.mockito.Mockito.verify;
|
| 59 | +import static org.mockito.Mockito.verifyNoMoreInteractions; |
58 | 60 |
|
59 | 61 | /**
|
60 | 62 | * Tests {@link BasicAuthenticationFilter}.
|
@@ -410,4 +412,80 @@ public void requestWhenSecurityContextRepository() throws Exception {
|
410 | 412 | assertThat(contextArg.getValue().getAuthentication().getName()).isEqualTo("rod");
|
411 | 413 | }
|
412 | 414 |
|
| 415 | + @Test |
| 416 | + public void doFilterWhenUsernameDoesNotChangeThenAuthenticationIsNotRequired() throws Exception { |
| 417 | + SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder.getContextHolderStrategy(); |
| 418 | + SecurityContext securityContext = securityContextHolderStrategy.createEmptyContext(); |
| 419 | + Authentication authentication = UsernamePasswordAuthenticationToken.authenticated("rod", "koala", |
| 420 | + AuthorityUtils.createAuthorityList("USER")); |
| 421 | + securityContext.setAuthentication(authentication); |
| 422 | + securityContextHolderStrategy.setContext(securityContext); |
| 423 | + |
| 424 | + String token = "rod:koala"; |
| 425 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 426 | + request.addHeader("Authorization", "Basic " + CodecTestUtils.encodeBase64(token)); |
| 427 | + FilterChain filterChain = mock(FilterChain.class); |
| 428 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 429 | + this.filter.doFilter(request, response, filterChain); |
| 430 | + assertThat(response.getStatus()).isEqualTo(200); |
| 431 | + |
| 432 | + verify(this.manager, never()).authenticate(any(Authentication.class)); |
| 433 | + verify(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); |
| 434 | + verifyNoMoreInteractions(this.manager, filterChain); |
| 435 | + } |
| 436 | + |
| 437 | + @Test |
| 438 | + public void doFilterWhenUsernameChangesThenAuthenticationIsRequired() throws Exception { |
| 439 | + SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder.getContextHolderStrategy(); |
| 440 | + SecurityContext securityContext = securityContextHolderStrategy.createEmptyContext(); |
| 441 | + Authentication authentication = UsernamePasswordAuthenticationToken.authenticated("user", "password", |
| 442 | + AuthorityUtils.createAuthorityList("USER")); |
| 443 | + securityContext.setAuthentication(authentication); |
| 444 | + securityContextHolderStrategy.setContext(securityContext); |
| 445 | + |
| 446 | + String token = "rod:koala"; |
| 447 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 448 | + request.addHeader("Authorization", "Basic " + CodecTestUtils.encodeBase64(token)); |
| 449 | + FilterChain filterChain = mock(FilterChain.class); |
| 450 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 451 | + this.filter.doFilter(request, response, filterChain); |
| 452 | + assertThat(response.getStatus()).isEqualTo(200); |
| 453 | + |
| 454 | + ArgumentCaptor<Authentication> authenticationCaptor = ArgumentCaptor.forClass(Authentication.class); |
| 455 | + verify(this.manager).authenticate(authenticationCaptor.capture()); |
| 456 | + verify(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); |
| 457 | + verifyNoMoreInteractions(this.manager, filterChain); |
| 458 | + |
| 459 | + Authentication authenticationRequest = authenticationCaptor.getValue(); |
| 460 | + assertThat(authenticationRequest).isInstanceOf(UsernamePasswordAuthenticationToken.class); |
| 461 | + assertThat(authenticationRequest.getName()).isEqualTo("rod"); |
| 462 | + } |
| 463 | + |
| 464 | + @Test |
| 465 | + public void doFilterWhenUsernameChangesAndNotUsernamePasswordAuthenticationTokenThenAuthenticationIsRequired() |
| 466 | + throws Exception { |
| 467 | + SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder.getContextHolderStrategy(); |
| 468 | + SecurityContext securityContext = securityContextHolderStrategy.createEmptyContext(); |
| 469 | + Authentication authentication = new TestingAuthenticationToken("user", "password", "USER"); |
| 470 | + securityContext.setAuthentication(authentication); |
| 471 | + securityContextHolderStrategy.setContext(securityContext); |
| 472 | + |
| 473 | + String token = "rod:koala"; |
| 474 | + MockHttpServletRequest request = new MockHttpServletRequest(); |
| 475 | + request.addHeader("Authorization", "Basic " + CodecTestUtils.encodeBase64(token)); |
| 476 | + FilterChain filterChain = mock(FilterChain.class); |
| 477 | + MockHttpServletResponse response = new MockHttpServletResponse(); |
| 478 | + this.filter.doFilter(request, response, filterChain); |
| 479 | + assertThat(response.getStatus()).isEqualTo(200); |
| 480 | + |
| 481 | + ArgumentCaptor<Authentication> authenticationCaptor = ArgumentCaptor.forClass(Authentication.class); |
| 482 | + verify(this.manager).authenticate(authenticationCaptor.capture()); |
| 483 | + verify(filterChain).doFilter(any(ServletRequest.class), any(ServletResponse.class)); |
| 484 | + verifyNoMoreInteractions(this.manager, filterChain); |
| 485 | + |
| 486 | + Authentication authenticationRequest = authenticationCaptor.getValue(); |
| 487 | + assertThat(authenticationRequest).isInstanceOf(UsernamePasswordAuthenticationToken.class); |
| 488 | + assertThat(authenticationRequest.getName()).isEqualTo("rod"); |
| 489 | + } |
| 490 | + |
413 | 491 | }
|
0 commit comments