use of org.springframework.session.events.SessionCreatedEvent 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));
}
use of org.springframework.session.events.SessionCreatedEvent in project spring-session by spring-projects.
the class SessionEventHttpSessionListenerAdapterTests method setup.
@Before
public void setup() {
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);
}
use of org.springframework.session.events.SessionCreatedEvent in project metasfresh-webui-api by metasfresh.
the class FixedMapSessionRepository method createSession.
@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;
}
use of org.springframework.session.events.SessionCreatedEvent in project redisson by redisson.
the class RedissonSessionRepository method onMessage.
@Override
public void onMessage(String pattern, String channel, String body) {
if (createdTopic.getPatternNames().contains(pattern)) {
RedissonSession session = getSession(body);
if (session != null) {
publishEvent(new SessionCreatedEvent(this, session));
}
} else if (deletedTopic.getPatternNames().contains(pattern)) {
String id = body.split(":")[1];
RedissonSession session = new RedissonSession(id);
if (session.load()) {
session.clearPrincipal();
publishEvent(new SessionDeletedEvent(this, session));
} else {
publishEvent(new SessionDeletedEvent(this, id));
}
} else if (expiredTopic.getPatternNames().contains(pattern)) {
String id = body.split(":")[1];
RedissonSession session = new RedissonSession(id);
if (session.load()) {
session.clearPrincipal();
publishEvent(new SessionExpiredEvent(this, session));
} else {
publishEvent(new SessionExpiredEvent(this, id));
}
}
}
use of org.springframework.session.events.SessionCreatedEvent 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);
}
Aggregations