Search in sources :

Example 21 with Session

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

the class RedisOperationsSessionRepositoryITests method putAllOnSingleAttrDoesNotRemoveOld.

@Test
public void putAllOnSingleAttrDoesNotRemoveOld() {
    RedisSession toSave = this.repository.createSession();
    toSave.setAttribute("a", "b");
    this.repository.save(toSave);
    toSave = this.repository.findById(toSave.getId());
    toSave.setAttribute("1", "2");
    this.repository.save(toSave);
    toSave = this.repository.findById(toSave.getId());
    Session session = this.repository.findById(toSave.getId());
    assertThat(session.getAttributeNames().size()).isEqualTo(2);
    assertThat(session.<String>getAttribute("a")).isEqualTo("b");
    assertThat(session.<String>getAttribute("1")).isEqualTo("2");
    this.repository.deleteById(toSave.getId());
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) 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 22 with Session

use of org.springframework.session.Session 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 23 with Session

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

the class RedisOperationsSessionRepository method handleCreated.

private void handleCreated(Map<Object, Object> loaded, String channel) {
    String id = channel.substring(channel.lastIndexOf(":") + 1);
    Session session = loadSession(id, loaded);
    publishEvent(new SessionCreatedEvent(this, session));
}
Also used : SessionCreatedEvent(org.springframework.session.events.SessionCreatedEvent) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession)

Example 24 with Session

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

the class SessionEventHttpSessionListenerAdapter method createHttpSessionEvent.

private HttpSessionEvent createHttpSessionEvent(AbstractSessionEvent event) {
    Session session = event.getSession();
    HttpSession httpSession = new HttpSessionAdapter<>(session, this.context);
    return new HttpSessionEvent(httpSession);
}
Also used : HttpSession(javax.servlet.http.HttpSession) HttpSessionEvent(javax.servlet.http.HttpSessionEvent) HttpSession(javax.servlet.http.HttpSession) Session(org.springframework.session.Session)

Example 25 with Session

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

the class SpringSessionBackedSessionRegistryTest method sessionInformationForExpiredSession.

@Test
public void sessionInformationForExpiredSession() {
    Session session = createSession(SESSION_ID, USER_NAME, NOW);
    session.setAttribute(SpringSessionBackedSessionInformation.EXPIRED_ATTR, Boolean.TRUE);
    when(this.sessionRepository.findById(SESSION_ID)).thenReturn(session);
    SessionInformation sessionInfo = this.sessionRegistry.getSessionInformation(SESSION_ID);
    assertThat(sessionInfo.getSessionId()).isEqualTo(SESSION_ID);
    assertThat(sessionInfo.getLastRequest().toInstant()).isEqualTo(NOW);
    assertThat(sessionInfo.getPrincipal()).isEqualTo(USER_NAME);
    assertThat(sessionInfo.isExpired()).isTrue();
}
Also used : SessionInformation(org.springframework.security.core.session.SessionInformation) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Aggregations

Session (org.springframework.session.Session)27 Test (org.junit.Test)19 MapSession (org.springframework.session.MapSession)19 HttpSession (javax.servlet.http.HttpSession)4 BatchPreparedStatementSetter (org.springframework.jdbc.core.BatchPreparedStatementSetter)4 ResultSetExtractor (org.springframework.jdbc.core.ResultSetExtractor)4 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)4 Authentication (org.springframework.security.core.Authentication)4 RedisSession (org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession)4 SessionCreatedEvent (org.springframework.session.events.SessionCreatedEvent)4 PreparedStatementSetter (org.springframework.jdbc.core.PreparedStatementSetter)3 EnableJdbcHttpSession (org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession)3 RequestContext (com.netflix.zuul.context.RequestContext)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 SecurityContext (org.springframework.security.core.context.SecurityContext)2 SessionInformation (org.springframework.security.core.session.SessionInformation)2 EnableRedisHttpSession (org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession)2 EnableRedisWebSession (org.springframework.session.data.redis.config.annotation.web.server.EnableRedisWebSession)2 IOException (java.io.IOException)1