Search in sources :

Example 21 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project spring-security by spring-projects.

the class HttpSessionEventPublisherTests method sessionDestroyedNullApplicationContext.

// SEC-2599
@Test(expected = IllegalStateException.class)
public void sessionDestroyedNullApplicationContext() {
    HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
    MockServletContext servletContext = new MockServletContext();
    MockHttpSession session = new MockHttpSession(servletContext);
    HttpSessionEvent event = new HttpSessionEvent(session);
    publisher.sessionDestroyed(event);
}
Also used : HttpSessionEventPublisher(org.springframework.security.web.session.HttpSessionEventPublisher) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) MockHttpSession(org.springframework.mock.web.MockHttpSession) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 22 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project spring-security by spring-projects.

the class HttpSessionEventPublisherTests method publishedEventIsReceivedbyListenerChildContext.

@Test
public void publishedEventIsReceivedbyListenerChildContext() {
    HttpSessionEventPublisher publisher = new HttpSessionEventPublisher();
    StaticWebApplicationContext context = new StaticWebApplicationContext();
    MockServletContext servletContext = new MockServletContext();
    servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", context);
    context.setServletContext(servletContext);
    context.registerSingleton("listener", MockApplicationListener.class, null);
    context.refresh();
    MockHttpSession session = new MockHttpSession(servletContext);
    MockApplicationListener listener = (MockApplicationListener) context.getBean("listener");
    HttpSessionEvent event = new HttpSessionEvent(session);
    publisher.sessionCreated(event);
    assertThat(listener.getCreatedEvent()).isNotNull();
    assertThat(listener.getDestroyedEvent()).isNull();
    assertThat(listener.getCreatedEvent().getSession()).isEqualTo(session);
    listener.setCreatedEvent(null);
    listener.setDestroyedEvent(null);
    publisher.sessionDestroyed(event);
    assertThat(listener.getDestroyedEvent()).isNotNull();
    assertThat(listener.getCreatedEvent()).isNull();
    assertThat(listener.getDestroyedEvent().getSession()).isEqualTo(session);
}
Also used : HttpSessionEventPublisher(org.springframework.security.web.session.HttpSessionEventPublisher) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) MockHttpSession(org.springframework.mock.web.MockHttpSession) StaticWebApplicationContext(org.springframework.web.context.support.StaticWebApplicationContext) MockServletContext(org.springframework.mock.web.MockServletContext) Test(org.junit.Test)

Example 23 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project drill by apache.

the class WebServer method createSessionHandler.

/**
   * @return A {@link SessionHandler} which contains a {@link HashSessionManager}
   */
private SessionHandler createSessionHandler(final SecurityHandler securityHandler) {
    SessionManager sessionManager = new HashSessionManager();
    sessionManager.setMaxInactiveInterval(config.getInt(ExecConstants.HTTP_SESSION_MAX_IDLE_SECS));
    sessionManager.addEventListener(new HttpSessionListener() {

        @Override
        public void sessionCreated(HttpSessionEvent se) {
        }

        @Override
        public void sessionDestroyed(HttpSessionEvent se) {
            final HttpSession session = se.getSession();
            if (session == null) {
                return;
            }
            final Object authCreds = session.getAttribute(SessionAuthentication.__J_AUTHENTICATED);
            if (authCreds != null) {
                final SessionAuthentication sessionAuth = (SessionAuthentication) authCreds;
                securityHandler.logout(sessionAuth);
                session.removeAttribute(SessionAuthentication.__J_AUTHENTICATED);
            }
            // Clear all the resources allocated for this session
            final WebSessionResources webSessionResources = (WebSessionResources) session.getAttribute(WebSessionResources.class.getSimpleName());
            if (webSessionResources != null) {
                webSessionResources.close();
                session.removeAttribute(WebSessionResources.class.getSimpleName());
            }
        }
    });
    return new SessionHandler(sessionManager);
}
Also used : SessionHandler(org.eclipse.jetty.server.session.SessionHandler) HttpSessionListener(javax.servlet.http.HttpSessionListener) HashSessionManager(org.eclipse.jetty.server.session.HashSessionManager) HashSessionManager(org.eclipse.jetty.server.session.HashSessionManager) SessionManager(org.eclipse.jetty.server.SessionManager) HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) SessionAuthentication(org.eclipse.jetty.security.authentication.SessionAuthentication)

Example 24 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project tomee by apache.

the class HttpSessionImpl method getSessionContext.

@Override
public HttpSessionContext getSessionContext() {
    touch();
    final SessionManager component = SystemInstance.get().getComponent(SessionManager.class);
    return new HttpSessionContext() {

        @Override
        public javax.servlet.http.HttpSession getSession(final String sessionId) {
            final HttpSessionEvent event = component.findSession(sessionId);
            return event == null ? null : event.getSession();
        }

        @Override
        public Enumeration<String> getIds() {
            return Collections.enumeration(component.findSessionIds());
        }
    };
}
Also used : SessionManager(org.apache.openejb.server.httpd.session.SessionManager) HttpSessionContext(javax.servlet.http.HttpSessionContext) HttpSessionEvent(javax.servlet.http.HttpSessionEvent)

Example 25 with HttpSessionEvent

use of javax.servlet.http.HttpSessionEvent in project tomee by apache.

the class HttpSessionImplTest method run.

@Test
public void run() throws URISyntaxException {
    final HttpRequest req = new HttpRequestImpl(new URI("http://localhost:1234/foo"));
    final javax.servlet.http.HttpSession session = req.getSession();
    Reflections.set(session, "listeners", Collections.<Object>singletonList(new HttpSessionListener() {

        private int count = 0;

        @Override
        public void sessionCreated(final HttpSessionEvent se) {
        // no-op
        }

        @Override
        public void sessionDestroyed(final HttpSessionEvent se) {
            se.getSession().setAttribute("seen", ++count);
        }
    }));
    session.invalidate();
    final long c1 = Integer.class.cast(session.getAttribute("seen"));
    session.invalidate();
    final long c2 = Integer.class.cast(session.getAttribute("seen"));
    assertEquals(c1, c2);
}
Also used : HttpSessionListener(javax.servlet.http.HttpSessionListener) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) URI(java.net.URI) Test(org.junit.Test)

Aggregations

HttpSessionEvent (javax.servlet.http.HttpSessionEvent)25 HttpSessionActivationListener (javax.servlet.http.HttpSessionActivationListener)8 HttpSessionListener (javax.servlet.http.HttpSessionListener)7 Test (org.junit.Test)6 IOException (java.io.IOException)4 HttpSession (javax.servlet.http.HttpSession)4 MockHttpSession (org.springframework.mock.web.MockHttpSession)4 MockServletContext (org.springframework.mock.web.MockServletContext)4 HttpSessionEventPublisher (org.springframework.security.web.session.HttpSessionEventPublisher)4 ServletContext (javax.servlet.ServletContext)3 SessionManager (org.apache.openejb.server.httpd.session.SessionManager)3 PersistentSession (io.undertow.servlet.api.SessionPersistenceManager.PersistentSession)2 NotSerializableException (java.io.NotSerializableException)2 WriteAbortedException (java.io.WriteAbortedException)2 HashMap (java.util.HashMap)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ServletException (javax.servlet.ServletException)2 Context (org.apache.catalina.Context)2 StaticWebApplicationContext (org.springframework.web.context.support.StaticWebApplicationContext)2 ImmutableHttpSessionAdapter (org.wildfly.clustering.web.session.ImmutableHttpSessionAdapter)2