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

          主站蜘蛛池模板: 天全县| 闸北区| 横山县| 邢台县| 防城港市| 开封市| 新干县| 眉山市| 西乌珠穆沁旗| 乌拉特中旗| 饶平县| 富平县| 云安县| 贞丰县| 连州市| 柳林县| 遵化市| 丰原市| 凯里市| 福州市| 图们市| 大兴区| 麟游县| 平顶山市| 太仆寺旗| 永新县| 财经| 莒南县| 黄大仙区| 屯昌县| 英吉沙县| 五莲县| 塘沽区| 嘉祥县| 宜兰市| 中阳县| 土默特右旗| 荥阳市| 遂平县| 定远县| 桦川县|