Search in sources :

Example 1 with SessionCreatedEvent

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

the class EnableHazelcastHttpSessionEventsTests method saveSessionTest.

@Test
public void saveSessionTest() throws InterruptedException {
    String username = "saves-" + System.currentTimeMillis();
    S sessionToSave = this.repository.createSession();
    String expectedAttributeName = "a";
    String expectedAttributeValue = "b";
    sessionToSave.setAttribute(expectedAttributeName, expectedAttributeValue);
    Authentication toSaveToken = new UsernamePasswordAuthenticationToken(username, "password", AuthorityUtils.createAuthorityList("ROLE_USER"));
    SecurityContext toSaveContext = SecurityContextHolder.createEmptyContext();
    toSaveContext.setAuthentication(toSaveToken);
    sessionToSave.setAttribute("SPRING_SECURITY_CONTEXT", toSaveContext);
    sessionToSave.setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, username);
    this.repository.save(sessionToSave);
    assertThat(this.registry.receivedEvent(sessionToSave.getId())).isTrue();
    assertThat(this.registry.<SessionCreatedEvent>getEvent(sessionToSave.getId())).isInstanceOf(SessionCreatedEvent.class);
    Session session = this.repository.findById(sessionToSave.getId());
    assertThat(session.getId()).isEqualTo(sessionToSave.getId());
    assertThat(session.getAttributeNames()).isEqualTo(sessionToSave.getAttributeNames());
    assertThat(session.<String>getAttribute(expectedAttributeName)).isEqualTo(sessionToSave.getAttribute(expectedAttributeName));
}
Also used : SessionCreatedEvent(org.springframework.session.events.SessionCreatedEvent) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Session(org.springframework.session.Session) Test(org.junit.Test)

Example 2 with SessionCreatedEvent

use of org.springframework.session.events.SessionCreatedEvent 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 3 with SessionCreatedEvent

use of org.springframework.session.events.SessionCreatedEvent in project metasfresh-webui-api by metasfresh.

the class FixedMapSessionRepository method createSession.

@Override
public ExpiringSession createSession() {
    final ExpiringSession result = new MapSession();
    if (defaultMaxInactiveInterval != null) {
        result.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);
    }
    // Fire event
    applicationEventPublisher.publishEvent(new SessionCreatedEvent(this, result.getId()));
    return result;
}
Also used : ExpiringSession(org.springframework.session.ExpiringSession) SessionCreatedEvent(org.springframework.session.events.SessionCreatedEvent) MapSession(org.springframework.session.MapSession)

Example 4 with SessionCreatedEvent

use of org.springframework.session.events.SessionCreatedEvent in project redisson by redisson.

the class RedissonSessionRepository method onMessage.

@Override
public void onMessage(String pattern, String channel, String body) {
    if (createdTopic.getPatternNames().contains(pattern)) {
        RedissonSession session = getSession(body);
        if (session != null) {
            publishEvent(new SessionCreatedEvent(this, session));
        }
    } else if (deletedTopic.getPatternNames().contains(pattern)) {
        String id = body.split(":")[1];
        RedissonSession session = new RedissonSession(id);
        if (session.load()) {
            session.clearPrincipal();
            publishEvent(new SessionDeletedEvent(this, session));
        } else {
            publishEvent(new SessionDeletedEvent(this, id));
        }
    } else if (expiredTopic.getPatternNames().contains(pattern)) {
        String id = body.split(":")[1];
        RedissonSession session = new RedissonSession(id);
        if (session.load()) {
            session.clearPrincipal();
            publishEvent(new SessionExpiredEvent(this, session));
        } else {
            publishEvent(new SessionExpiredEvent(this, id));
        }
    }
}
Also used : SessionCreatedEvent(org.springframework.session.events.SessionCreatedEvent) SessionDeletedEvent(org.springframework.session.events.SessionDeletedEvent) SessionExpiredEvent(org.springframework.session.events.SessionExpiredEvent)

Example 5 with SessionCreatedEvent

use of org.springframework.session.events.SessionCreatedEvent 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)

Aggregations

SessionCreatedEvent (org.springframework.session.events.SessionCreatedEvent)6 Session (org.springframework.session.Session)4 MapSession (org.springframework.session.MapSession)3 Test (org.junit.Test)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 SecurityContext (org.springframework.security.core.context.SecurityContext)2 SessionDestroyedEvent (org.springframework.session.events.SessionDestroyedEvent)2 Before (org.junit.Before)1 MockServletContext (org.springframework.mock.web.MockServletContext)1 ExpiringSession (org.springframework.session.ExpiringSession)1 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)1 EnableRedisHttpSession (org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession)1 SessionDeletedEvent (org.springframework.session.events.SessionDeletedEvent)1 SessionExpiredEvent (org.springframework.session.events.SessionExpiredEvent)1