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

          主站蜘蛛池模板: 大悟县| 乐至县| 长寿区| 枣强县| 贵州省| 仁怀市| 景洪市| 犍为县| 望江县| 恩平市| 莲花县| 噶尔县| 新泰市| 德保县| 白水县| 成安县| 曲周县| 宁化县| 巨野县| 济源市| 宁德市| 忻州市| 南投市| 东乡| 青田县| 济阳县| 云林县| 曲松县| 古蔺县| 安溪县| 内丘县| 五华县| 金平| 呼和浩特市| 安陆市| 连山| 盐城市| 恭城| 杭锦旗| 阿拉善左旗| 宁远县|