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 閱讀(292) 評論(0)  編輯  收藏 所屬分類: MONGODB

          主站蜘蛛池模板: 福鼎市| 莱阳市| 浦县| 泽州县| 金湖县| 崇信县| 怀宁县| 汉源县| 方山县| 怀集县| 九寨沟县| 嘉荫县| 浙江省| 通榆县| 松江区| 望奎县| 南京市| 白水县| 长宁县| 鹤山市| 郎溪县| 堆龙德庆县| 北安市| 荔波县| 河池市| 兴城市| 叙永县| 云和县| 昭通市| 荃湾区| 南城县| 册亨县| 南和县| 全南县| 纳雍县| 叶城县| 大石桥市| 乡宁县| 诸暨市| 蚌埠市| 突泉县|