Search in sources :

Example 36 with HttpSession

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

the class ServletRequestAttributes method getSession.

/**
 * Exposes the {@link HttpSession} that we're wrapping.
 * @param allowCreate whether to allow creation of a new session if none exists yet
 */
@Nullable
protected final HttpSession getSession(boolean allowCreate) {
    if (isRequestActive()) {
        HttpSession session = this.request.getSession(allowCreate);
        this.session = session;
        return session;
    } else {
        // Access through stored session reference, if any...
        HttpSession session = this.session;
        if (session == null) {
            if (allowCreate) {
                throw new IllegalStateException("No session found and request already completed - cannot create new session!");
            } else {
                session = this.request.getSession(false);
                this.session = session;
            }
        }
        return session;
    }
}
Also used : HttpSession(jakarta.servlet.http.HttpSession) Nullable(org.springframework.lang.Nullable)

Example 37 with HttpSession

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

the class ServletRequestAttributes method registerSessionDestructionCallback.

/**
 * Register the given callback as to be executed after session termination.
 * <p>Note: The callback object should be serializable in order to survive
 * web app restarts.
 * @param name the name of the attribute to register the callback for
 * @param callback the callback to be executed for destruction
 */
protected void registerSessionDestructionCallback(String name, Runnable callback) {
    HttpSession session = obtainSession();
    session.setAttribute(DESTRUCTION_CALLBACK_NAME_PREFIX + name, new DestructionCallbackBindingListener(callback));
}
Also used : HttpSession(jakarta.servlet.http.HttpSession)

Example 38 with HttpSession

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

the class ServletRequestAttributes method setAttribute.

@Override
public void setAttribute(String name, Object value, int scope) {
    if (scope == SCOPE_REQUEST) {
        if (!isRequestActive()) {
            throw new IllegalStateException("Cannot set request attribute - request is not active anymore!");
        }
        this.request.setAttribute(name, value);
    } else {
        HttpSession session = obtainSession();
        this.sessionAttributesToUpdate.remove(name);
        session.setAttribute(name, value);
    }
}
Also used : HttpSession(jakarta.servlet.http.HttpSession)

Example 39 with HttpSession

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

the class HtmlUnitRequestBuilderTests method buildRequestSessionWithExistingSession.

@Test
public void buildRequestSessionWithExistingSession() throws Exception {
    String sessionId = "session-id";
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId);
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession session = actualRequest.getSession();
    assertThat(session.getId()).isEqualTo(sessionId);
    assertSingleSessionCookie("JSESSIONID=" + session.getId() + "; Path=/test; Domain=example.com");
    requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession()).isEqualTo(session);
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + sessionId + "NEW");
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession()).isNotEqualTo(session);
    assertSingleSessionCookie("JSESSIONID=" + actualRequest.getSession().getId() + "; Path=/test; Domain=example.com");
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) Test(org.junit.jupiter.api.Test)

Example 40 with HttpSession

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

the class HtmlUnitRequestBuilderTests method buildRequestSession.

@Test
public void buildRequestSession() throws Exception {
    MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
    HttpSession newSession = actualRequest.getSession();
    assertThat(newSession).isNotNull();
    assertSingleSessionCookie("JSESSIONID=" + newSession.getId() + "; Path=/test; Domain=example.com");
    webRequest.setAdditionalHeader("Cookie", "JSESSIONID=" + newSession.getId());
    requestBuilder = new HtmlUnitRequestBuilder(sessions, webClient, webRequest);
    actualRequest = requestBuilder.buildRequest(servletContext);
    assertThat(actualRequest.getSession()).isSameAs(newSession);
}
Also used : MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) HttpSession(jakarta.servlet.http.HttpSession) MockHttpSession(org.springframework.mock.web.MockHttpSession) 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