How to use Spring to connect to MongoDB which requires authentication

匿名 (未验证) 提交于 2019-12-03 10:03:01

问题:

I am using the below Spring configuration in order to connect to mongoDB

where mongo.url=mongodb://:@:27017

However I'm getting an authetication error. My understanding was that MongoUI can take a URL in the above format.

I know that mongoTemplate can accept userCredentials object however I would need to extract them from the URL first and i'm not sure how to do that in the configuration.

Any idea how can I change my config above to suppot this assuming mongo.url format cannot be changed?

回答1:

found the solution using Spring Expression Language



回答2:

If you want to add authntication using java config

@Configuration @EnableMongoRepositories("path.to.your.repository") public class MongoConfig extends AbstractMongoConfiguration { @Value("${mongodb.name}") private String  dbName;  @Value("${mongodb.host}") private String  host;  @Value("${mongodb.port}") private Integer port;  @Value("${mongodb.username}") private String  userName;  @Value("${mongodb.password}") private String  password;   @Override protected String getDatabaseName() {     return this.dbName; }  @Override public Mongo mongo() throws Exception {     return new MongoClient(this.host, this.port); }  @Override @Bean public SimpleMongoDbFactory mongoDbFactory() throws Exception {     return new SimpleMongoDbFactory(mongo(), getDatabaseName()); }  @Override @Bean public MongoTemplate mongoTemplate() throws Exception {     final UserCredentials userCredentials = new UserCredentials(this.userName, this.password);      final MongoTemplate mongoTemplate = new MongoTemplate(mongo(), getDatabaseName(), userCredentials);     mongoTemplate.setWriteConcern(WriteConcern.SAFE);      return mongoTemplate; }  }


回答3:

To update @Lealem Admassu's answer for java config, in Mongo 3 they changed the API, and now it is recommended to use mongo's MongoCredentials instead of UserCredentials.

Here there is a simple example of how to get a MongoClient: http://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.mongo-3.authentication

The next code can be done modular, but more or less this works for me (I needed a MongoTemplate):

public MongoTemplate getMongoTemplate(String host, int port,                                       String authenticationDB,                                       String database,                                       String user, char[] password)         throws UnknownHostException {     return new MongoTemplate(             new SimpleMongoDbFactory(                     new MongoClient(                             new ServerAddress(host, port),                             Collections.singletonList(                                     MongoCredential.createCredential(                                             user,                                             authenticationDB,                                             password                                     )                             )                     ),                     database             )     ); }


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!