Search in sources :

Example 16 with Session

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

the class JdbcOperationsSessionRepositoryTests method getSessionFound.

@Test
@SuppressWarnings("unchecked")
public void getSessionFound() {
    Session saved = this.repository.new JdbcSession("primaryKey", new MapSession());
    saved.setAttribute("savedName", "savedValue");
    given(this.jdbcOperations.query(isA(String.class), isA(PreparedStatementSetter.class), isA(ResultSetExtractor.class))).willReturn(Collections.singletonList(saved));
    JdbcOperationsSessionRepository.JdbcSession session = this.repository.findById(saved.getId());
    assertThat(session.getId()).isEqualTo(saved.getId());
    assertThat(session.isNew()).isFalse();
    assertThat(session.<String>getAttribute("savedName")).isEqualTo("savedValue");
    assertPropagationRequiresNew();
    verify(this.jdbcOperations, times(1)).query(isA(String.class), isA(PreparedStatementSetter.class), isA(ResultSetExtractor.class));
}
Also used : ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) MapSession(org.springframework.session.MapSession) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 17 with Session

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

the class JdbcOperationsSessionRepositoryTests method getSessionExpired.

@Test
@SuppressWarnings("unchecked")
public void getSessionExpired() {
    Session expired = this.repository.new JdbcSession();
    expired.setLastAccessedTime(Instant.now().minusSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS + 1));
    given(this.jdbcOperations.query(isA(String.class), isA(PreparedStatementSetter.class), isA(ResultSetExtractor.class))).willReturn(Collections.singletonList(expired));
    JdbcOperationsSessionRepository.JdbcSession session = this.repository.findById(expired.getId());
    assertThat(session).isNull();
    assertPropagationRequiresNew();
    verify(this.jdbcOperations, times(1)).query(isA(String.class), isA(PreparedStatementSetter.class), isA(ResultSetExtractor.class));
    verify(this.jdbcOperations, times(1)).update(startsWith("DELETE"), eq(expired.getId()));
}
Also used : ResultSetExtractor(org.springframework.jdbc.core.ResultSetExtractor) BatchPreparedStatementSetter(org.springframework.jdbc.core.BatchPreparedStatementSetter) PreparedStatementSetter(org.springframework.jdbc.core.PreparedStatementSetter) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) Test(org.junit.Test)

Example 18 with Session

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

the class AbstractJdbcOperationsSessionRepositoryITests method saves.

@Test
public void saves() throws InterruptedException {
    String username = "saves-" + System.currentTimeMillis();
    JdbcOperationsSessionRepository.JdbcSession 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.repository.save(toSave);
    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.repository.deleteById(toSave.getId());
    assertThat(this.repository.findById(toSave.getId())).isNull();
}
Also used : Authentication(org.springframework.security.core.Authentication) SecurityContext(org.springframework.security.core.context.SecurityContext) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) EnableJdbcHttpSession(org.springframework.session.jdbc.config.annotation.web.http.EnableJdbcHttpSession) Test(org.junit.Test)

Example 19 with Session

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

the class Initializer method contextInitialized.

@Override
public void contextInitialized(ServletContextEvent sce) {
    this.instance = createHazelcastInstance();
    Map<String, Session> sessions = this.instance.getMap(SESSION_MAP_NAME);
    MapSessionRepository sessionRepository = new MapSessionRepository(sessions);
    SessionRepositoryFilter<? extends Session> filter = new SessionRepositoryFilter<>(sessionRepository);
    Dynamic fr = sce.getServletContext().addFilter("springSessionFilter", filter);
    fr.addMappingForUrlPatterns(EnumSet.of(DispatcherType.REQUEST), true, "/*");
}
Also used : Dynamic(javax.servlet.FilterRegistration.Dynamic) MapSessionRepository(org.springframework.session.MapSessionRepository) Session(org.springframework.session.Session) MapSession(org.springframework.session.MapSession) SessionRepositoryFilter(org.springframework.session.web.http.SessionRepositoryFilter)

Example 20 with Session

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

the class SessionRepositoryFilterTests method doFilterAdapterOnNewSession.

@Test
public void doFilterAdapterOnNewSession() throws Exception {
    this.filter.setHttpSessionIdResolver(this.strategy);
    doFilter(new DoInFilter() {

        @Override
        public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrappedResponse) throws IOException {
            wrappedRequest.getSession();
        }
    });
    HttpServletRequest request = (HttpServletRequest) this.chain.getRequest();
    Session session = this.sessionRepository.findById(request.getSession().getId());
    verify(this.strategy).setSessionId(any(HttpServletRequest.class), any(HttpServletResponse.class), eq(session.getId()));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) HttpSession(javax.servlet.http.HttpSession) 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