我一直在使用本指南通过 mongodb 设置 Spring session 数据
但是我在配置方面遇到问题。我将 Mongodb 与 Spring boot 结合使用,并尝试为 Spring boot Web 应用程序配置 session 时间和 session 名称,但它始终默认为 30 分钟,并且 mongodb 中的集合名称仍然是“sessions”
这些是我尝试过的:
将这些添加到 application.properties:
server.session.timeout=1
spring.session.mongodb.collection-name=TestSESSIONS
还有这个
server.servlet.session.timeout=60s
spring.session.mongodb.collection-name=TestSESSIONS
这些配置都不起作用
我已经看过这个URL对于 mongodb 的 spring 常见应用程序属性,但它们都没有帮助配置 mongodb 的 session 时间和集合名称。
经过几个小时的研究,似乎 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
Action:
Consider revisiting the entries above or defining a bean of type 'org.springframework.data.mongodb.core.MongoOperations' in your configuration.
这是来自上面链接的 spring.io 指南'mongoSessionConverter'
的@bean
这是java文件MongoHttpSessionConfiguration从 spring 开始,这是 spring 的自动配置;我尝试扩展 "MongoHttpSessionConfiguration"
并重写我自己的 setter 方法。例如 sessionTime 的 "setMaxInactiveIntervalInSeconds"
和
"setCollectionName"
为 mongododb 数据库集合名称。
但我遇到了这个错误:
Description:
The bean 'mongoSessionRepository', defined in class path resource [com/khatpass/app/config/SessionListenerConfig.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/session/data/mongo/config/annotation/web/http/MongoHttpSessionConfiguration.class] and overriding is disabled.
我一直在尝试使用 Mongodb 配置 Spring Boot session 。 session 始终默认为 30 分钟,并且集合名称在 mongodb 集合中始终为“sessions”。不确定如何更改 serverSelectionTimeout='30000 ms'
和 mongodb 集合名称“sessions”我不知道该怎么办,需要帮助。
2019-02-24 13:39:54.501 INFO 36113 --- [ main] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=MULTIPLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
最佳答案
经过大量研究,最后,通过查看源代码,我找到了解决方案:
@EnableMongoHttpSession(maxInactiveIntervalInSeconds = 24 * 60 * 60)
public class SessionConfiguration {}
为了覆盖默认的集合名称,还有另一个注释属性collectionName
。
这适用于 Spring Boot 2.1.1
关于java - 无法配置 Java Spring Boot 数据 session Mongodb,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54854942/