Questo contenuto non è disponibile nella lingua selezionata.

Chapter 2. Externalizing sessions with Spring Session


Store session data for Spring applications in Data Grid caches and independently of the container.

2.1. Externalizing Sessions with Spring Session

Use the Spring Session API to externalize session data to Data Grid.

Procedure

  1. Add dependencies to your pom.xml.

    • Embedded caches: infinispan-spring6-embedded
    • Remote caches: infinispan-spring6-remote

      The following example is for remote caches:

      <dependencies>
          <dependency>
              <groupId>org.infinispan</groupId>
              <artifactId>infinispan-core</artifactId>
          </dependency>
          <dependency>
              <groupId>org.infinispan</groupId>
              <artifactId>infinispan-spring6-remote</artifactId>
          </dependency>
          <dependency>
              <groupId>org.springframework</groupId>
              <artifactId>spring-context</artifactId>
              <version>${version.spring}</version>
          </dependency>
          <dependency>
             <groupId>org.springframework.session</groupId>
             <artifactId>spring-session-core</artifactId>
             <version>${version.spring}</version>
         </dependency>
         <dependency>
             <groupId>org.springframework</groupId>
             <artifactId>spring-web</artifactId>
             <version>${version.spring}</version>
         </dependency>
      </dependencies>
  2. Specify the appropriate FactoryBean to expose a CacheManager instance.

    • Embedded caches: SpringEmbeddedCacheManagerFactoryBean
    • Remote caches: SpringRemoteCacheManagerFactoryBean
  3. Enable Spring Session with the appropriate annotation.

    • Embedded caches: @EnableInfinispanEmbeddedHttpSession
    • Remote caches: @EnableInfinispanRemoteHttpSession

      These annotations have optional parameters:

      • maxInactiveIntervalInSeconds sets session expiration time in seconds. The default is 1800.
      • cacheName specifies the name of the cache that stores sessions. The default is sessions.

The following example shows a complete, annotation-based configuration:

@EnableInfinispanEmbeddedHttpSession
@Configuration
public class Config {

   @Bean
   public SpringEmbeddedCacheManagerFactoryBean springCacheManager() {
      return new SpringEmbeddedCacheManagerFactoryBean();
   }

   //An optional configuration bean responsible for replacing the default
   //cookie that obtains configuration.
   //For more information refer to the Spring Session documentation.
   @Bean
   public HttpSessionIdResolver httpSessionIdResolver() {
       return HeaderHttpSessionIdResolver.xAuthToken();
   }
}
Red Hat logoGithubRedditYoutubeTwitter

Formazione

Prova, acquista e vendi

Community

Informazioni sulla documentazione di Red Hat

Aiutiamo gli utenti Red Hat a innovarsi e raggiungere i propri obiettivi con i nostri prodotti e servizi grazie a contenuti di cui possono fidarsi.

Rendiamo l’open source più inclusivo

Red Hat si impegna a sostituire il linguaggio problematico nel codice, nella documentazione e nelle proprietà web. Per maggiori dettagli, visita ilBlog di Red Hat.

Informazioni su Red Hat

Forniamo soluzioni consolidate che rendono più semplice per le aziende lavorare su piattaforme e ambienti diversi, dal datacenter centrale all'edge della rete.

© 2024 Red Hat, Inc.