title: How to correctly authenticate with MongoDB when using Repositories in JAVA Spring Framework
body: <p>I am trying to get Data from a mongoDB with a repository. But when I call the "find" methods, java throws a MongoQueryException and tells me, that find requires authentication. I want to configure my application over a XML file and application.yml</p>
  
  <p>My Product Class</p>
  
  <pre><code>@Document
  public class Product{
  
      @Id
      @Getter @Setter
      private String _id;
      @Getter @Setter
      private String name;
      @Getter @Setter
      private String brand;
      @Getter @Setter
      private String line;
      @Getter @Setter
      private String classification;
  }
  </code></pre>
  
  <p>My Repository</p>
  
  <pre><code>@Repository
  public interface ProductRepository extends MongoRepository&lt;Product, String&gt; {
  
      public List&lt;Product&gt; findByBrand( String brand );
      public List&lt;Product&gt; findByBrandLike( String brand );    
      public List&lt;Product&gt; findByName( String name );
      public List&lt;Product&gt; findByNameLike( String name )  
      public List&lt;Product&gt; findByLine( String line );    
      public List&lt;Product&gt; findByLineLike( String line );    
      public List&lt;Product&gt; findByClassification( String classification );    
      public List&lt;Product&gt; findByClassificationLike( String classification );
  }
  </code></pre>
  
  <p>My Configuration</p>
  
  <pre><code>&lt;beans xmlns = "http://www.springframework.org/schema/beans"
     xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
     xmlns:context = "http://www.springframework.org/schema/context"
     xmlns:mongo="http://www.springframework.org/schema/data/mongo"
     xsi:schemaLocation = "http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/data/mongo https://www.springframework.org/schema/data/mongo/spring-mongo.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd"&gt;
  
      &lt;mongo:repositories base-package="de.myproject.mongo.repositories"/&gt;
      &lt;mongo:mongo-client id="mongoClient" host="${my.mongodb.host}" port="${my.mongodb.port}"/&gt;
      &lt;mongo:db-factory id="mongoDbFactory" mongo-ref="mongoClient"/&gt;
      &lt;context:annotation-config/&gt;
  
      &lt;bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate"&gt;
          &lt;constructor-arg ref="mongoDbFactory"/&gt;
      &lt;/bean&gt;
  
      &lt;bean class="org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor"/&gt;
  
      ...
  &lt;/beans&gt;
  </code></pre>
  
  <p>My application.yml</p>
  
  <pre><code>my:
    mongodb:
      host: mongodb
      password: myPassword
      port: 27017
      username: myUser
  
      productdb: myDB # where to save product data
      product_col: product # where to put product content
      err_log: log # where  to put url's that did not work
  </code></pre>
  
  <p>This is the errorstack I get:</p>
  
  <pre><code>org.springframework.data.mongodb.UncategorizedMongoDbException: Query failed with error code 13 and error message 'command find requires authentication' on server mongodb:27017; nested exception is com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'command find requires authentication' on server mongodb:27017
  at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:138)
  at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:2783)
  at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2688)
  at org.springframework.data.mongodb.core.MongoTemplate.doFind(MongoTemplate.java:2431)
  at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.doFind(ExecutableFindOperationSupport.java:214)
  at org.springframework.data.mongodb.core.ExecutableFindOperationSupport$ExecutableFindSupport.all(ExecutableFindOperationSupport.java:157)
  at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.lambda$getExecution$1(AbstractMongoQuery.java:115)
  at org.springframework.data.mongodb.repository.query.AbstractMongoQuery.execute(AbstractMongoQuery.java:99)
  at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:605)
  at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.lambda$invoke$3(RepositoryFactorySupport.java:595)
  at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:595)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:59)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.data.repository.core.support.SurroundingTransactionDetectorMethodInterceptor.invoke(SurroundingTransactionDetectorMethodInterceptor.java:61)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
  at com.sun.proxy.$Proxy91.findByBrandLike(Unknown Source)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.base/java.lang.reflect.Method.invoke(Method.java:566)
  at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139)
  at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
  at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212)
  at com.sun.proxy.$Proxy92.findByBrandLike(Unknown Source)
  at de.myproject.spiders.ContentSpider$2.process(ContentSpider.java:179)
  at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.MulticastProcessor.doProcessSequential(MulticastProcessor.java:715)
  at org.apache.camel.processor.MulticastProcessor.doProcessSequential(MulticastProcessor.java:638)
  at org.apache.camel.processor.MulticastProcessor.process(MulticastProcessor.java:248)
  at org.apache.camel.processor.Splitter.process(Splitter.java:129)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:76)
  at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:117)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:76)
  at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
  at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:117)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:76)
  at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
  at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:117)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
  at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:76)
  at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
  at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
  at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97)
  at org.apache.camel.http.common.CamelServlet.doService(CamelServlet.java:213)
  at org.apache.camel.http.common.CamelServlet.service(CamelServlet.java:79)
  at javax.servlet.http.HttpServlet.service(HttpServlet.java:750)
  at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:74)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
  at org.springframework.boot.actuate.web.trace.servlet.HttpTraceFilter.doFilterInternal(HttpTraceFilter.java:88)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at org.springframework.web.filter.FormContentFilter.doFilterInternal(FormContentFilter.java:92)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:93)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.filterAndRecordMetrics(WebMvcMetricsFilter.java:114)
  at org.springframework.boot.actuate.metrics.web.servlet.WebMvcMetricsFilter.doFilterInternal(WebMvcMetricsFilter.java:104)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:200)
  at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:118)
  at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
  at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
  at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
  at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
  at io.undertow.servlet.handlers.ServletChain$1.handleRequest(ServletChain.java:68)
  at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
  at io.undertow.servlet.handlers.RedirectDirHandler.handleRequest(RedirectDirHandler.java:68)
  at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:132)
  at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
  at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
  at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
  at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
  at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
  at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:269)
  at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:78)
  at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:133)
  at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:130)
  at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
  at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
  at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:249)
  at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:78)
  at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:99)
  at io.undertow.server.Connectors.executeRootHandler(Connectors.java:376)
  at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:830)
  at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
  at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
  at java.base/java.lang.Thread.run(Thread.java:834)
  Caused by: com.mongodb.MongoQueryException: Query failed with error code 13 and error message 'command find requires authentication' on server mongodb:27017
      at com.mongodb.operation.FindOperation$1.call(FindOperation.java:706)
      at com.mongodb.operation.FindOperation$1.call(FindOperation.java:695)
      at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:462)
      at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:406)
      at com.mongodb.operation.FindOperation.execute(FindOperation.java:695)
      at com.mongodb.operation.FindOperation.execute(FindOperation.java:83)
      at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:179)
      at com.mongodb.client.internal.MongoIterableImpl.execute(MongoIterableImpl.java:132)
      at com.mongodb.client.internal.MongoIterableImpl.iterator(MongoIterableImpl.java:86)
      at org.springframework.data.mongodb.core.MongoTemplate.executeFindMultiInternal(MongoTemplate.java:2671)
      ... 150 more
  </code></pre>
  
  <p>I tried adding the authentication and database settings to the application.yml, but that did not work for me. If you need any more information, just let me know.</p>