Java Code Examples for org.springframework.session.MapSession#setLastAccessedTime()

The following examples show how to use org.springframework.session.MapSession#setLastAccessedTime() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RedissonSessionRepository.java    From redisson with Apache License 2.0 6 votes vote down vote up
private MapSession loadSession(String sessionId) {
    RMap<String, Object> map = redisson.getMap(keyPrefix + sessionId, new CompositeCodec(StringCodec.INSTANCE, redisson.getConfig().getCodec()));
    Set<Entry<String, Object>> entrySet = map.readAllEntrySet();
    if (entrySet.isEmpty()) {
        return null;
    }
    
    MapSession delegate = new MapSession(sessionId);
    for (Entry<String, Object> entry : entrySet) {
        if ("session:creationTime".equals(entry.getKey())) {
            delegate.setCreationTime(Instant.ofEpochMilli((Long) entry.getValue()));
        } else if ("session:lastAccessedTime".equals(entry.getKey())) {
            delegate.setLastAccessedTime(Instant.ofEpochMilli((Long) entry.getValue()));
        } else if ("session:maxInactiveInterval".equals(entry.getKey())) {
            delegate.setMaxInactiveInterval(Duration.ofSeconds((Long) entry.getValue()));
        } else if (entry.getKey().startsWith(SESSION_ATTR_PREFIX)) {
            delegate.setAttribute(entry.getKey().substring(SESSION_ATTR_PREFIX.length()), entry.getValue());
        }
    }
    return delegate;
}
 
Example 2
Source File: SessionUpdateEntryProcessor.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Override
public Object process(Map.Entry<String, MapSession> entry) {
	MapSession value = entry.getValue();
	if (value == null) {
		return Boolean.FALSE;
	}
	if (this.lastAccessedTime != null) {
		value.setLastAccessedTime(this.lastAccessedTime);
	}
	if (this.maxInactiveInterval != null) {
		value.setMaxInactiveInterval(this.maxInactiveInterval);
	}
	if (this.delta != null) {
		for (final Map.Entry<String, Object> attribute : this.delta.entrySet()) {
			if (attribute.getValue() != null) {
				value.setAttribute(attribute.getKey(), attribute.getValue());
			}
			else {
				value.removeAttribute(attribute.getKey());
			}
		}
	}
	entry.setValue(value);
	return Boolean.TRUE;
}
 
Example 3
Source File: JdbcIndexedSessionRepository.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Override
public List<JdbcSession> extractData(ResultSet rs) throws SQLException, DataAccessException {
	List<JdbcSession> sessions = new ArrayList<>();
	while (rs.next()) {
		String id = rs.getString("SESSION_ID");
		JdbcSession session;
		if (sessions.size() > 0 && getLast(sessions).getId().equals(id)) {
			session = getLast(sessions);
		}
		else {
			MapSession delegate = new MapSession(id);
			String primaryKey = rs.getString("PRIMARY_ID");
			delegate.setCreationTime(Instant.ofEpochMilli(rs.getLong("CREATION_TIME")));
			delegate.setLastAccessedTime(Instant.ofEpochMilli(rs.getLong("LAST_ACCESS_TIME")));
			delegate.setMaxInactiveInterval(Duration.ofSeconds(rs.getInt("MAX_INACTIVE_INTERVAL")));
			session = new JdbcSession(delegate, primaryKey, false);
		}
		String attributeName = rs.getString("ATTRIBUTE_NAME");
		if (attributeName != null) {
			byte[] bytes = getLobHandler().getBlobAsBytes(rs, "ATTRIBUTE_BYTES");
			session.delegate.setAttribute(attributeName, lazily(() -> deserialize(bytes)));
		}
		sessions.add(session);
	}
	return sessions;
}
 
Example 4
Source File: RedisSessionMapper.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Override
public MapSession apply(Map<String, Object> map) {
	Assert.notEmpty(map, "map must not be empty");
	MapSession session = new MapSession(this.sessionId);
	Long creationTime = (Long) map.get(CREATION_TIME_KEY);
	if (creationTime == null) {
		handleMissingKey(CREATION_TIME_KEY);
	}
	session.setCreationTime(Instant.ofEpochMilli(creationTime));
	Long lastAccessedTime = (Long) map.get(LAST_ACCESSED_TIME_KEY);
	if (lastAccessedTime == null) {
		handleMissingKey(LAST_ACCESSED_TIME_KEY);
	}
	session.setLastAccessedTime(Instant.ofEpochMilli(lastAccessedTime));
	Integer maxInactiveInterval = (Integer) map.get(MAX_INACTIVE_INTERVAL_KEY);
	if (maxInactiveInterval == null) {
		handleMissingKey(MAX_INACTIVE_INTERVAL_KEY);
	}
	session.setMaxInactiveInterval(Duration.ofSeconds(maxInactiveInterval));
	map.forEach((name, value) -> {
		if (name.startsWith(ATTRIBUTE_PREFIX)) {
			session.setAttribute(name.substring(ATTRIBUTE_PREFIX.length()), value);
		}
	});
	return session;
}
 
Example 5
Source File: RedisIndexedSessionRepository.java    From spring-session with Apache License 2.0 6 votes vote down vote up
private MapSession loadSession(String id, Map<Object, Object> entries) {
	MapSession loaded = new MapSession(id);
	for (Map.Entry<Object, Object> entry : entries.entrySet()) {
		String key = (String) entry.getKey();
		if (RedisSessionMapper.CREATION_TIME_KEY.equals(key)) {
			loaded.setCreationTime(Instant.ofEpochMilli((long) entry.getValue()));
		}
		else if (RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY.equals(key)) {
			loaded.setMaxInactiveInterval(Duration.ofSeconds((int) entry.getValue()));
		}
		else if (RedisSessionMapper.LAST_ACCESSED_TIME_KEY.equals(key)) {
			loaded.setLastAccessedTime(Instant.ofEpochMilli((long) entry.getValue()));
		}
		else if (key.startsWith(RedisSessionMapper.ATTRIBUTE_PREFIX)) {
			loaded.setAttribute(key.substring(RedisSessionMapper.ATTRIBUTE_PREFIX.length()), entry.getValue());
		}
	}
	return loaded;
}
 
Example 6
Source File: RedisIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void delete() {
	String attrName = "attrName";
	MapSession expected = new MapSession();
	expected.setLastAccessedTime(Instant.now().minusSeconds(60));
	expected.setAttribute(attrName, "attrValue");
	given(this.redisOperations.boundHashOps(anyString())).willReturn(this.boundHashOperations);
	given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);
	Map map = map(RedisIndexedSessionRepository.getSessionAttrNameKey(attrName), expected.getAttribute(attrName),
			RedisSessionMapper.CREATION_TIME_KEY, expected.getCreationTime().toEpochMilli(),
			RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, (int) expected.getMaxInactiveInterval().getSeconds(),
			RedisSessionMapper.LAST_ACCESSED_TIME_KEY, expected.getLastAccessedTime().toEpochMilli());
	given(this.boundHashOperations.entries()).willReturn(map);
	given(this.redisOperations.boundSetOps(anyString())).willReturn(this.boundSetOperations);

	String id = expected.getId();
	this.redisRepository.deleteById(id);

	assertThat(getDelta().get(RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY)).isEqualTo(0);
	verify(this.redisOperations, atLeastOnce()).delete(getKey("expires:" + id));
	verify(this.redisOperations, never()).boundValueOps(getKey("expires:" + id));
}
 
Example 7
Source File: SpringSessionBackedSessionRegistryTest.java    From spring-session with Apache License 2.0 5 votes vote down vote up
private Session createSession(String sessionId, String userName, Instant lastAccessed) {
	MapSession session = new MapSession(sessionId);
	session.setLastAccessedTime(lastAccessed);
	Authentication authentication = mock(Authentication.class);
	when(authentication.getName()).thenReturn(userName);
	SecurityContextImpl securityContext = new SecurityContextImpl();
	securityContext.setAuthentication(authentication);
	session.setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
	return session;
}
 
Example 8
Source File: HazelcastIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
void getSessionExpired() {
	verify(this.sessions, times(1)).addEntryListener(any(MapListener.class), anyBoolean());

	MapSession expired = new MapSession();
	expired.setLastAccessedTime(Instant.now().minusSeconds(MapSession.DEFAULT_MAX_INACTIVE_INTERVAL_SECONDS + 1));
	given(this.sessions.get(eq(expired.getId()))).willReturn(expired);

	HazelcastSession session = this.repository.findById(expired.getId());

	assertThat(session).isNull();
	verify(this.sessions, times(1)).get(eq(expired.getId()));
	verify(this.sessions, times(1)).remove(eq(expired.getId()));
	verifyZeroInteractions(this.sessions);
}
 
Example 9
Source File: RedisSessionRepositoryTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
private RedisSession createTestSession(Map<String, Object> attributes) {
	MapSession cached = new MapSession(TEST_SESSION_ID);
	cached.setCreationTime(Instant.EPOCH);
	cached.setLastAccessedTime(Instant.EPOCH);
	attributes.forEach(cached::setAttribute);
	return this.sessionRepository.new RedisSession(cached, false);
}
 
Example 10
Source File: RedisIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void getSessionFound() {
	String attribute1 = "attribute1";
	String attribute2 = "attribute2";
	MapSession expected = new MapSession();
	expected.setLastAccessedTime(Instant.now().minusSeconds(60));
	expected.setAttribute(attribute1, "test");
	expected.setAttribute(attribute2, null);
	given(this.redisOperations.boundHashOps(getKey(expected.getId()))).willReturn(this.boundHashOperations);
	Map map = map(RedisIndexedSessionRepository.getSessionAttrNameKey(attribute1),
			expected.getAttribute(attribute1), RedisIndexedSessionRepository.getSessionAttrNameKey(attribute2),
			expected.getAttribute(attribute2), RedisSessionMapper.CREATION_TIME_KEY,
			expected.getCreationTime().toEpochMilli(), RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY,
			(int) expected.getMaxInactiveInterval().getSeconds(), RedisSessionMapper.LAST_ACCESSED_TIME_KEY,
			expected.getLastAccessedTime().toEpochMilli());
	given(this.boundHashOperations.entries()).willReturn(map);

	RedisSession session = this.redisRepository.findById(expected.getId());
	assertThat(session.getId()).isEqualTo(expected.getId());
	assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames());
	assertThat(session.<String>getAttribute(attribute1)).isEqualTo(expected.getAttribute(attribute1));
	assertThat(session.<String>getAttribute(attribute2)).isEqualTo(expected.getAttribute(attribute2));
	assertThat(session.getCreationTime().truncatedTo(ChronoUnit.MILLIS))
			.isEqualTo(expected.getCreationTime().truncatedTo(ChronoUnit.MILLIS));
	assertThat(session.getMaxInactiveInterval()).isEqualTo(expected.getMaxInactiveInterval());
	assertThat(session.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS))
			.isEqualTo(expected.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS));
}
 
Example 11
Source File: ReactiveRedisSessionRepositoryTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
@SuppressWarnings("unchecked")
void getSessionFound() {
	given(this.redisOperations.opsForHash()).willReturn(this.hashOperations);
	String attribute1 = "attribute1";
	String attribute2 = "attribute2";
	MapSession expected = new MapSession("test");
	expected.setLastAccessedTime(Instant.now().minusSeconds(60));
	expected.setAttribute(attribute1, "test");
	expected.setAttribute(attribute2, null);
	Map map = map(RedisSessionMapper.ATTRIBUTE_PREFIX + attribute1, expected.getAttribute(attribute1),
			RedisSessionMapper.ATTRIBUTE_PREFIX + attribute2, expected.getAttribute(attribute2),
			RedisSessionMapper.CREATION_TIME_KEY, expected.getCreationTime().toEpochMilli(),
			RedisSessionMapper.MAX_INACTIVE_INTERVAL_KEY, (int) expected.getMaxInactiveInterval().getSeconds(),
			RedisSessionMapper.LAST_ACCESSED_TIME_KEY, expected.getLastAccessedTime().toEpochMilli());
	given(this.hashOperations.entries(anyString())).willReturn(Flux.fromIterable(map.entrySet()));

	StepVerifier.create(this.repository.findById("test")).consumeNextWith((session) -> {
		verify(this.redisOperations).opsForHash();
		verify(this.hashOperations).entries(anyString());
		verifyZeroInteractions(this.redisOperations);
		verifyZeroInteractions(this.hashOperations);

		assertThat(session.getId()).isEqualTo(expected.getId());
		assertThat(session.getAttributeNames()).isEqualTo(expected.getAttributeNames());
		assertThat(session.<String>getAttribute(attribute1)).isEqualTo(expected.getAttribute(attribute1));
		assertThat(session.<String>getAttribute(attribute2)).isEqualTo(expected.getAttribute(attribute2));
		assertThat(session.getCreationTime().truncatedTo(ChronoUnit.MILLIS))
				.isEqualTo(expected.getCreationTime().truncatedTo(ChronoUnit.MILLIS));
		assertThat(session.getMaxInactiveInterval()).isEqualTo(expected.getMaxInactiveInterval());
		assertThat(session.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS))
				.isEqualTo(expected.getLastAccessedTime().truncatedTo(ChronoUnit.MILLIS));
	}).verifyComplete();
}