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();
}
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();
}
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;
}
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);
}
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);
}
Aggregations