Spring Data mongodb:添加用于MongoDb访问的凭据 [英] Spring Data mongodb: adding credentials for MongoDb access

查看:203
本文介绍了Spring Data mongodb:添加用于MongoDb访问的凭据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Spring应用程序中具有以下工作配置:

I have the following working configurations in my Spring application:

<mongo:mongo id="myRs" replica-set="myhost:27017">

  <mongo:options max-wait-time="1500"
                   connect-timeout="30000" />
</mongo:mongo>

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <property name="writeResultChecking" value="EXCEPTION"/>
    <property name="writeConcern" value="FSYNC_SAFE"/>
    <constructor-arg ref="myRs"/>
    <constructor-arg name="databaseName" value="mydb"/>
</bean>

现在我要做的就是设置用户名/密码来访问mongo数据库,而无需更改我的数据库代码(即仅通过更新Spring应用程序上下文xml文件)。那可能吗?如果是这样,怎么办?

Now all I want to do is to set up username/password for accessing the mongo database without changing my code (i.e. only by updating the Spring app context xml file). Is that possible? If so, how?

谢谢。

推荐答案

您可以将用户名密码传递给MongoTemplate。使用 PropertyPlaceholderConfigurer ,您甚至可以从属性文件中读取用户名和密码。

You can pass username password like this to MongoTemplate. Using PropertyPlaceholderConfigurer you can even read the username and password from a property file.

<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
    <property name="writeResultChecking" value="EXCEPTION"/>
    <property name="writeConcern" value="FSYNC_SAFE"/>
    <constructor-arg ref="myRs"/>
    <constructor-arg name="databaseName" value="mydb"/>
    <constructor-arg name="userCredentials" ref="userCredentials"/>
</bean>

<bean id="userCredentials" class="org.springframework.data.authentication.UserCredentials">
    <constructor-arg name="username" value="username" />
    <constructor-arg name="password" value="password" />
</bean>

这篇关于Spring Data mongodb:添加用于MongoDb访问的凭据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆