为了账号安全,请及时绑定邮箱和手机立即绑定

无法配置 Java Spring Boot 数据会话 Mongodb

无法配置 Java Spring Boot 数据会话 Mongodb

慕桂英3389331 2022-07-06 09:42:48
我一直在使用本指南使用 mongodb 设置春季会话数据https://docs.spring.io/spring-session-data-mongodb/docs/2.1.1.RELEASE/reference/htmlsingle/#introduction但是我在配置方面遇到问题。我在 Spring Boot 中使用 Mongodb,我正在尝试为 Spring Boot Web 应用程序配置我的会话时间和会话名称,但它一直默认为 30 分钟,并且 mongodb 中的集合名称仍然是“会话”这些是我尝试过的:将这些添加到 application.properties:server.session.timeout=1spring.session.mongodb.collection-name=TestSESSIONS和这个server.servlet.session.timeout=60sspring.session.mongodb.collection-name=TestSESSIONS这些配置都不起作用我已经查看了这个URL以获取 mongodb 的 spring 通用应用程序属性,但它都没有帮助配置 mongodb 的会话时间和集合名称。经过数小时的研究,似乎 Spring Boot 使用了某种自动配置"org.springframework.boot.autoconfigure"所以我在我的application.properties中添加了这个spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration禁用自动配置。但现在它只是给了我这个错误:***************************APPLICATION FAILED TO START***************************Description:Parameter 0 of method mongoSessionRepository in org.springframework.session.data.mongo.config.annotation.web.http.MongoHttpSessionConfiguration required a bean of type 'org.springframework.data.mongodb.core.MongoOperations' that could not be found.The following candidates were found but could not be injected:    - Bean method 'mongoTemplate' in 'MongoDataAutoConfiguration' not loaded because AnyNestedCondition 0 matched 2 did not; NestedCondition on MongoDataAutoConfiguration.AnyMongoClientAvailable.FallbackClientAvailable @ConditionalOnBean (types: com.mongodb.client.MongoClient; SearchStrategy: all) did not find any beans of type com.mongodb.client.MongoClient; NestedCondition on MongoDataAutoConfiguration.AnyMongoClientAvailable.PreferredClientAvailable @ConditionalOnBean (types: com.mongodb.MongoClient; SearchStrategy: all) did not find any beans of type com.mongodb.MongoClient
查看完整描述

2 回答

?
墨色风雨

TA贡献1853条经验 获得超6个赞

经过这么多研究,最后,通过源代码,我找到了解决方案:

@EnableMongoHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60)
public class SessionConfiguration {}

要覆盖默认集合名称,还有另一个注解属性collectionName

这适用于 Spring Boot 2.1.1


查看完整回答
反对 回复 2022-07-06
?
茅侃侃

TA贡献1842条经验 获得超21个赞

在查看了MongoOperationsSessionRepository类之后,org.springframework.session.data.mongo它似乎无法配置,application.properties因为该类使用静态最终值

public static final int DEFAULT_INACTIVE_INTERVAL = 1800;

public static final String DEFAULT_COLLECTION_NAME = "sessions";

更改值的唯一方法是在对象保存之前拦截它。这些字段没有 getter 或 setter,不能轻易改变,真是个笑话!


查看完整回答
反对 回复 2022-07-06
  • 2 回答
  • 0 关注
  • 351 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信