Skip to content

Commit

Permalink
Make sessionIdGenerator configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
ghillert committed Aug 30, 2024
1 parent c126ff7 commit fe9dc30
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ public void setIndexResolver(IndexResolver<Session> indexResolver) {
this.indexResolver = indexResolver;
}

/**
* Sets the {@link SessionIdGenerator} to be used when generating a new session id. If not specified
* a {@link UuidSessionIdGenerator} will be used.
* @param sessionIdGenerator the {@link SessionIdGenerator} to use. Must not be null.
*/
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
Assert.notNull(sessionIdGenerator, "sessionIdGenerator cannot be null");
this.sessionIdGenerator = sessionIdGenerator;
}

/**
* Set the name of map used to store sessions.
* @param sessionMapName the session map name
Expand Down Expand Up @@ -181,7 +191,7 @@ public boolean isUseEntryProcessor() {

@Override
public CoherenceSpringSession createSession() {
MapSession cached = new MapSession();
MapSession cached = new MapSession(this.sessionIdGenerator);
if (this.defaultMaxInactiveInterval != null) {
cached.setMaxInactiveInterval(this.defaultMaxInactiveInterval);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2023, Oracle and/or its affiliates.
* Copyright (c) 2021, 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
Expand Down Expand Up @@ -32,6 +32,7 @@
import org.springframework.session.MapSession;
import org.springframework.session.SaveMode;
import org.springframework.session.Session;
import org.springframework.session.SessionIdGenerator;
import org.springframework.session.config.SessionRepositoryCustomizer;
import org.springframework.session.config.annotation.web.http.SpringHttpSessionConfiguration;
import org.springframework.session.web.http.SessionRepositoryFilter;
Expand Down Expand Up @@ -65,6 +66,7 @@ public class CoherenceHttpSessionConfiguration extends SpringHttpSessionConfigur
private Coherence coherence;

private IndexResolver<Session> indexResolver;
private SessionIdGenerator sessionIdGenerator;
private List<SessionRepositoryCustomizer<CoherenceIndexedSessionRepository>> sessionRepositoryCustomizers;

@Bean
Expand Down Expand Up @@ -114,6 +116,11 @@ public void setIndexResolver(IndexResolver<Session> indexResolver) {
this.indexResolver = indexResolver;
}

@Autowired(required = false)
public void setSessionIdGenerator(SessionIdGenerator sessionIdGenerator) {
this.sessionIdGenerator = sessionIdGenerator;
}

@Autowired(required = false)
public void setSessionRepositoryCustomizer(
ObjectProvider<SessionRepositoryCustomizer<CoherenceIndexedSessionRepository>> sessionRepositoryCustomizers) {
Expand Down Expand Up @@ -157,6 +164,9 @@ private CoherenceIndexedSessionRepository createCoherenceIndexedSessionRepositor
if (this.indexResolver != null) {
sessionRepository.setIndexResolver(this.indexResolver);
}
if (this.sessionIdGenerator != null) {
sessionRepository.setSessionIdGenerator(this.sessionIdGenerator);
}
if (StringUtils.hasText(this.sessionMapName)) {
sessionRepository.setSessionMapName(this.sessionMapName);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.spring.session;

import com.oracle.coherence.spring.configuration.annotation.EnableCoherence;
import com.oracle.coherence.spring.session.config.annotation.web.http.EnableCoherenceHttpSession;
import com.oracle.coherence.spring.session.support.FixedSessionIdGenerator;
import com.tangosol.net.Coherence;
import com.tangosol.net.Session;
import com.tangosol.net.cache.CacheMap;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.MapSession;
import org.springframework.session.SessionIdGenerator;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;

import static org.assertj.core.api.Assertions.assertThat;

/**
* Tests for providing a custom {@link SessionIdGenerator} using embedded Coherence.
*
* @author Gunnar Hillert
*/
@DirtiesContext
@SpringJUnitWebConfig
class CustomSessionIdGeneratorTests {

private static final String SESSION_ID = "test-session-id";

@Autowired
private Coherence coherenceInstance;

@Autowired
protected CoherenceIndexedSessionRepository repository;

CustomSessionIdGeneratorTests() {
System.setProperty("coherence.cluster", "CustomSessionIdGeneratorTests");
System.setProperty("coherence.ttl", "0");
System.setProperty("java.net.preferIPv4Stack", "true");
System.setProperty("coherence.wka", "127.0.0.1");
}

@Test
void createAndDestroyCoherenceSessionAndValidatedSessionId() {

final CoherenceSpringSession sessionToSave = this.repository.createSession();
final String sessionId = sessionToSave.getId();

assertThat(sessionId).isEqualTo(SESSION_ID);

this.repository.save(sessionToSave);

final Session coherenceSession = this.coherenceInstance.getSession();
final CacheMap<String, MapSession> cacheMap = coherenceSession
.getCache(CoherenceIndexedSessionRepository.DEFAULT_SESSION_MAP_NAME);

assertThat(cacheMap.get(SESSION_ID)).isEqualTo(sessionToSave.getDelegate());

this.repository.deleteById(SESSION_ID);

assertThat(cacheMap.get(SESSION_ID)).isNull();

}

@EnableCoherenceHttpSession
@EnableCoherence
@Configuration
static class CoherenceSessionConfig {
@Bean
SessionIdGenerator sessionIdGenerator() {
return new FixedSessionIdGenerator(SESSION_ID);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright (c) 2024, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.spring.session.support;

import org.springframework.session.SessionIdGenerator;

/**
* Example of a custom {@link SessionIdGenerator}.
*
* @author Gunnar Hillert
*/
public class FixedSessionIdGenerator implements SessionIdGenerator {

private final String id;

public FixedSessionIdGenerator(String id) {
this.id = id;
}

@Override
public String generate() {
return this.id;
}

}

0 comments on commit fe9dc30

Please sign in to comment.