Search in sources :

Example 11 with ServletContext

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

the class SessionManagementConfigTests method requestWhenCreateSessionIsSetToIfRequiredThenCreatesSessionOnLogin.

@Test
public void requestWhenCreateSessionIsSetToIfRequiredThenCreatesSessionOnLogin() throws Exception {
    this.spring.configLocations(xml("CreateSessionIfRequired")).autowire();
    ServletContext servletContext = this.mvc.getDispatcherServlet().getServletContext();
    // @formatter:off
    MockHttpServletRequest request = post("/login").param("username", "user").param("password", "password").buildRequest(servletContext);
    // @formatter:on
    request = csrf().postProcessRequest(request);
    MockHttpServletResponse response = request(request, this.spring.getContext());
    assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_MOVED_TEMPORARILY);
    assertThat(request.getSession(false)).isNotNull();
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ServletContext(jakarta.servlet.ServletContext) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) Test(org.junit.jupiter.api.Test)

Example 12 with ServletContext

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

the class AccessControlListTagTests method childContext.

@Test
public void childContext() throws Exception {
    ServletContext servletContext = this.pageContext.getServletContext();
    WebApplicationContext wac = (WebApplicationContext) servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    servletContext.removeAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
    servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
    Object domainObject = new Object();
    given(this.pe.hasPermission(this.bob, domainObject, "READ")).willReturn(true);
    this.tag.setDomainObject(domainObject);
    this.tag.setHasPermission("READ");
    this.tag.setVar("allowed");
    assertThat(this.tag.getDomainObject()).isSameAs(domainObject);
    assertThat(this.tag.getHasPermission()).isEqualTo("READ");
    assertThat(this.tag.doStartTag()).isEqualTo(Tag.EVAL_BODY_INCLUDE);
    assertThat((Boolean) this.pageContext.getAttribute("allowed")).isTrue();
}
Also used : ServletContext(jakarta.servlet.ServletContext) MockServletContext(org.springframework.mock.web.MockServletContext) WebApplicationContext(org.springframework.web.context.WebApplicationContext) Test(org.junit.jupiter.api.Test)

Example 13 with ServletContext

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

the class WebTestUtils method findFilter.

@SuppressWarnings("unchecked")
static <T extends Filter> T findFilter(HttpServletRequest request, Class<T> filterClass) {
    ServletContext servletContext = request.getServletContext();
    Filter springSecurityFilterChain = getSpringSecurityFilterChain(servletContext);
    if (springSecurityFilterChain == null) {
        return null;
    }
    List<Filter> filters = ReflectionTestUtils.invokeMethod(springSecurityFilterChain, "getFilters", request);
    if (filters == null) {
        return null;
    }
    for (Filter filter : filters) {
        if (filterClass.isAssignableFrom(filter.getClass())) {
            return (T) filter;
        }
    }
    return null;
}
Also used : CsrfFilter(org.springframework.security.web.csrf.CsrfFilter) SecurityContextPersistenceFilter(org.springframework.security.web.context.SecurityContextPersistenceFilter) Filter(jakarta.servlet.Filter) ServletContext(jakarta.servlet.ServletContext)

Example 14 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenCustomDispatcherWebApplicationContextSuffixThenUses.

@Test
public void onStartupWhenCustomDispatcherWebApplicationContextSuffixThenUses() {
    ServletContext context = mock(ServletContext.class);
    FilterRegistration.Dynamic registration = mock(FilterRegistration.Dynamic.class);
    ArgumentCaptor<DelegatingFilterProxy> proxyCaptor = ArgumentCaptor.forClass(DelegatingFilterProxy.class);
    given(context.addFilter(eq("springSecurityFilterChain"), proxyCaptor.capture())).willReturn(registration);
    new AbstractSecurityWebApplicationInitializer() {

        @Override
        protected String getDispatcherWebApplicationContextSuffix() {
            return "dispatcher";
        }
    }.onStartup(context);
    DelegatingFilterProxy proxy = proxyCaptor.getValue();
    assertThat(proxy.getContextAttribute()).isEqualTo("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher");
    assertThat(proxy).hasFieldOrPropertyWithValue("targetBeanName", "springSecurityFilterChain");
    verify(registration).addMappingForUrlPatterns(DEFAULT_DISPATCH, false, "/*");
    verify(registration).setAsyncSupported(true);
    verifyNoAddListener(context);
}
Also used : ServletContext(jakarta.servlet.ServletContext) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) Test(org.junit.jupiter.api.Test)

Example 15 with ServletContext

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

the class AbstractSecurityWebApplicationInitializerTests method onStartupWhenSessionTrackingModesConfiguredThenUsed.

@Test
public void onStartupWhenSessionTrackingModesConfiguredThenUsed() {
    ServletContext context = mock(ServletContext.class);
    FilterRegistration.Dynamic registration = mock(FilterRegistration.Dynamic.class);
    ArgumentCaptor<DelegatingFilterProxy> proxyCaptor = ArgumentCaptor.forClass(DelegatingFilterProxy.class);
    given(context.addFilter(eq("springSecurityFilterChain"), proxyCaptor.capture())).willReturn(registration);
    ArgumentCaptor<Set<SessionTrackingMode>> modesCaptor = ArgumentCaptor.forClass(new HashSet<SessionTrackingMode>() {
    }.getClass());
    willDoNothing().given(context).setSessionTrackingModes(modesCaptor.capture());
    new AbstractSecurityWebApplicationInitializer() {

        @Override
        public Set<SessionTrackingMode> getSessionTrackingModes() {
            return Collections.singleton(SessionTrackingMode.SSL);
        }
    }.onStartup(context);
    assertProxyDefaults(proxyCaptor.getValue());
    Set<SessionTrackingMode> modes = modesCaptor.getValue();
    assertThat(modes).hasSize(1);
    assertThat(modes).containsExactly(SessionTrackingMode.SSL);
}
Also used : HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) Set(java.util.Set) SessionTrackingMode(jakarta.servlet.SessionTrackingMode) ServletContext(jakarta.servlet.ServletContext) FilterRegistration(jakarta.servlet.FilterRegistration) DelegatingFilterProxy(org.springframework.web.filter.DelegatingFilterProxy) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Aggregations

ServletContext (jakarta.servlet.ServletContext)148 Test (org.junit.jupiter.api.Test)63 ServletConfig (jakarta.servlet.ServletConfig)40 Enumeration (java.util.Enumeration)29 MockServletContext (org.springframework.web.testfixture.servlet.MockServletContext)24 BeforeMethod (org.testng.annotations.BeforeMethod)22 IOException (java.io.IOException)20 ServletException (jakarta.servlet.ServletException)17 FilterRegistration (jakarta.servlet.FilterRegistration)16 DelegatingFilterProxy (org.springframework.web.filter.DelegatingFilterProxy)15 Filter (jakarta.servlet.Filter)13 Test (org.junit.Test)13 ServletContextAwareProcessor (org.springframework.web.context.support.ServletContextAwareProcessor)12 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)12 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)12 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)11 WebApplicationContext (org.springframework.web.context.WebApplicationContext)9 File (java.io.File)8 Context (org.apache.catalina.Context)7 BlockingIOCometSupport (org.atmosphere.container.BlockingIOCometSupport)7