paulwong

          WEBFLUX + SPRING SESSION + REACTIVE MONGODB

          添加依賴,pom.xml

          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-webflux</artifactId>
          </dependency>
                  
                  
          <!-- spring session with mongodb -->
          <dependency>
             <groupId>org.springframework.session</groupId>
             <artifactId>spring-session-data-mongodb</artifactId>
          </dependency>
                  
          <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-data-mongodb-reactive</artifactId>
          </dependency>

          配置文件,application.yaml

          spring:
             session:
                store-type: mongodb
                timeout: 30s
                mongodb:
                   collection-name: WEB_SESSIONS


          java配置,HttpSessionConfiguration.java:

          package com.paul.testmicroservicecommon.config;

          import org.springframework.boot.autoconfigure.session.MongoSessionProperties;
          import org.springframework.boot.autoconfigure.session.SessionProperties;
          import org.springframework.boot.context.properties.EnableConfigurationProperties;
          import org.springframework.context.annotation.Bean;
          import org.springframework.session.config.ReactiveSessionRepositoryCustomizer;
          import org.springframework.session.data.mongo.ReactiveMongoSessionRepository;
          import org.springframework.session.data.mongo.config.annotation.web.reactive.EnableMongoWebSession;

          @EnableMongoWebSession
          @EnableConfigurationProperties(MongoSessionProperties.class)
          public class HttpSessionConfiguration {
              
              @Bean
              public ReactiveSessionRepositoryCustomizer<ReactiveMongoSessionRepository> customize(
                  SessionProperties sessionProperties,
                  MongoSessionProperties mongoSessionProperties
              ){
                  return c -> {
                      c.setMaxInactiveIntervalInSeconds((int)sessionProperties.getTimeout().getSeconds());
                      c.setCollectionName(mongoSessionProperties.getCollectionName());
                  };
              }

          }

          posted on 2021-12-22 09:24 paulwong 閱讀(295) 評論(0)  編輯  收藏 所屬分類: MONGODB

          主站蜘蛛池模板: 同德县| 准格尔旗| 昌黎县| 阳信县| 广西| 洮南市| 颍上县| 建水县| 曲沃县| 曲松县| 肇源县| 徐水县| 长武县| 通化市| 贺兰县| 漳州市| 郴州市| 兴义市| 新兴县| 台南县| 江城| 涡阳县| 应城市| 海安县| 旅游| 晋州市| 务川| 彭阳县| 安远县| 永泰县| 丰宁| 芦山县| 河津市| 日土县| 周宁县| 博白县| 金坛市| 简阳市| 三河市| 巴青县| 行唐县|