Search in sources :

Example 11 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method changeRedisNamespace.

@Test
public void changeRedisNamespace() {
    String namespace = "foo:bar";
    this.redisRepository.setRedisKeyNamespace(namespace);
    RedisSession session = this.redisRepository.new RedisSession(new MapSession());
    session.setMaxInactiveInterval(Duration.ZERO);
    given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
    this.redisRepository.save(session);
    String id = session.getId();
    verify(this.redisOperations, atLeastOnce()).delete(namespace + ":sessions:expires:" + id);
    verify(this.redisOperations, never()).boundValueOps(namespace + ":sessions:expires:" + id);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 12 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method saveRemoveAttribute.

@Test
public void saveRemoveAttribute() {
    String attrName = "attrName";
    RedisSession session = this.redisRepository.new RedisSession(new MapSession());
    session.removeAttribute(attrName);
    given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
    given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
    given(this.redisOperations.boundValueOps(anyString())).willReturn(this.boundValueOperations);
    this.redisRepository.save(session);
    assertThat(getDelta()).isEqualTo(map(RedisOperationsSessionRepository.getSessionAttrNameKey(attrName), null));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 13 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method resolveIndexOnSecurityContext.

@Test
public void resolveIndexOnSecurityContext() {
    String principal = "resolveIndexOnSecurityContext";
    Authentication authentication = new UsernamePasswordAuthenticationToken(principal, "notused", AuthorityUtils.createAuthorityList("ROLE_USER"));
    SecurityContext context = new SecurityContextImpl();
    context.setAuthentication(authentication);
    PrincipalNameResolver resolver = RedisOperationsSessionRepository.PRINCIPAL_NAME_RESOLVER;
    RedisSession session = this.redisRepository.createSession();
    session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, context);
    assertThat(resolver.resolvePrincipal(session)).isEqualTo(principal);
}
Also used : SecurityContextImpl(org.springframework.security.core.context.SecurityContextImpl) Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PrincipalNameResolver(org.springframework.session.data.redis.RedisOperationsSessionRepository.PrincipalNameResolver) Test(org.junit.Test)

Example 14 with RedisSession

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

the class RedisOperationsSessionRepositoryTests method flushModeOnSaveRemoveAttribute.

@Test
public void flushModeOnSaveRemoveAttribute() {
    RedisSession session = this.redisRepository.createSession();
    session.removeAttribute("remove");
    verifyZeroInteractions(this.boundHashOperations);
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Test(org.junit.Test)

Example 15 with RedisSession

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

the class RedisOperationsSessionRepositoryITests method findByDeletedSecurityPrincipalName.

@Test
public void findByDeletedSecurityPrincipalName() throws Exception {
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute(SPRING_SECURITY_CONTEXT, this.context);
    this.repository.save(toSave);
    toSave.setAttribute(SPRING_SECURITY_CONTEXT, null);
    this.repository.save(toSave);
    Map<String, RedisSession> findByPrincipalName = this.repository.findByIndexNameAndIndexValue(INDEX_NAME, getSecurityName());
    assertThat(findByPrincipalName).isEmpty();
}
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