2

Environment

spring-boot 1.3.2

spring-mongo 0.8.0

build my spring-boot microservice with gradle.

Problem

one of mongo database enable the auth,so i configure these arguments in application.yml

spring: data: mongo: username:xxx password:xxx authentication-database:xxx

it works well for mongodb which has set the auth.

but I got error with mongdb which has not set the auth even I set the username="",password="".it fail with auth.

Caused by: com.mongodb.CommandFailureException: { "serverUsed" : "localhost:27017" , "ok" : 0.0 , "code" : 18 , "errmsg" : "Authentication failed."} at com.mongodb.CommandResult.getException(CommandResult.java:76) ~[mongo-java-driver-2.13.3.jar:na] at com.mongodb.CommandResult.throwOnError(CommandResult.java:140) ~[mongo-java-driver-2.13.3.jar:na] at com.mongodb.DBPort$SaslAuthenticator.authenticate(DBPort.java:903) ~[mongo-java-driver-2.13.3.jar:na] at com.mongodb.DBPort.authenticate(DBPort.java:436) ~[mongo-java-driver-2.13.3.jar:na] at com.mongodb.DBPort.checkAuth(DBPort.java:447) ~[mongo-java-driver-2.13.3.jar:na] at com.mongodb.DBTCPConnector.doOperation(DBTCPConnector.java:207) ~[mongo-java-driver-2.13.3.jar:na] at com.mongodb.DBCollectionImpl.createIndex(DBCollectionImpl.java:392) ~[mongo-java-driver-2.13.3.jar:na] at com.mongodb.DBCollection.createIndex(DBCollection.java:597) ~[mongo-java-driver-2.13.3.jar:na]

My question is how to remove the mongo.username,password key dynamic for these mongo db which has not set auth?

also I am curious about why cannot remove the key in application.yml file.while have seen that we can automatic property expansion using Maven or gradle in this document:property expansion

Update if just leave these key empty,still get error

The full response is { "ok" : 0.0, "errmsg" : "Invalid database name: ''", "code" : 73 }}}]

Code Stone
  • 129
  • 3
  • 12
  • I think this is common question in spring-boot configuration.not just mongo – Code Stone Oct 11 '16 at 11:27
  • look closer at createMongoClient method in org.springframework.boot.autoconfigure.mongo.MongoProperties class. It seems that when username and password are null (empty string is not a null), then auth credentials are omitted. Try leaving username and password properties clearly empty. – bart.s Oct 11 '16 at 13:12
  • once configing the key of username,password in application.yml.even leave the value empty,it also get the auth error – Code Stone Oct 11 '16 at 13:45
  • @bart.s so how to pass a null value to argument in yml file? – Code Stone Oct 11 '16 at 13:53
  • if leaving empty values does not work then it setting null won't be as simple as I thought. Do you have any problems connecting to such "non-secured" mongodb from shell? – bart.s Oct 11 '16 at 14:00
  • @bart.s no problems from shell.and once removing these key defined in yml file.it works well – Code Stone Oct 11 '16 at 14:02
  • is authentication-database also empty? – bart.s Oct 11 '16 at 14:07
  • whether the value of authentication-database is empty or not empty,it got auth error. – Code Stone Oct 11 '16 at 14:13
  • 1
    Are those "auth"/"no-auth" databases separated via spring profiles? – bart.s Oct 11 '16 at 14:16
  • have took this into consideration.now there are two ways to solve the problem.one way is creating another yml file.another way is spring profiles. – Code Stone Oct 11 '16 at 14:24
  • yes, that's what I was thinking about, too. Remove mongo config from application.yml and create profile-dependent configuration in proper .yml files. – bart.s Oct 11 '16 at 14:34
  • not familar with spring profiles.will spend time on this.thanks for your all suggestions – Code Stone Oct 11 '16 at 14:39

1 Answers1

2

After talking with @bart.s.Resolving this issue with spring profiles. when connect to mongo db which enable auth,need to active profiles.config profile key in application.yml like this

--- 
spring:
    profiles: prod
    data:
         mongodb:
           username: ${MONGO_USERNAME:mongo}
           password: ${MONGO_PASSWORD:mongo}
           authentication-database: ${MONGO_AUTH:admin}      

when connect to mongo with auth.add "--spring.profiles.active=prod" to active the mongodb username and password connection info.

Code Stone
  • 129
  • 3
  • 12