Search in sources :

Example 76 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class RequestAttributeAuthenticationFilterTests method rejectsMissingHeader.

@Test(expected = PreAuthenticatedCredentialsNotFoundException.class)
public void rejectsMissingHeader() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
    filter.doFilter(request, response, chain);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestAttributeAuthenticationFilter(org.springframework.security.web.authentication.preauth.RequestAttributeAuthenticationFilter) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 77 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class RequestAttributeAuthenticationFilterTests method credentialsAreRetrievedIfHeaderNameIsSet.

@Test
public void credentialsAreRetrievedIfHeaderNameIsSet() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockFilterChain chain = new MockFilterChain();
    RequestAttributeAuthenticationFilter filter = new RequestAttributeAuthenticationFilter();
    filter.setAuthenticationManager(createAuthenticationManager());
    filter.setCredentialsEnvironmentVariable("myCredentialsVariable");
    request.setAttribute("REMOTE_USER", "cat");
    request.setAttribute("myCredentialsVariable", "catspassword");
    filter.doFilter(request, response, chain);
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
    assertThat(SecurityContextHolder.getContext().getAuthentication().getCredentials()).isEqualTo("catspassword");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RequestAttributeAuthenticationFilter(org.springframework.security.web.authentication.preauth.RequestAttributeAuthenticationFilter) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 78 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class JaasApiIntegrationFilterTests method assertJaasSubjectEquals.

// ~ Helper Methods
// ====================================================================================================
private void assertJaasSubjectEquals(final Subject expectedValue) throws Exception {
    MockFilterChain chain = new MockFilterChain() {

        public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
            // See if the subject was updated
            Subject currentSubject = Subject.getSubject(AccessController.getContext());
            assertThat(currentSubject).isEqualTo(expectedValue);
            // run so we know the chain was executed
            super.doFilter(request, response);
        }
    };
    filter.doFilter(request, response, chain);
    // ensure that the chain was actually invoked
    assertThat(chain.getRequest()).isNotNull();
}
Also used : ServletRequest(javax.servlet.ServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) ServletResponse(javax.servlet.ServletResponse) MockFilterChain(org.springframework.mock.web.MockFilterChain) Subject(javax.security.auth.Subject)

Example 79 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class SessionManagementFilterTests method responseIsRedirectedToTimeoutUrlIfSetAndSessionIsInvalid.

@Test
public void responseIsRedirectedToTimeoutUrlIfSetAndSessionIsInvalid() throws Exception {
    SecurityContextRepository repo = mock(SecurityContextRepository.class);
    // repo will return false to containsContext()
    SessionAuthenticationStrategy strategy = mock(SessionAuthenticationStrategy.class);
    SessionManagementFilter filter = new SessionManagementFilter(repo, strategy);
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setRequestedSessionId("xxx");
    request.setRequestedSessionIdValid(false);
    MockHttpServletResponse response = new MockHttpServletResponse();
    filter.doFilter(request, response, new MockFilterChain());
    assertThat(response.getRedirectedUrl()).isNull();
    // Now set a redirect URL
    request = new MockHttpServletRequest();
    request.setRequestedSessionId("xxx");
    request.setRequestedSessionIdValid(false);
    SimpleRedirectInvalidSessionStrategy iss = new SimpleRedirectInvalidSessionStrategy("/timedOut");
    iss.setCreateNewSession(true);
    filter.setInvalidSessionStrategy(iss);
    FilterChain fc = mock(FilterChain.class);
    filter.doFilter(request, response, fc);
    verifyZeroInteractions(fc);
    assertThat(response.getRedirectedUrl()).isEqualTo("/timedOut");
}
Also used : SessionAuthenticationStrategy(org.springframework.security.web.authentication.session.SessionAuthenticationStrategy) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) FilterChain(javax.servlet.FilterChain) MockFilterChain(org.springframework.mock.web.MockFilterChain) SecurityContextRepository(org.springframework.security.web.context.SecurityContextRepository) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Example 80 with MockFilterChain

use of org.springframework.mock.web.MockFilterChain in project spring-security by spring-projects.

the class SessionManagementFilterTests method strategyIsNotInvokedIfAuthenticationIsNull.

@Test
public void strategyIsNotInvokedIfAuthenticationIsNull() throws Exception {
    SecurityContextRepository repo = mock(SecurityContextRepository.class);
    SessionAuthenticationStrategy strategy = mock(SessionAuthenticationStrategy.class);
    SessionManagementFilter filter = new SessionManagementFilter(repo, strategy);
    HttpServletRequest request = new MockHttpServletRequest();
    filter.doFilter(request, new MockHttpServletResponse(), new MockFilterChain());
    verifyZeroInteractions(strategy);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpServletRequest(javax.servlet.http.HttpServletRequest) SessionAuthenticationStrategy(org.springframework.security.web.authentication.session.SessionAuthenticationStrategy) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) SecurityContextRepository(org.springframework.security.web.context.SecurityContextRepository) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.Test)

Aggregations

MockFilterChain (org.springframework.mock.web.MockFilterChain)108 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)106 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)106 Test (org.junit.Test)77 ServletRequest (javax.servlet.ServletRequest)28 ServletResponse (javax.servlet.ServletResponse)28 IOException (java.io.IOException)24 ServletException (javax.servlet.ServletException)24 HttpServletResponse (javax.servlet.http.HttpServletResponse)22 NestedServletException (org.springframework.web.util.NestedServletException)19 Before (org.junit.Before)17 ErrorPage (org.springframework.boot.web.server.ErrorPage)15 HttpServletResponseWrapper (javax.servlet.http.HttpServletResponseWrapper)14 MockFilterConfig (org.springframework.mock.web.MockFilterConfig)11 AuthenticationManager (org.springframework.security.authentication.AuthenticationManager)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)10 MockHttpSession (org.springframework.mock.web.MockHttpSession)9 MockServletContext (org.springframework.mock.web.MockServletContext)9 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8