title: How to use Spring to connect to MongoDB which requires authentication
body: <p>I am using the below Spring configuration in order to connect to mongoDB </p>
  
  <pre><code>&lt;bean id="mongoTemplate" class="org.springframework.data.document.mongodb.MongoTemplate"&gt;
          &lt;constructor-arg name="mongo" ref="mongo"/&gt;
          &lt;constructor-arg name="databaseName" value="${mongodb.dbname}"/&gt;
      &lt;/bean&gt;
  
  
      &lt;bean class="com.mongodb.MongoURI" id="mongoUri"&gt;
          &lt;constructor-arg value="${mongodb.url}"  /&gt;
      &lt;/bean&gt;
  
      &lt;bean class="com.mongodb.Mongo" id="mongo"&gt;
          &lt;constructor-arg ref="mongoUri" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>where <code>mongo.url=mongodb://&lt;user&gt;:&lt;password&gt;@&lt;host&gt;:27017</code></p>
  
  <p>However I'm getting an authetication error.
  My understanding was that MongoUI can take a URL in the above format.</p>
  
  <p>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.</p>
  
  <p>Any idea how can I change my config above to suppot this assuming mongo.url format cannot be changed?</p>