Search in sources :

Example 1 with Session

use of org.springframework.session.Session in project tutorials by eugenp.

the class SessionSavingZuulPreFilter method run.

@Override
public Object run() {
    RequestContext context = RequestContext.getCurrentContext();
    HttpSession httpSession = context.getRequest().getSession();
    Session session = repository.getSession(httpSession.getId());
    context.addZuulRequestHeader("Cookie", "SESSION=" + httpSession.getId());
    log.info("ZuulPreFilter session proxy: {}", session.getId());
    return null;
}
Also used : HttpSession(javax.servlet.http.HttpSession) RequestContext(com.netflix.zuul.context.RequestContext) HttpSession(javax.servlet.http.HttpSession) Session(org.springframework.session.Session)

Example 2 with Session

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

the class AbstractHttpSessionListenerTests method springSessionDestroyedTranslatedToSpringSecurityDestroyed.

@Test
public void springSessionDestroyedTranslatedToSpringSecurityDestroyed() {
    Session session = new MapSession();
    this.publisher.publishEvent(new org.springframework.session.events.SessionDestroyedEvent(this, session));
    assertThat(this.listener.getEvent().getId()).isEqualTo(session.getId());
}
Also used : MapSession(org.springframework.session.MapSession) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 3 with Session

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

the class RedisOperationsSessionRepositoryTests method createSessionDefaultMaxInactiveInterval.

@Test
public void createSessionDefaultMaxInactiveInterval() throws Exception {
    Session session = this.redisRepository.createSession();
    assertThat(session.getMaxInactiveInterval()).isEqualTo(new MapSession().getMaxInactiveInterval());
}
Also used : MapSession(org.springframework.session.MapSession) RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 4 with Session

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

the class RedisOperationsSessionRepositoryTests method createSessionCustomMaxInactiveInterval.

@Test
public void createSessionCustomMaxInactiveInterval() throws Exception {
    int interval = 1;
    this.redisRepository.setDefaultMaxInactiveInterval(interval);
    Session session = this.redisRepository.createSession();
    assertThat(session.getMaxInactiveInterval()).isEqualTo(Duration.ofSeconds(interval));
}
Also used : RedisSession(org.springframework.session.data.redis.RedisOperationsSessionRepository.RedisSession) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 5 with Session

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

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