|
@@ -25,6 +25,7 @@ import jakarta.servlet.http.HttpServletRequest;
|
|
|
import org.junit.jupiter.api.AfterEach;
|
|
|
import org.junit.jupiter.api.Test;
|
|
|
import org.junit.jupiter.api.extension.ExtendWith;
|
|
|
+import org.mockito.ArgumentCaptor;
|
|
|
import org.mockito.Mock;
|
|
|
import org.mockito.junit.jupiter.MockitoExtension;
|
|
|
|
|
@@ -38,7 +39,9 @@ import org.springframework.security.authentication.AuthenticationManagerResolver
|
|
|
import org.springframework.security.authentication.BadCredentialsException;
|
|
|
import org.springframework.security.authentication.TestingAuthenticationToken;
|
|
|
import org.springframework.security.core.Authentication;
|
|
|
+import org.springframework.security.core.context.SecurityContext;
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
|
|
+import org.springframework.security.web.context.SecurityContextRepository;
|
|
|
import org.springframework.security.web.util.matcher.RequestMatcher;
|
|
|
|
|
|
import static org.assertj.core.api.Assertions.assertThat;
|
|
@@ -256,4 +259,36 @@ public class AuthenticationFilterTests {
|
|
|
assertThat(session.getId()).isNotEqualTo(sessionId);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void filterWhenSuccessfulAuthenticationThenNoSessionCreated() throws Exception {
|
|
|
+ Authentication authentication = new TestingAuthenticationToken("test", "this", "ROLE_USER");
|
|
|
+ given(this.authenticationConverter.convert(any())).willReturn(authentication);
|
|
|
+ given(this.authenticationManager.authenticate(any())).willReturn(authentication);
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
|
|
+ MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
+ FilterChain chain = new MockFilterChain();
|
|
|
+ AuthenticationFilter filter = new AuthenticationFilter(this.authenticationManager,
|
|
|
+ this.authenticationConverter);
|
|
|
+ filter.doFilter(request, response, chain);
|
|
|
+ assertThat(request.getSession(false)).isNull();
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void filterWhenCustomSecurityContextRepositoryAndSuccessfulAuthenticationRepositoryUsed() throws Exception {
|
|
|
+ SecurityContextRepository securityContextRepository = mock(SecurityContextRepository.class);
|
|
|
+ ArgumentCaptor<SecurityContext> securityContextArg = ArgumentCaptor.forClass(SecurityContext.class);
|
|
|
+ Authentication authentication = new TestingAuthenticationToken("test", "this", "ROLE_USER");
|
|
|
+ given(this.authenticationConverter.convert(any())).willReturn(authentication);
|
|
|
+ given(this.authenticationManager.authenticate(any())).willReturn(authentication);
|
|
|
+ MockHttpServletRequest request = new MockHttpServletRequest("GET", "/");
|
|
|
+ MockHttpServletResponse response = new MockHttpServletResponse();
|
|
|
+ FilterChain chain = new MockFilterChain();
|
|
|
+ AuthenticationFilter filter = new AuthenticationFilter(this.authenticationManager,
|
|
|
+ this.authenticationConverter);
|
|
|
+ filter.setSecurityContextRepository(securityContextRepository);
|
|
|
+ filter.doFilter(request, response, chain);
|
|
|
+ verify(securityContextRepository).saveContext(securityContextArg.capture(), eq(request), eq(response));
|
|
|
+ assertThat(securityContextArg.getValue().getAuthentication()).isEqualTo(authentication);
|
|
|
+ }
|
|
|
+
|
|
|
}
|