Home > Net >  Spring Boot session in Redis
Spring Boot session in Redis

Time:10-13

I am creating a spring boot login register system . By default spring boot stores session in in memory on Server . But in case of production app , i would like to store those session in Redis .

I am using DaoAuthenticationProvider and AuthenticationManager , does i need to do something here or simply download redis dependencies and mention them in pom.xml . After mentioning them i will update application.properties for session management in redis . Is this sufficient to store session in redis ? I will store user data in Mysql .

CodePudding user response:

Updating Dependencies

Before you use Spring Session, you must ensure your dependencies. We assume you are working with a working Spring Boot web application. If you are using Maven, you must add the following dependencies:

pom.xml

<dependencies>
    <!-- ... -->

    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
    </dependency>
</dependencies>

Spring Boot Configuration

src/main/resources/application.properties

    spring.session.store-type=redis # Session store type.
    server.servlet.session.timeout= # Session timeout. If a duration suffix is not specified, seconds is used.
    spring.session.redis.flush-mode=on_save # Sessions flush mode.
    spring.session.redis.namespace=spring:session # Namespace for keys used to store sessions.

Configuring the Redis Connection

src/main/resources/application.properties

    spring.redis.host=localhost # Redis server host.
    spring.redis.password= # Login password of the redis server.
    spring.redis.port=6379 # Redis server port.

Servlet Container Initialization

Spring Boot Configuration created a Spring bean named springSessionRepositoryFilter that implements Filter. The springSessionRepositoryFilter bean is responsible for replacing the HttpSession with a custom implementation that is backed by Spring Session.

In order for our Filter to do its magic, Spring needs to load our Config class. Last, we need to ensure that our servlet container (that is, Tomcat) uses our springSessionRepositoryFilter for every request. Fortunately, Spring Boot takes care of both of these steps for us.

Here is all you need if you have configured it then it is done.

thank you...

CodePudding user response:

Try this way

Adding Spring Session Data Redis to your build This project uses a Maven BOM (Bill of Materials) and a release train to coordinate versions, e.g. Dragonfruit-SR2, 2020.0.3, etc.

Using the BOM with Maven With Maven, you need to import the BOM first:

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-bom</artifactId>
      <version>2020.0.3</version>
      <type>pom</type>
      <scope>import</scope>
    </dependency>
  </dependencies>
</dependencyManagement>COPY
This example is using 2020.0.3, but you plug in the release train version you need.

Notice the use of the <dependencyManagement> section and the import scope.

Next, add your dependencies to the project without a <version>:

<dependencies>
  <dependency>
    <groupId>org.springframework.session</groupId>
    <artifactId>spring-session-data-redis</artifactId>
  </dependency>
</dependencies>
  • Related