Search in sources :

Example 51 with HttpSession

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

the class RememberMeConfigurerTests method logoutWhenRememberMeCookieThenAuthenticationIsRememberMeCookieExpired.

@Test
public void logoutWhenRememberMeCookieThenAuthenticationIsRememberMeCookieExpired() throws Exception {
    this.spring.register(RememberMeConfig.class).autowire();
    // @formatter:off
    MockHttpServletRequestBuilder loginRequest = post("/login").with(csrf()).param("username", "user").param("password", "password").param("remember-me", "true");
    // @formatter:on
    MvcResult mvcResult = this.mvc.perform(loginRequest).andReturn();
    Cookie rememberMeCookie = mvcResult.getResponse().getCookie("remember-me");
    HttpSession session = mvcResult.getRequest().getSession();
    // @formatter:off
    MockHttpServletRequestBuilder logoutRequest = post("/logout").with(csrf()).cookie(rememberMeCookie).session((MockHttpSession) session);
    this.mvc.perform(logoutRequest).andExpect(redirectedUrl("/login?logout")).andExpect(cookie().maxAge("remember-me", 0));
// @formatter:on
}
Also used : Cookie(jakarta.servlet.http.Cookie) HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 52 with HttpSession

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

the class SessionManagementConfigurerTests method loginWhenUserSessionExpiredAndMaxSessionsIsOneThenLoggedIn.

@Test
public void loginWhenUserSessionExpiredAndMaxSessionsIsOneThenLoggedIn() throws Exception {
    this.spring.register(ConcurrencyControlConfig.class).autowire();
    // @formatter:off
    MockHttpServletRequestBuilder firstRequest = post("/login").with(csrf()).param("username", "user").param("password", "password");
    MvcResult mvcResult = this.mvc.perform(firstRequest).andReturn();
    // @formatter:on
    HttpSession authenticatedSession = mvcResult.getRequest().getSession();
    this.spring.getContext().publishEvent(new HttpSessionDestroyedEvent(authenticatedSession));
    // @formatter:off
    MockHttpServletRequestBuilder secondRequest = post("/login").with(csrf()).param("username", "user").param("password", "password");
    this.mvc.perform(secondRequest).andExpect(status().isFound()).andExpect(redirectedUrl("/"));
// @formatter:on
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockHttpServletRequestBuilder(org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder) MvcResult(org.springframework.test.web.servlet.MvcResult) HttpSessionDestroyedEvent(org.springframework.security.web.session.HttpSessionDestroyedEvent) Test(org.junit.jupiter.api.Test)

Example 53 with HttpSession

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

the class SessionManagementConfigurerTests method requestWhenSessionCreationPolicyStateLessInLambdaThenNoSessionCreated.

@Test
public void requestWhenSessionCreationPolicyStateLessInLambdaThenNoSessionCreated() throws Exception {
    this.spring.register(SessionCreationPolicyStateLessInLambdaConfig.class).autowire();
    MvcResult mvcResult = this.mvc.perform(get("/")).andReturn();
    HttpSession session = mvcResult.getRequest().getSession(false);
    assertThat(session).isNull();
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 54 with HttpSession

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

the class SecurityContextConfigurerTests method requestWhenSecurityContextWithDefaultsInLambdaThenSessionIsCreated.

@Test
public void requestWhenSecurityContextWithDefaultsInLambdaThenSessionIsCreated() throws Exception {
    this.spring.register(SecurityContextWithDefaultsInLambdaConfig.class).autowire();
    MvcResult mvcResult = this.mvc.perform(formLogin()).andReturn();
    HttpSession session = mvcResult.getRequest().getSession(false);
    assertThat(session).isNotNull();
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Example 55 with HttpSession

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

the class SecurityContextConfigurerTests method requestWhenNullSecurityContextRepositoryInLambdaThenContextNotSavedInSession.

@Test
public void requestWhenNullSecurityContextRepositoryInLambdaThenContextNotSavedInSession() throws Exception {
    this.spring.register(NullSecurityContextRepositoryInLambdaConfig.class).autowire();
    MvcResult mvcResult = this.mvc.perform(formLogin()).andReturn();
    HttpSession session = mvcResult.getRequest().getSession(false);
    assertThat(session).isNull();
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) Test(org.junit.jupiter.api.Test)

Aggregations

HttpSession (jakarta.servlet.http.HttpSession)101 Test (org.junit.jupiter.api.Test)39 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)17 MvcResult (org.springframework.test.web.servlet.MvcResult)16 HttpServletRequest (jakarta.servlet.http.HttpServletRequest)13 MockHttpServletResponse (org.springframework.mock.web.MockHttpServletResponse)12 MockHttpSession (org.springframework.mock.web.MockHttpSession)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.web.testfixture.servlet.MockHttpServletRequest)11 MockHttpServletResponse (org.springframework.web.testfixture.servlet.MockHttpServletResponse)9 SecurityContext (org.springframework.security.core.context.SecurityContext)7 PathPatternsParameterizedTest (org.springframework.web.servlet.handler.PathPatternsParameterizedTest)7 Authentication (org.springframework.security.core.Authentication)6 Cookie (jakarta.servlet.http.Cookie)5 Request (org.apache.catalina.connector.Request)5 OAuth2AuthorizedClient (org.springframework.security.oauth2.client.OAuth2AuthorizedClient)5 SessionFixationProtectionStrategy (org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy)5 IOException (java.io.IOException)4 PrintWriter (java.io.PrintWriter)4 Response (org.apache.catalina.connector.Response)4