Table of Contents
Spring Session provides an API and implementations for managing a user’s session information. It also provides transparent integration with:
javax.servlet.http.HttpSession
in an application container
(e.g. Tomcat) neutral way.
Additional features include:
javax.servlet.http.HttpSession
alive
when receiving WebSocket
messages
If you are looking to get started with Spring Session right of way, the best place to start is with our Sample Applications.
Table 2.1. Sample Applications using Spring Boot
Source | Description | Guide |
---|---|---|
Demonstrates how to use Spring Session to manage the |
Table 2.2. Sample Applications using Spring Java-based configuration
Source | Description | Guide |
---|---|---|
Demonstrates how to use Spring Session to manage the | ||
Demonstrates how to use Spring Session to manage the |
Table 2.3. Sample Applications using Spring XML-based configuration
Source | Description | Guide |
---|---|---|
Demonstrates how to use Spring Session to manage the | ||
Demonstrates how to use Spring Session to manage the |
Spring Session provides transparent integration with HttpSession
. This means that developers can switch
the javax.servlet.http.HttpSession
implementation out with an implementation that is backed by Spring Session.
We already mentioned that Spring Session provides transparent integration with HttpSession
, but what benefits
do we get out of this?
javax.servlet.http.HttpSession
alive
when receiving WebSocket
messages
When Apache Geode is used with Spring Session, a web application’s
javax.servlet.http.HttpSession
can be replaced with a clustered implementation managed by Apache Geode
and conveniently accessed using Spring Session’s API.
The two most common topologies to manage Spring Sessions using Apache Geode include:
Additionally, Apache Geode supports site-to-site replication using WAN technology. The ability to configure and use Apache Geode’s WAN support is independent of Spring Session, and beyond the scope of this document.
More details on configuring Apache Geode WAN functionality using Spring Data Geode can be found here.
The Client-Server topology will probably be the more common configuration choice among users when using Apache Geode as a provider in Spring Session since a Geode server will have significantly different and unique JVM heap requirements as compared to the application. Using a Client-Server topology enables an application to manage (e.g. replicate) application state independently from other application processes.
In a Client-Server topology, an application using Spring Session will open 1 or more connections to a remote cluster
of Geode servers that will manage access to all HttpSession
state.
You can configure a Client-Server topology with either:
This section describes how to configure Apache Geode’s Client-Server topology with Java-based configuration.
![]() | Note |
---|---|
The HttpSession with Apache Geode (Client-Server) provides a working sample demonstrating how to
integrate Spring Session with Apache Geode to manage the |
After adding the required dependencies and repository declarations, we can create the Spring configuration.
The Spring configuration is responsible for creating a Servlet Filter
that replaces the HttpSession
with an implementation backed by Spring Session and Apache Geode.
Add the following Spring configuration:
@ClientCacheApplication(name = "SpringSessionDataGeodeClientJavaConfigSample", logLevel = "warning", pingInterval = 5000L, readTimeout = 15000, retryAttempts = 1, subscriptionEnabled = true)@EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 30, poolName = "DEFAULT")
public class ClientConfig extends IntegrationTestConfig { // Required to resolve property placeholders in Spring @Value annotations. @Bean static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } @Bean ClientCacheConfigurer clientCacheServerPortConfigurer( @Value("${spring.session.data.geode.cache.server.port:40404}") int port) {
return (beanName, clientCacheFactoryBean) -> clientCacheFactoryBean.setServers(Collections.singletonList( newConnectionEndpoint("localhost", port))); } }
First, we declare our Web application to be a Geode cache client by annotating our | |
| |
Then, we adjust the port used by the cache client |
![]() | Tip |
---|---|
In typical Geode production deployments, where the cluster includes potentially hundreds or thousands of Geode servers (data nodes), it is more common for clients to connect to 1 or more Geode Locators running in the cluster. A Locator passes meta-data to clients about the servers available in the cluster, the server load and which servers have the client’s data of interest, which is particularly important for direct, single-hop data access and latency-sensitive operations. See more details about the Client/Server Deployment in the Apache Geode User Guide. |
![]() | Note |
---|---|
For more information on configuring _Spring Data Geode, refer to the Reference Guide. |
@EnableGemFireHttpSession
enables a developer to configure certain aspects of both Spring Session and Apache Geode
out-of-the-box using the following attributes:
clientRegionShortcut
- specifies Apache Geode data management policy
on the client with a Geode ClientRegionShortcut
(default is PROXY
). This attribute is only used when configuring a client Region
.
indexableSessionAttributes
- Identifies the Session attributes by name that should be indexed for querying operations.
Only Session attributes identified by name will be indexed.
maxInactiveIntervalInSeconds
- controls HttpSession idle-timeout expiration (defaults to 30 minutes).
poolName
- name of the dedicated Apache Geode Pool
used to connect a client to the cluster of servers. The attribute
is only used when the application is a cache client. Defaults to gemfirePool
.
regionName
- specifies the name of the Apache Geode Region
used to store and manage HttpSession
state
(default is "ClusteredSpringSessions").
serverRegionShortcut
- specifies Apache Geode data management policy
on the server using a Geode RegionShortcut
(default is PARTITION
). This attribute is only used when configuring server Regions
, or when a P2P topology is employed.
![]() | Note |
---|---|
It is important to remember that the Apache Geode client |
So far, we only covered one side of the equation. We also need an Apache Geode Server for our cache client to talk to and send Session state to the server to manage.
In this sample, we will use the following Java configuration to spin up an Apache Geode Server:
@CacheServerApplication(name = "SpringSessionSampleJavaConfigGemFireClientServer", logLevel = "warning")@EnableGemFireHttpSession(maxInactiveIntervalInSeconds = 30)
public class ServerConfig { @SuppressWarnings("resource") public static void main(String[] args) throws IOException { new AnnotationConfigApplicationContext(ServerConfig.class).registerShutdownHook(); } // Required to resolve property placeholders in Spring @Value annotations. @Bean static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() { return new PropertySourcesPlaceholderConfigurer(); } @Bean CacheServerConfigurer cacheServerPortConfigurer( @Value("${spring.session.data.geode.cache.server.port:40404}") int port) {
return (beanName, cacheServerFactoryBean) -> { cacheServerFactoryBean.setPort(port); }; } }
First, we use the new Spring Data Geode configuration annotation | |
(Optional) Then, the | |
Finally, we adjust the port that the |
The sample makes use of Spring’s PropertySourcesPlaceholderConfigurer
in order to externalize the sample
application’s configuration using a properties file or with JVM System properties, which ever.
Our Spring Java Configuration created a Spring bean named springSessionRepositoryFilter
that implements javax.servlet.Filter
. The springSessionRepositoryFilter
bean is responsible for replacing the
javax.servlet.http.HttpSession
with a custom implementation backed by Spring Session and Apache Geode.
In order for our Filter
to do its magic, Spring needs to load the ClientConfig
class. We also need to ensure our
Servlet container (i.e. Tomcat) uses our springSessionRepositoryFilter
for every request.
Fortunately, Spring Session provides a utility class named AbstractHttpSessionApplicationInitializer
to make both
of these steps extremely easy.
You can find an example below:
src/main/java/sample/Initializer.java.
public class Initializer extends AbstractHttpSessionApplicationInitializer {public Initializer() { super(ClientConfig.class);
} }
![]() | Note |
---|---|
The name of our class ( |
The first step is to extend | |
|
This section describes how to use Apache Geode’s Client-Server topology to back an HttpSession
with XML-based configuration.
![]() | Note |
---|---|
The HttpSession with Apache Geode (Client-Server) using XML provides a working sample demonstrating
how to integrate Spring Session with Apache Geode to manage the |
After adding the required dependencies and repository declarations, we can create the Spring configuration.
The Spring configuration is responsible for creating a Servlet
Filter
that replaces the javax.servlet.http.HttpSession
with an implementation backed by Spring Session and Apache Geode.
Add the following Spring configuration:
<context:annotation-config/> <context:property-placeholder location="classpath:META-INF/spring/application.properties"/> <bean class="sample.ClientServerReadyBeanPostProcessor"/><util:properties id="gemfireProperties"> <prop key="log-level">${spring.session.data.geode.log-level:warning}</prop> </util:properties>
<gfe:client-cache properties-ref="gemfireProperties" pool-name="gemfirePool"/>
<gfe:pool ping-interval="5000" read-timeout="15000" retry-attempts="1" subscription-enabled="true"> <gfe:server host="${application.geode.client-server.host}" port="${spring.session.data.geode.cache.server.port:${application.geode.client-server.port:40404}}"/> </gfe:pool>
<bean class="org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration" p:maxInactiveIntervalInSeconds="30" p:poolName="DEFAULT"/>
(Optional) First, we can include a | |
We must create an instance of an Apache Geode | |
Then we configure a | |
Finally, a |
![]() | Tip |
---|---|
In typical Geode production deployments, where the cluster includes potentially hundreds or thousands of Geode servers (data nodes), it is more common for clients to connect to 1 or more Geode Locators running in the cluster. A Locator passes meta-data to clients about the servers available in the cluster, the server load and which servers have the client’s data of interest, which is particularly important for direct, single-hop data access and latency-sensitive operations. See more details about the Client/Server Deployment in the Apache Geode User Guide. |
![]() | Note |
---|---|
For more information on configuring _Spring Data Geode, refer to the Reference Guide. |
So far, we only covered one side of the equation. We also need an Apache Geode Server for our cache client to talk to and send Session state to the server to manage.
In this sample, we will use the following XML configuration to spin up an Apache Geode Server:
<context:annotation-config/> <context:property-placeholder location="classpath:META-INF/spring/application.properties"/><util:properties id="gemfireProperties"> <prop key="name">SpringSessionSampleXmlGemFireClientServer</prop> <prop key="log-level">${spring.session.data.gemfire.log-level:warning}</prop> <!-- <prop key="jmx-manager">true</prop> <prop key="jmx-manager-start">true</prop> --> </util:properties>
<gfe:cache properties-ref="gemfireProperties"/>
<gfe:cache-server auto-startup="true" bind-address="${application.geode.client-server.host:localhost}" host-name-for-clients="${application.geode.client-server.host:localhost}" port="${spring.session.data.geode.cache.server.port:${application.geode.client-server.port:40404}}"/>
<bean class="org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration" p:maxInactiveIntervalInSeconds="30"/>
(Optional) First, we can include a | |
We must configure an Apache Geode peer | |
Next, we define a | |
Finally, we enable the same Spring Session functionality we declared in the client XML configuration
by registering an instance of |
The Apache Geode Server gets bootstrapped with the following:
@Configuration@ImportResource("META-INF/spring/session-server.xml")
public class ServerConfig { public static void main(String[] args) { new AnnotationConfigApplicationContext(ServerConfig.class).registerShutdownHook(); } }
![]() | Tip |
---|---|
Rather than defining a simple Java class with a |
Our Spring XML Configuration created a Spring bean named springSessionRepositoryFilter
that implements javax.servlet.Filter
intervace. The springSessionRepositoryFilter
bean is responsible for replacing
the javax.servlet.http.HttpSession
with a custom implementation that is provided by Spring Session and Apache Geode.
In order for our Filter
to do its magic, we need to instruct Spring to load our session-client.xml
configuration file.
We do this with the following configuration:
src/main/webapp/WEB-INF/web.xml.
<context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/spring/session-client.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
The ContextLoaderListener
reads the contextConfigLocation
context parameter value and picks up our session-client.xml configuration file.
Finally, we need to ensure that our Servlet container (i.e. Tomcat) uses our springSessionRepositoryFilter
for every request.
The following snippet performs this last step for us:
src/main/webapp/WEB-INF/web.xml.
<filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>
The DelegatingFilterProxy
will look up a bean by the name of springSessionRepositoryFilter
and cast it to a Filter
. For every HTTP request,
the DelegatingFilterProxy
is invoked, which delegates to the springSessionRepositoryFilter
.
Perhaps a less common approach is to configure the Spring Session application as a peer member in the Geode cluster using the Peer-To-Peer (P2P) topology. In this configuration, the Spring Session application would be an actual server (data node) in the Geode cluster, and not a cache client as before.
One advantage to this approach is the proximity of the application to the application’s state (i.e. its data). However, there are other effective means of accomplishing similar data dependent computations, such as using Geode’s Function Execution. Any of Geode’s other features can be used when Geode is serving as a provider in Spring Session.
P2P is very useful for testing purposes as well as for smaller, more focused and self-contained applications, such as those found in a microservices architecture, and will most certainly improve on your application’s perceived latency, throughput and consistency needs.
You can configure a Peer-To-Peer (P2P) topology with either:
This section describes how to configure Apache Geode’s Peer-To-Peer (P2P) topology to manage an HttpSession
using Java-based configuration.
![]() | Note |
---|---|
The HttpSession with Apache Geode (P2P) provides a working sample demonstrating how to
integrate Spring Session with Apache Geode to manage the |
After adding the required dependencies and repository declarations, we can create the Spring configuration.
The Spring configuration is responsible for creating a Servlet
Filter
that replaces the javax.servlet.http.HttpSession
with an implementation backed by Spring Session and Apache Geode.
Add the following Spring configuration:
@PeerCacheApplication(name = "SpringSessionSampleJavaConfigGemFireP2p", logLevel = "warning")@EnableGemFireHttpSession
@EnableManager(start = true)
public class Config { }
First, we use the new Spring Data Geode configuration annotation | |
Then, the | |
(Optionally) Finally, we annotated the |
![]() | Note |
---|---|
For more information on configuring _Spring Data Geode, refer to the Reference Guide. |
@EnableGemFireHttpSession
enables a developer to configure certain aspects of both Spring Session and Apache Geode
out-of-the-box using the following attributes:
clientRegionShortcut
- specifies Apache Geode data management policy
on the client with a Geode ClientRegionShortcut
(default is PROXY
). This attribute is only used when configuring a client Region
.
indexableSessionAttributes
- Identifies the Session attributes by name that should be indexed for querying operations.
Only Session attributes identified by name will be indexed.
maxInactiveIntervalInSeconds
- controls HttpSession idle-timeout expiration (defaults to 30 minutes).
poolName
- name of the dedicated Apache Geode Pool
used to connect a client to the cluster of servers. The attribute
is only used when the application is a cache client. Defaults to gemfirePool
.
regionName
- specifies the name of the Apache Geode Region
used to store and manage HttpSession
state
(default is "ClusteredSpringSessions").
serverRegionShortcut
- specifies Apache Geode data management policy
on the server using a Geode RegionShortcut
(default is PARTITION
). This attribute is only used when configuring server Regions
, or when a P2P topology is employed.
Our <<[httpsession-spring-java-configuration-gemfire-p2p,Spring Java Configuration>> created a Spring bean named
springSessionRepositoryFilter
that implements javasx.servlet.Filter
. The springSessionRepositoryFilter
bean
is responsible for replacing the javax.servlet.http.HttpSession
with a custom implementation backed by Spring Session
and Apache Geode.
In order for our Filter
to do its magic, Spring needs to load our Config
class. We also need to ensure our Servlet container (i.e. Tomcat) uses our springSessionRepositoryFilter
on every HTTP request.
Fortunately, Spring Session provides a utility class named AbstractHttpSessionApplicationInitializer
to make both
of these steps extremely easy.
You can find an example below:
src/main/java/sample/Initializer.java.
public class Initializer extends AbstractHttpSessionApplicationInitializer {public Initializer() { super(Config.class);
} }
![]() | Note |
---|---|
The name of our class ( |
The first step is to extend | |
|
This section describes how to use Apache Geode’s Peer-To-Peer (P2P) topology to back an HttpSession
using XML-based configuration.
![]() | Note |
---|---|
The HttpSession with Apache Geode (P2P) using XML provides a working sample demonstrating how to
integrate Spring Session with Apache Geode to manage the |
After adding the required dependencies and repository declarations, we can create the Spring configuration.
The Spring configuration is responsible for creating a Servlet
Filter
that replaces the javax.servlet.http.HttpSession
with an implementation backed by Spring Session and Apache Geode.
Add the following Spring configuration:
src/main/webapp/WEB-INF/spring/session.xml.
<context:annotation-config/> <context:property-placeholder/><util:properties id="gemfireProperties"> <prop key="name">SpringSessionSampleXmlGemFireP2p</prop> <prop key="log-level">${spring.session.data.geode.log-level:warning}</prop> <prop key="jmx-manager">true</prop> <prop key="jmx-manager-start">true</prop> </util:properties>
<gfe:cache properties-ref="gemfireProperties" use-bean-factory-locator="false"/>
<bean class="org.springframework.session.data.gemfire.config.annotation.web.http.GemFireHttpSessionConfiguration"/>
(Optional) First, we can include a | |
We must configure an Apache Geode peer | |
Finally, we enable Spring Session functionality by registering an instance of |
![]() | Tip |
---|---|
Additionally, we have configured this data node (server) as a Apache Geode Manager as well using Geode-specific JMX properties that enable JMX client (e.g. Gfsh) to connect to this running server. |
![]() | Note |
---|---|
For more information on configuring _Spring Data Geode, refer to the Reference Guide. |
The Spring XML Configuration created a Spring bean named springSessionRepositoryFilter
that implements javax.servlet.Filter
. The springSessionRepositoryFilter
bean is responsible for replacing
the javax.servlet.http.HttpSession
with a custom implementation that is backed by Spring Session and Apache Geode.
In order for our Filter
to do its magic, we need to instruct Spring to load our session.xml
configuration file.
We do this with the following configuration:
src/main/webapp/WEB-INF/web.xml.
<context-param> <param-name>contextConfigLocation</param-name> <param-value> /WEB-INF/spring/*.xml </param-value> </context-param> <listener> <listener-class> org.springframework.web.context.ContextLoaderListener </listener-class> </listener>
The ContextLoaderListener
reads the contextConfigLocation
context parameter value and picks up our session.xml configuration file.
Finally, we need to ensure that our Servlet container (i.e. Tomcat) uses our springSessionRepositoryFilter
for every HTTP request.
The following snippet performs this last step for us:
src/main/webapp/WEB-INF/web.xml.
<filter> <filter-name>springSessionRepositoryFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>springSessionRepositoryFilter</filter-name> <url-pattern>/*</url-pattern> <dispatcher>REQUEST</dispatcher> <dispatcher>ERROR</dispatcher> </filter-mapping>
The DelegatingFilterProxy
will look up a bean by the name of springSessionRepositoryFilter
and cast it to a Filter
. For every HTTP request
the DelegatingFilterProxy
is invoked, delegating to the springSessionRepositoryFilter
.
Fortunately both javax.servlet.http.HttpSession
and javax.servlet.http.HttpServletRequest
(the API for
obtaining an HttpSession
) are both interfaces. This means that we can provide our own implementations
for each of these APIs.
![]() | Note |
---|---|
This section describes how Spring Session provides transparent integration with |
First, we create a custom javax.servlet.http.HttpServletRequest
that returns a custom implementation of
javax.servlet.http.HttpSession
. It looks something like the following:
public class SessionRepositoryRequestWrapper extends HttpServletRequestWrapper { public SessionRepositoryRequestWrapper(HttpServletRequest original) { super(original); } public HttpSession getSession() { return getSession(true); } public HttpSession getSession(boolean createNew) { // create an HttpSession implementation from Spring Session } // ... other methods delegate to the original HttpServletRequest ... }
Any method that returns an javax.servlet.http.HttpSession
is overridden. All other methods are implemented by
javax.servlet.http.HttpServletRequestWrapper
and simply delegate to the original javax.servlet.http.HttpServletRequest
implementation.
We replace the javax.servlet.http.HttpServletRequest
implementation using a Servlet Filter
called SessionRepositoryFilter
.
The pseudocode can be found below:
public class SessionRepositoryFilter implements Filter { public doFilter(ServletRequest request, ServletResponse response, FilterChain chain) { HttpServletRequest httpRequest = (HttpServletRequest) request; SessionRepositoryRequestWrapper customRequest = new SessionRepositoryRequestWrapper(httpRequest); chain.doFilter(customRequest, response, chain); } // ... }
By passing in a custom javax.servlet.http.HttpServletRequest
implementation into the FilterChain
we ensure that
anything invoked after our Filter
uses the custom javax.servlet.http.HttpSession
implementation.
This highlights why it is important that Spring Session’s SessionRepositoryFilter
must be placed before anything
that interacts with the javax.servlet.http.HttpSession
.
Spring Session supports HttpSessionListener
by translating SessionDestroyedEvent
and SessionCreatedEvent
into
HttpSessionEvent
by declaring SessionEventHttpSessionListenerAdapter
.
To use this support, you need to:
SessionRepository
implementation supports and is configured to fire SessionDestroyedEvent
and SessionCreatedEvent
.
SessionEventHttpSessionListenerAdapter
as a Spring bean.
HttpSessionListener
into the SessionEventHttpSessionListenerAdapter
If you are using the configuration support documented in HttpSession with Apache Geode,
then all you need to do is register every HttpSessionListener
as a bean.
For example, assume you want to support Spring Security’s concurrency control and need to use HttpSessionEventPublisher
you can simply add HttpSessionEventPublisher
as a bean.
A SessionRepository
is in charge of creating, retrieving, and persisting Session
instances.
If possible, developers should not interact directly with a SessionRepository
or a Session
. Instead, developers
should prefer to interact with SessionRepository
and Session
indirectly through the javax.servlet.http.HttpSession
and WebSocket
integration.
Spring Session’s most basic API for using a Session
is the SessionRepository
. The API is intentionally
very simple so that it is easy to provide additional implementations with basic functionality.
Some SessionRepository
implementations may choose to implement FindByIndexNameSessionRepository
also.
For example, Spring Session’s Apache Geode support implements FindByIndexNameSessionRepository
.
The FindByIndexNameSessionRepository
provides a single method to look up all the Sessions
for a particular user.
This is done by ensuring that the session attribute with the name FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME
is populated with the username. It is the responsibility of the developer to ensure the attribute is populated
since Spring Session is not aware of the authentication mechanism being used.
![]() | Note |
---|---|
Some implementations of |
The @EnableSpringHttpSession
annotation can be added to any @Configuration
class to expose the SessionRepositoryFilter
as a bean in the Spring application context named "springSessionRepositoryFilter".
In order to leverage the annotation, a single SessionRepository
bean must be provided.
The @EnableGemFireHttpSession
annotation can be added to any @Configuration
class in place of
the @EnableSpringHttpSession
annotation to expose the SessionRepositoryFilter
as a bean
in the Spring application context named "springSessionRepositoryFilter" and to position Apache Geode as a provider
to manage the javax.servlet.http.HttpSession
.
When using the @EnableGemFireHttpSession
annotation, additional configuration is imported out-of-the-box
that also provides a Geode specific implementation of the SessionRepository
interface, the GemFireOperationsSessionRepository
.
GemFireOperationsSessionRepository
is a SessionRepository
implementation that is implemented using Spring Session Data Geode’s
GemFireOperationsSessionRepository
.
In a web environment, this repository is used in conjunction with the SessionRepositoryFilter
.
This implementation supports SessionCreatedEvents
, SessionDeletedEvents
and SessionDestroyedEvents
through SessionEventHttpSessionListenerAdapter
.
While best practices concerning the proper definition of Indexes that positively impact Geode’s performance is beyond the scope of this document, it is important to realize that Spring Session Data Geode creates and uses Indexes to query and find Sessions efficiently.
Out-of-the-box, Spring Session Data Geode creates 1 Hash-typed Index on the principal name. There are two different
built-in strategies for finding the principal name. The first strategy is that the value of the Session attribute
with the name FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME
will be Indexed to the same index name.
For example:
String indexName = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
session.setAttribute(indexName, username);
Map<String, Session> idToSessions =
this.sessionRepository.findByIndexNameAndIndexValue(indexName, username);
Alternatively, Spring Session Data Geode will map Spring Security’s current Authentication#getName()
to the Index
FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME
.
For example, if you are using Spring Security you can find the current user’s sessions using:
SecurityContext securityContext = SecurityContextHolder.getContext();
Authentication authentication = securityContext.getAuthentication();
String indexName = FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME;
Map<String, Session> idToSessions =
this.sessionRepository.findByIndexNameAndIndexValue(indexName, authentication.getName());
This enables developers using the GemFireOperationsSessionRepository
programmatically to query and find all Sessions
with a given principal name efficiently.
Additionally, Spring Session Data Geode will create a Range-based Index on the implementing Session’s Map-type
attributes
property (i.e. on any arbitrary Session attribute) when a developer identifies 1 or more named Session
attributes that should be indexed by Geode.
Sessions attributes to index can be specified with the indexableSessionAttributes
attribute on the @EnableGemFireHttpSession
annotation. A developer adds this annotation to their Spring application @Configuration
class when s/he wishes to
enable Spring Session’s support for HttpSession
backed by Apache Geode.
String indexName = "name1"; session.setAttribute(indexName, indexValue); Map<String, Session> idToSessions = this.sessionRepository.findByIndexNameAndIndexValue(indexName, indexValue);
![]() | Note |
---|---|
Only Session attribute names identified in the |
However, there is one caveat. Any values stored in indexable Session attributes must implement the java.lang.Comparable<T>
interface. If those object values do not implement Comparable
, then GemFire will throw an error on startup when the
Index is defined for Regions with persistent Session data, or when an attempt is made at runtime to assign the indexable
Session attribute a value that is not Comparable
and the Session is saved to GemFire.
![]() | Note |
---|---|
Any Session attribute that is not indexed may store non- |
To learn more about GemFire’s Range-based Indexes, see Creating Indexes on Map Fields.
To learn more about GemFire Indexing in general, see Working with Indexes.
We are glad to consider you a part of our community. Please find additional information below.
You can get help by asking questions on StackOverflow with the tag spring-session. Similarly we encourage helping others by answering questions on StackOverflow.
The source code can be found on GitHub at https://github.com/spring-projects/spring-session-data-geode
We track issues in GitHub Issues at https://github.com/spring-projects/spring-session-data-geode/issues
We appreciate Pull Requests.
Spring Session Data Geode and Spring Session Data GemFire are Open Source Software released under the Apache 2.0 license.
The minimum requirements for Spring Session are:
@EnableGemFireHttpSession
requires Spring Data Geode 2.0.0.RC2 and Spring Data GemFire 2.0.0.RC2.
@EnableGemFireHttpSession
requires Apache Geode 1.2.0 or Pivotal GemFire 9.1.0.
![]() | Note |
---|---|
At its core Spring Session only has a required dependency on |