Search in sources :

Example 16 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method changeSessionIdSaveTwice.

// gh-962
@Test
public void changeSessionIdSaveTwice() {
    RedisSession toSave = this.repository.createSession();
    String originalId = toSave.getId();
    toSave.changeSessionId();
    this.repository.save(toSave);
    this.repository.save(toSave);
    assertThat(this.repository.findById(toSave.getId())).isNotNull();
    assertThat(this.repository.findById(originalId)).isNull();
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 17 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findByDeletedPrincipalNameReload.

@Test
public void findByDeletedPrincipalNameReload() throws Exception {
    String principalName = "findByDeletedPrincipalName" + UUID.randomUUID();
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(INDEX_NAME, principalName);
    this.repository.save(toSave);
    RedisSession getSession = this.repository.findById(toSave.getId());
    getSession.setAttribute(INDEX_NAME, null);
    this.repository.save(getSession);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName);
    assertThat(findByPrincipalName).isEmpty();
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 18 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findBySecurityPrincipalNameExpireRemovesIndex.

@Test
public void findBySecurityPrincipalNameExpireRemovesIndex() throws Exception {
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(SPRING_SECURITY_CONTEXT, this.context);
    this.repository.save(toSave);
    String body = "RedisOperationsSessionRepositoryITests:sessions:expires:" + toSave.getId();
    String channel = ":expired";
    DefaultMessage message = new DefaultMessage(channel.getBytes("UTF-8"), body.getBytes("UTF-8"));
    byte[] pattern = new byte[] {};
    this.repository.onMessage(message, pattern);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, getSecurityName());
    assertThat(findByPrincipalName).hasSize(0);
    assertThat(findByPrincipalName.keySet()).doesNotContain(toSave.getId());
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 19 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method changeSessionIdWhenOnlyChangeId.

@Test
public void changeSessionIdWhenOnlyChangeId() throws Exception {
    String attrName = "changeSessionId";
    String attrValue = "changeSessionId-value";
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(attrName, attrValue);
    this.repository.save(toSave);
    RedisSession findById = this.repository.findById(toSave.getId());
    assertThat(findById.<String>getAttribute(attrName)).isEqualTo(attrValue);
    String originalFindById = findById.getId();
    String changeSessionId = findById.changeSessionId();
    this.repository.save(findById);
    assertThat(this.repository.findById(originalFindById)).isNull();
    RedisSession findByChangeSessionId = this.repository.findById(changeSessionId);
    assertThat(findByChangeSessionId.<String>getAttribute(attrName)).isEqualTo(attrValue);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 20 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession in project spring-session by spring-projects.

the class RedisOperationsSessionRepositoryITests method findByPrincipalName.

@Test
public void findByPrincipalName() throws Exception {
    String principalName = "findByPrincipalName" + UUID.randomUUID();
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(INDEX_NAME, principalName);
    this.repository.save(toSave);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName);
    assertThat(findByPrincipalName).hasSize(1);
    assertThat(findByPrincipalName.keySet()).containsOnly(toSave.getId());
    this.repository.deleteById(toSave.getId());
    assertThat(this.registry.receivedEvent(toSave.getId())).isTrue();
    findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, principalName);
    assertThat(findByPrincipalName).hasSize(0);
    assertThat(findByPrincipalName.keySet()).doesNotContain(toSave.getId());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)47 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)47 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 MapSession (org.springframework.session.MapSession)6 HashMap (java.util.HashMap)2 Map (java.util.Map)2 DefaultMessage (org.springframework.data.redis.connection.DefaultMessage)2 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)2 Authentication (org.springframework.security.core.Authentication)2 SecurityContext (org.springframework.security.core.context.SecurityContext)2 Session (org.springframework.session.Session)2 PrincipalNameResolver (org.springframework.session.data.redis.RedisOperationsSessionRepository.PrincipalNameResolver)2 EnableRedisHttpSession (org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession)2 Duration (java.time.Duration)1 Instant (java.time.Instant)1 TimeUnit (java.util.concurrent.TimeUnit)1 SecurityContextImpl (org.springframework.security.core.context.SecurityContextImpl)1 SessionCreatedEvent (org.springframework.session.events.SessionCreatedEvent)1 SessionDestroyedEvent (org.springframework.session.events.SessionDestroyedEvent)1