Search in sources :

Example 41 with RedisSession

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

the class RedisOperationsSessionRepositoryITests method findByPrincipalNameNoSecurityPrincipalNameChange.

@Test
public void findByPrincipalNameNoSecurityPrincipalNameChange() throws Exception {
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(SPRING_SECURITY_CONTEXT, this.context);
    this.repository.save(toSave);
    toSave.setAttribute("other", "value");
    this.repository.save(toSave);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, getSecurityName());
    assertThat(findByPrincipalName).hasSize(1);
    assertThat(findByPrincipalName.keySet()).containsOnly(toSave.getId());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 42 with RedisSession

use of org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession 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 43 with RedisSession

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

the class RedisOperationsSessionRepositoryITests method changeSessionIdWhenSetAttributeOnChangedSession.

@Test
public void changeSessionIdWhenSetAttributeOnChangedSession() throws Exception {
    String attrName = "changeSessionId";
    String attrValue = "changeSessionId-value";
    RedisSession toSave = this.repository.createSession();
    this.repository.save(toSave);
    RedisSession findById = this.repository.findById(toSave.getId());
    findById.setAttribute(attrName, 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 44 with RedisSession

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

the class RedisOperationsSessionRepositoryITests method changeSessionIdWhenHasNotSaved.

@Test
public void changeSessionIdWhenHasNotSaved() throws Exception {
    String attrName = "changeSessionId";
    String attrValue = "changeSessionId-value";
    RedisSession toSave = this.repository.createSession();
    String originalId = toSave.getId();
    toSave.changeSessionId();
    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 45 with RedisSession

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

the class RedisOperationsSessionRepositoryITests method changeSessionIdWhenChangeTwice.

@Test
public void changeSessionIdWhenChangeTwice() throws Exception {
    RedisSession toSave = this.repository.createSession();
    this.repository.save(toSave);
    String originalId = toSave.getId();
    String changeId1 = toSave.changeSessionId();
    String changeId2 = toSave.changeSessionId();
    this.repository.save(toSave);
    assertThat(this.repository.findById(originalId)).isNull();
    assertThat(this.repository.findById(changeId1)).isNull();
    assertThat(this.repository.findById(changeId2)).isNotNull();
}
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