Search in sources :

Example 41 with FilterChain

use of jakarta.servlet.FilterChain in project spring-security by spring-projects.

the class SecurityContextPersistenceFilterTests method contextIsClearedAfterChainProceeds.

@Test
public void contextIsClearedAfterChainProceeds() throws Exception {
    final FilterChain chain = mock(FilterChain.class);
    final MockHttpServletRequest request = new MockHttpServletRequest();
    final MockHttpServletResponse response = new MockHttpServletResponse();
    SecurityContextPersistenceFilter filter = new SecurityContextPersistenceFilter();
    SecurityContextHolder.getContext().setAuthentication(this.testToken);
    filter.doFilter(request, response, chain);
    verify(chain).doFilter(any(ServletRequest.class), any(ServletResponse.class));
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
Also used : ServletRequest(jakarta.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(jakarta.servlet.ServletResponse) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 42 with FilterChain

use of jakarta.servlet.FilterChain in project spring-security by spring-projects.

the class DigestAuthenticationFilterTests method executeFilterInContainerSimulator.

private MockHttpServletResponse executeFilterInContainerSimulator(Filter filter, final ServletRequest request, final boolean expectChainToProceed) throws ServletException, IOException {
    final MockHttpServletResponse response = new MockHttpServletResponse();
    final FilterChain chain = mock(FilterChain.class);
    filter.doFilter(request, response, chain);
    verify(chain, times(expectChainToProceed ? 1 : 0)).doFilter(request, response);
    return response;
}
Also used : FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 43 with FilterChain

use of jakarta.servlet.FilterChain in project spring-security by spring-projects.

the class RememberMeAuthenticationFilterTests method testOperationWhenAuthenticationExistsInContextHolder.

@Test
public void testOperationWhenAuthenticationExistsInContextHolder() throws Exception {
    // Put an Authentication object into the SecurityContextHolder
    Authentication originalAuth = new TestingAuthenticationToken("user", "password", "ROLE_A");
    SecurityContextHolder.getContext().setAuthentication(originalAuth);
    // Setup our filter correctly
    RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(mock(AuthenticationManager.class), new MockRememberMeServices(this.remembered));
    filter.afterPropertiesSet();
    // Test
    MockHttpServletRequest request = new MockHttpServletRequest();
    FilterChain fc = mock(FilterChain.class);
    request.setRequestURI("x");
    filter.doFilter(request, new MockHttpServletResponse(), fc);
    // Ensure filter didn't change our original object
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(originalAuth);
    verify(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) Authentication(org.springframework.security.core.Authentication) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 44 with FilterChain

use of jakarta.servlet.FilterChain in project spring-security by spring-projects.

the class RememberMeAuthenticationFilterTests method testOperationWhenNoAuthenticationInContextHolder.

@Test
public void testOperationWhenNoAuthenticationInContextHolder() throws Exception {
    AuthenticationManager am = mock(AuthenticationManager.class);
    given(am.authenticate(this.remembered)).willReturn(this.remembered);
    RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(am, new MockRememberMeServices(this.remembered));
    filter.afterPropertiesSet();
    MockHttpServletRequest request = new MockHttpServletRequest();
    FilterChain fc = mock(FilterChain.class);
    request.setRequestURI("x");
    filter.doFilter(request, new MockHttpServletResponse(), fc);
    // Ensure filter setup with our remembered authentication object
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isSameAs(this.remembered);
    verify(fc).doFilter(any(HttpServletRequest.class), any(HttpServletResponse.class));
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) HttpServletRequest(jakarta.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(jakarta.servlet.http.HttpServletResponse) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 45 with FilterChain

use of jakarta.servlet.FilterChain in project spring-security by spring-projects.

the class RememberMeAuthenticationFilterTests method authenticationSuccessHandlerIsInvokedOnSuccessfulAuthenticationIfSet.

@Test
public void authenticationSuccessHandlerIsInvokedOnSuccessfulAuthenticationIfSet() throws Exception {
    AuthenticationManager am = mock(AuthenticationManager.class);
    given(am.authenticate(this.remembered)).willReturn(this.remembered);
    RememberMeAuthenticationFilter filter = new RememberMeAuthenticationFilter(am, new MockRememberMeServices(this.remembered));
    filter.setAuthenticationSuccessHandler(new SimpleUrlAuthenticationSuccessHandler("/target"));
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    FilterChain fc = mock(FilterChain.class);
    request.setRequestURI("x");
    filter.doFilter(request, response, fc);
    assertThat(response.getRedirectedUrl()).isEqualTo("/target");
    // Should return after success handler is invoked, so chain should not proceed
    verifyZeroInteractions(fc);
}
Also used : AuthenticationManager(org.springframework.security.authentication.AuthenticationManager) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(jakarta.servlet.FilterChain) SimpleUrlAuthenticationSuccessHandler(org.springframework.security.web.authentication.SimpleUrlAuthenticationSuccessHandler) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Aggregations

FilterChain (jakarta.servlet.FilterChain)141 Test (org.junit.jupiter.api.Test)134 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)103 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)102 HttpServletResponse (jakarta.servlet.http.HttpServletResponse)68 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)54 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)35 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)32 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)29 ServletRequest (jakarta.servlet.ServletRequest)25 ServletResponse (jakarta.servlet.ServletResponse)25 Authentication (org.springframework.security.core.Authentication)23 MockFilterChain (org.springframework.mock.web.MockFilterChain)20 ServletException (jakarta.servlet.ServletException)16 StandardCharsets (java.nio.charset.StandardCharsets)16 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)16 IOException (java.io.IOException)15 BeforeEach (org.junit.jupiter.api.BeforeEach)14 FileCopyUtils (org.springframework.util.FileCopyUtils)14 Arrays (java.util.Arrays)11