我一直在使用本指南通过mongodb设置spring会话数据
https://docs.spring.io/spring-session-data-mongodb/docs/2.1.1.RELEASE/reference/htmlsingle/#introduction
但是我在配置方面遇到问题。我正在将Mongodb与Spring Boot结合使用,并且尝试为Spring Boot Web应用程序配置会话时间和会话名称,但它始终默认为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的春季通用应用程序属性,但是这些都没有帮助配置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
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这是来自spring的java文件MongoHttpSessionConfiguration,由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会话。会话始终默认为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