org.springframework.session.events.SessionCreatedEvent Java Examples

The following examples show how to use org.springframework.session.events.SessionCreatedEvent. 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: SessionEventHttpSessionListenerAdapter.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Override
public void onApplicationEvent(AbstractSessionEvent event) {
	if (this.listeners.isEmpty()) {
		return;
	}

	HttpSessionEvent httpSessionEvent = createHttpSessionEvent(event);

	for (HttpSessionListener listener : this.listeners) {
		if (event instanceof SessionDestroyedEvent) {
			listener.sessionDestroyed(httpSessionEvent);
		}
		else if (event instanceof SessionCreatedEvent) {
			listener.sessionCreated(httpSessionEvent);
		}
	}
}
 
Example #2
Source File: SessionEventHazelcastIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Test
void expiredSessionTest() throws InterruptedException {
	S sessionToSave = this.repository.createSession();

	this.repository.save(sessionToSave);

	assertThat(this.registry.receivedEvent(sessionToSave.getId())).isTrue();
	assertThat(this.registry.<SessionCreatedEvent>getEvent(sessionToSave.getId()))
			.isInstanceOf(SessionCreatedEvent.class);
	this.registry.clear();

	assertThat(sessionToSave.getMaxInactiveInterval())
			.isEqualTo(Duration.ofSeconds(MAX_INACTIVE_INTERVAL_IN_SECONDS));

	assertThat(this.registry.receivedEvent(sessionToSave.getId())).isTrue();
	assertThat(this.registry.<SessionExpiredEvent>getEvent(sessionToSave.getId()))
			.isInstanceOf(SessionExpiredEvent.class);

	assertThat(this.repository.findById(sessionToSave.getId())).isNull();
}
 
Example #3
Source File: SessionEventHazelcastIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Test
void deletedSessionTest() throws InterruptedException {
	S sessionToSave = this.repository.createSession();

	this.repository.save(sessionToSave);

	assertThat(this.registry.receivedEvent(sessionToSave.getId())).isTrue();
	assertThat(this.registry.<SessionCreatedEvent>getEvent(sessionToSave.getId()))
			.isInstanceOf(SessionCreatedEvent.class);
	this.registry.clear();

	this.repository.deleteById(sessionToSave.getId());

	assertThat(this.registry.receivedEvent(sessionToSave.getId())).isTrue();
	assertThat(this.registry.<SessionDeletedEvent>getEvent(sessionToSave.getId()))
			.isInstanceOf(SessionDeletedEvent.class);

	assertThat(this.repository.findById(sessionToSave.getId())).isNull();
}
 
Example #4
Source File: SessionEventHazelcastIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Test // gh-1077
void changeSessionIdNoEventTest() throws InterruptedException {
	S sessionToSave = this.repository.createSession();
	sessionToSave.setMaxInactiveInterval(Duration.ofMinutes(30));

	this.repository.save(sessionToSave);

	assertThat(this.registry.receivedEvent(sessionToSave.getId())).isTrue();
	assertThat(this.registry.<SessionCreatedEvent>getEvent(sessionToSave.getId()))
			.isInstanceOf(SessionCreatedEvent.class);
	this.registry.clear();

	sessionToSave.changeSessionId();
	this.repository.save(sessionToSave);

	assertThat(this.registry.receivedEvent(sessionToSave.getId())).isFalse();
}
 
Example #5
Source File: SessionEventHazelcastIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 6 votes vote down vote up
@Test // gh-1300
void updateMaxInactiveIntervalTest() throws InterruptedException {
	S sessionToSave = this.repository.createSession();
	sessionToSave.setMaxInactiveInterval(Duration.ofMinutes(30));
	this.repository.save(sessionToSave);

	assertThat(this.registry.receivedEvent(sessionToSave.getId())).isTrue();
	assertThat(this.registry.<SessionCreatedEvent>getEvent(sessionToSave.getId()))
			.isInstanceOf(SessionCreatedEvent.class);
	this.registry.clear();

	S sessionToUpdate = this.repository.findById(sessionToSave.getId());
	sessionToUpdate.setLastAccessedTime(Instant.now());
	sessionToUpdate.setMaxInactiveInterval(Duration.ofSeconds(1));
	this.repository.save(sessionToUpdate);

	assertThat(this.registry.receivedEvent(sessionToUpdate.getId())).isTrue();
	assertThat(this.registry.<SessionExpiredEvent>getEvent(sessionToUpdate.getId()))
			.isInstanceOf(SessionExpiredEvent.class);
	assertThat(this.repository.findById(sessionToUpdate.getId())).isNull();
}
 
Example #6
Source File: FixedMapSessionRepository.java    From metasfresh-webui-api-legacy with GNU General Public License v3.0 5 votes vote down vote up
@Override
public ExpiringSession createSession()
{
	final ExpiringSession result = new MapSession();
	if (defaultMaxInactiveInterval != null)
	{
		result.setMaxInactiveIntervalInSeconds(defaultMaxInactiveInterval);
	}

	// Fire event
	applicationEventPublisher.publishEvent(new SessionCreatedEvent(this, result.getId()));

	return result;
}
 
Example #7
Source File: SessionEventsListener.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void onApplicationEvent(AbstractSessionEvent event) {
    if (event instanceof SessionCreatedEvent) {
        sessionCreatedEvents++;
    }
    if (event instanceof SessionDeletedEvent) {
        sessionDeletedEvents++;
    }
    if (event instanceof SessionExpiredEvent) {
        sessionExpiredEvents++;
    }
}
 
Example #8
Source File: SessionEventHttpSessionListenerAdapterTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@BeforeEach
void setup() {
	MockitoAnnotations.initMocks(this);
	this.listener = new SessionEventHttpSessionListenerAdapter(Arrays.asList(this.listener1, this.listener2));
	this.listener.setServletContext(new MockServletContext());

	Session session = new MapSession();
	this.destroyed = new SessionDestroyedEvent(this, session);
	this.created = new SessionCreatedEvent(this, session);
}
 
Example #9
Source File: HazelcastIndexedSessionRepository.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Override
public void entryAdded(EntryEvent<String, MapSession> event) {
	MapSession session = event.getValue();
	if (session.getId().equals(session.getOriginalId())) {
		if (logger.isDebugEnabled()) {
			logger.debug("Session created with id: " + session.getId());
		}
		this.eventPublisher.publishEvent(new SessionCreatedEvent(this, session));
	}
}
 
Example #10
Source File: SessionEventHazelcastIndexedSessionRepositoryTests.java    From spring-session with Apache License 2.0 5 votes vote down vote up
@Test
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));
}
 
Example #11
Source File: RedisIndexedSessionRepository.java    From spring-session with Apache License 2.0 4 votes vote down vote up
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));
}
 
Example #12
Source File: RedisIndexedSessionRepositoryITests.java    From spring-session with Apache License 2.0 4 votes vote down vote up
@Test
void saves() throws InterruptedException {
	String username = "saves-" + System.currentTimeMillis();

	String usernameSessionKey = "RedisIndexedSessionRepositoryITests: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);
}