Search in sources :

Example 16 with MockFilterConfig

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

the class AbstractAuthenticationProcessingFilterTests method testSuccessLoginThenFailureLoginResultsInSessionLosingToken.

@Test
public void testSuccessLoginThenFailureLoginResultsInSessionLosingToken() throws Exception {
    // Setup our HTTP request
    MockHttpServletRequest request = createMockAuthenticationRequest();
    // Setup our filter configuration
    MockFilterConfig config = new MockFilterConfig(null, null);
    // Setup our expectation that the filter chain will not be invoked, as we redirect
    // to defaultTargetUrl
    MockFilterChain chain = new MockFilterChain(false);
    MockHttpServletResponse response = new MockHttpServletResponse();
    // Setup our test object, to grant access
    MockAuthenticationFilter filter = new MockAuthenticationFilter(true);
    filter.setFilterProcessesUrl("/j_mock_post");
    filter.setAuthenticationSuccessHandler(successHandler);
    // Test
    filter.doFilter(request, response, chain);
    assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp");
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
    assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()).isEqualTo("test");
    // Now try again but this time have filter deny access
    // Setup our HTTP request
    // Setup our expectation that the filter chain will not be invoked, as we redirect
    // to authenticationFailureUrl
    chain = new MockFilterChain(false);
    response = new MockHttpServletResponse();
    // Setup our test object, to deny access
    filter = new MockAuthenticationFilter(false);
    filter.setFilterProcessesUrl("/j_mock_post");
    filter.setAuthenticationFailureHandler(failureHandler);
    // Test
    filter.doFilter(request, response, chain);
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 17 with MockFilterConfig

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

the class AbstractAuthenticationProcessingFilterTests method testLoginErrorWithNoFailureUrlSendsUnauthorizedStatus.

/**
	 * SEC-462
	 */
@Test
public void testLoginErrorWithNoFailureUrlSendsUnauthorizedStatus() throws Exception {
    MockHttpServletRequest request = createMockAuthenticationRequest();
    MockFilterConfig config = new MockFilterConfig(null, null);
    MockFilterChain chain = new MockFilterChain(true);
    MockHttpServletResponse response = new MockHttpServletResponse();
    MockAuthenticationFilter filter = new MockAuthenticationFilter(false);
    successHandler.setDefaultTargetUrl("http://monkeymachine.co.uk/");
    filter.setAuthenticationSuccessHandler(successHandler);
    filter.doFilter(request, response, chain);
    assertThat(response.getStatus()).isEqualTo(HttpServletResponse.SC_UNAUTHORIZED);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 18 with MockFilterConfig

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

the class AbstractAuthenticationProcessingFilterTests method testNoSessionIsCreatedIfAllowSessionCreationIsFalse.

/**
	 * SEC-571
	 */
@Test
public void testNoSessionIsCreatedIfAllowSessionCreationIsFalse() throws Exception {
    MockHttpServletRequest request = createMockAuthenticationRequest();
    MockFilterConfig config = new MockFilterConfig(null, null);
    MockFilterChain chain = new MockFilterChain(true);
    MockHttpServletResponse response = new MockHttpServletResponse();
    // Reject authentication, so exception would normally be stored in session
    MockAuthenticationFilter filter = new MockAuthenticationFilter(false);
    failureHandler.setAllowSessionCreation(false);
    filter.setAuthenticationFailureHandler(failureHandler);
    filter.doFilter(request, response, chain);
    assertThat(request.getSession(false)).isNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 19 with MockFilterConfig

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

the class AbstractAuthenticationProcessingFilterTests method testFilterProcessesUrlVariationsRespected.

@Test
public void testFilterProcessesUrlVariationsRespected() throws Exception {
    // Setup our HTTP request
    MockHttpServletRequest request = createMockAuthenticationRequest();
    request.setServletPath("/j_OTHER_LOCATION");
    request.setRequestURI("/mycontext/j_OTHER_LOCATION");
    // Setup our filter configuration
    MockFilterConfig config = new MockFilterConfig(null, null);
    // Setup our expectation that the filter chain will not be invoked, as we redirect
    // to defaultTargetUrl
    MockFilterChain chain = new MockFilterChain(false);
    MockHttpServletResponse response = new MockHttpServletResponse();
    // Setup our test object, to grant access
    MockAuthenticationFilter filter = new MockAuthenticationFilter(true);
    filter.setFilterProcessesUrl("/j_OTHER_LOCATION");
    filter.setAuthenticationSuccessHandler(successHandler);
    // Test
    filter.doFilter(request, response, chain);
    assertThat(response.getRedirectedUrl()).isEqualTo("/mycontext/logged_in.jsp");
    assertThat(SecurityContextHolder.getContext().getAuthentication()).isNotNull();
    assertThat(SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString()).isEqualTo("test");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Example 20 with MockFilterConfig

use of org.springframework.mock.web.MockFilterConfig in project spring-boot by spring-projects.

the class DelegatingFilterProxyRegistrationBeanTests method initShouldNotCauseEarlyInitialization.

@Test
public void initShouldNotCauseEarlyInitialization() throws Exception {
    this.applicationContext.registerBeanDefinition("mockFilter", new RootBeanDefinition(MockFilter.class));
    DelegatingFilterProxyRegistrationBean registrationBean = createFilterRegistrationBean();
    Filter filter = registrationBean.getFilter();
    filter.init(new MockFilterConfig());
    assertThat(mockFilterInitialized.get()).isNull();
    filter.doFilter(new MockHttpServletRequest(), new MockHttpServletResponse(), new MockFilterChain());
    assertThat(mockFilterInitialized.get()).isEqualTo(true);
}
Also used : Filter(javax.servlet.Filter) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) MockFilterChain(org.springframework.mock.web.MockFilterChain) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) MockFilterConfig(org.springframework.mock.web.MockFilterConfig) Test(org.junit.Test)

Aggregations

MockFilterConfig (org.springframework.mock.web.MockFilterConfig)22 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)19 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)19 Test (org.junit.Test)12 MockFilterChain (org.springframework.mock.web.MockFilterChain)11 MockServletContext (org.springframework.mock.web.MockServletContext)9 WebStatFilter (com.alibaba.druid.support.http.WebStatFilter)8 Map (java.util.Map)5 List (java.util.List)4 WebAppStat (com.alibaba.druid.support.http.stat.WebAppStat)3 ServletException (javax.servlet.ServletException)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 MockHttpSession (org.springframework.mock.web.MockHttpSession)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 MockDatabase (org.opennms.core.test.db.MockDatabase)2 DaoTestConfigBean (org.opennms.test.DaoTestConfigBean)2 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)2 MockServletConfig (org.springframework.mock.web.MockServletConfig)2 OpenSessionInViewFilter (org.springframework.orm.hibernate3.support.OpenSessionInViewFilter)2 UserService (com.alibaba.druid.stat.spring.UserService)1