Search in sources :

Example 1 with SessionDestroyedEvent

use of org.springframework.session.events.SessionDestroyedEvent in project spring-session by spring-projects.

the class SessionEventHttpSessionListenerAdapterTests method setup.

@Before
public void setup() {
    this.listener = new SessionEventHttpSessionListenerAdapter(Arrays.asList(this.listener1, this.listener2));
    this.listener.setServletContext(new MockServletContext());
    Session session = new MapSession();
    this.destroyed = new SessionDestroyedEvent(this, session);
    this.created = new SessionCreatedEvent(this, session);
}
Also used : SessionCreatedEvent(org.springframework.session.events.SessionCreatedEvent) MapSession(org.springframework.session.MapSession) SessionDestroyedEvent(org.springframework.session.events.SessionDestroyedEvent) MockServletContext(org.springframework.mock.web.MockServletContext) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) Before(org.junit.Before)

Example 2 with SessionDestroyedEvent

use of org.springframework.session.events.SessionDestroyedEvent in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method saves.

@Test
public void saves() throws InterruptedException {
    String username = "saves-" + System.currentTimeMillis();
    String usernameSessionKey = "RedisOperationsSessionRepositoryITests:index:" + INDEX_NAME + ":" + username;
    RedisSession toSave = this.repository.createSession();
    String expectedAttributeName = "a";
    String expectedAttributeValue = "b";
    toSave.setAttribute(expectedAttributeName, expectedAttributeValue);
    Authentication toSaveToken = new UsernamePasswordAuthenticationToken(username, "password", AuthorityUtils.createAuthorityList("ROLE_USER"));
    SecurityContext toSaveContext = SecurityContextHolder.createEmptyContext();
    toSaveContext.setAuthentication(toSaveToken);
    toSave.setAttribute(SPRING_SECURITY_CONTEXT, toSaveContext);
    toSave.setAttribute(INDEX_NAME, username);
    this.registry.clear();
    this.repository.save(toSave);
    assertThat(this.registry.receivedEvent(toSave.getId())).isTrue();
    assertThat(this.registry.<SessionCreatedEvent>getEvent(toSave.getId())).isInstanceOf(SessionCreatedEvent.class);
    assertThat(this.redis.boundSetOps(usernameSessionKey).members()).contains(toSave.getId());
    Session session = this.repository.findById(toSave.getId());
    assertThat(session.getId()).isEqualTo(toSave.getId());
    assertThat(session.getAttributeNames()).isEqualTo(toSave.getAttributeNames());
    assertThat(session.<String>getAttribute(expectedAttributeName)).isEqualTo(toSave.getAttribute(expectedAttributeName));
    this.registry.clear();
    this.repository.deleteById(toSave.getId());
    assertThat(this.repository.findById(toSave.getId())).isNull();
    assertThat(this.registry.<SessionDestroyedEvent>getEvent(toSave.getId())).isInstanceOf(SessionDestroyedEvent.class);
    assertThat(this.redis.boundSetOps(usernameSessionKey).members()).doesNotContain(toSave.getId());
    assertThat(this.registry.getEvent(toSave.getId()).getSession().<String>getAttribute(expectedAttributeName)).isEqualTo(expectedAttributeValue);
}
Also used : SessionCreatedEvent(org.springframework.session.events.SessionCreatedEvent) Authentication(org.springframework.security.core.Authentication) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) SessionDestroyedEvent(org.springframework.session.events.SessionDestroyedEvent) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) EnableRedisHttpSession(org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession) Session(org.springframework.session.Session) Test(org.junit.Test)

Example 3 with SessionDestroyedEvent

use of org.springframework.session.events.SessionDestroyedEvent in project spring-session by spring-projects.

the class WebSocketRegistryListener method onApplicationEvent.

@Override
public void onApplicationEvent(ApplicationEvent event) {
    if (event instanceof SessionDestroyedEvent) {
        SessionDestroyedEvent e = (SessionDestroyedEvent) event;
        closeWsSessions(e.getSessionId());
    } else if (event instanceof SessionConnectEvent) {
        SessionConnectEvent e = (SessionConnectEvent) event;
        afterConnectionEstablished(e.getWebSocketSession());
    } else if (event instanceof SessionDisconnectEvent) {
        SessionDisconnectEvent e = (SessionDisconnectEvent) event;
        Map<String, Object> sessionAttributes = SimpMessageHeaderAccessor.getSessionAttributes(e.getMessage().getHeaders());
        String httpSessionId = sessionAttributes == null ? null : SessionRepositoryMessageInterceptor.getSessionId(sessionAttributes);
        afterConnectionClosed(httpSessionId, e.getSessionId());
    }
}
Also used : SessionDisconnectEvent(org.springframework.web.socket.messaging.SessionDisconnectEvent) SessionDestroyedEvent(org.springframework.session.events.SessionDestroyedEvent) SessionConnectEvent(org.springframework.session.web.socket.events.SessionConnectEvent)

Aggregations

SessionDestroyedEvent (org.springframework.session.events.SessionDestroyedEvent)3 Session (org.springframework.session.Session)2 SessionCreatedEvent (org.springframework.session.events.SessionCreatedEvent)2 Before (org.junit.Before)1 Test (org.junit.Test)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)1 Authentication (org.springframework.security.core.Authentication)1 SecurityContext (org.springframework.security.core.context.SecurityContext)1 MapSession (org.springframework.session.MapSession)1 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)1 EnableRedisHttpSession (org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession)1 SessionConnectEvent (org.springframework.session.web.socket.events.SessionConnectEvent)1 SessionDisconnectEvent (org.springframework.web.socket.messaging.SessionDisconnectEvent)1