title: How to fix Identifier doesn't match expected value error when connection to mongodb with kerberos?
body: <p>I'm trying to connect to mongodb server with kerberos using keytab and principal with a java application on a linux server.</p>
  <p>I doubled checked that the keytab and principal are the right ones.</p>
  <p>Succeeded in connecting manually from the server (not from the java code).</p>
  <p>However, I keep getting this error which won't let me touch the database:</p>
  <pre><code>org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoConnection': Invocation of init method failed; nested exception is com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=GSSAPI, userName='X', source='$external', password=&lt;hidden&gt;, mechanismProperties=&lt;hidden&gt;}
  
          at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:160)
  
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization(AbstractAutowireCapableBeanFactory.java:416)
  
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1788)
  
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:595)
  
          at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:517)
  
          at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:323)
  
          at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
  
          at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:321)
  
          at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)
  
          at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:882)
  
          at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:878)
  
          at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
  
          at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)
  
          at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)
  
          at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397)
  
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)
  
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1226)
  
          at org.springframework.boot.SpringApplication.run(SpringApplication.java:1215)
  
          at com.Application.main(Application.java:40)
  
         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.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48)
  
          at org.springframework.boot.loader.Launcher.launch(Launcher.java:87)
  
          at org.springframework.boot.loader.Launcher.launch(Launcher.java:51)
  
          at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:52)
  
  Caused by: com.mongodb.MongoSecurityException: Exception authenticating MongoCredential{mechanism=GSSAPI, userName=’X', source='$external', password=&lt;hidden&gt;, mechanismProperties=&lt;hidden&gt;}
  
          at com.mongodb.internal.connection.SaslAuthenticator.wrapException(SaslAuthenticator.java:173)
  
          at com.mongodb.internal.connection.SaslAuthenticator.access$300(SaslAuthenticator.java:40)
  
          at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:70)
  
          at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:47)
  
          at com.mongodb.internal.connection.SaslAuthenticator.doAsSubject(SaslAuthenticator.java:179)
  
          at com.mongodb.internal.connection.SaslAuthenticator.authenticate(SaslAuthenticator.java:47)
  
          at com.mongodb.internal.connection.InternalStreamConnectionInitializer.authenticateAll(InternalStreamConnectionInitializer.java:156)
  
          at com.mongodb.internal.connection.InternalStreamConnectionInitializer.initialize(InternalStreamConnectionInitializer.java:63)
  
          at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:129)
  
          at com.mongodb.internal.connection.UsageTrackingInternalConnection.open(UsageTrackingInternalConnection.java:50)
  
          at com.mongodb.internal.connection.DefaultConnectionPool$PooledConnection.open(DefaultConnectionPool.java:398)
  
          at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:115)
  
          at com.mongodb.internal.connection.DefaultConnectionPool.get(DefaultConnectionPool.java:101)
  
          at com.mongodb.internal.connection.DefaultServer.getConnection(DefaultServer.java:92)
  
          at com.mongodb.binding.ClusterBinding$ClusterBindingConnectionSource.getConnection(ClusterBinding.java:126)
  
          at com.mongodb.client.internal.ClientSessionBinding$SessionBindingConnectionSource.getConnection(ClientSessionBinding.java:136)
  
          at com.mongodb.operation.OperationHelper.withConnectionSource(OperationHelper.java:528)
  
          at com.mongodb.operation.OperationHelper.withConnection(OperationHelper.java:492)
  
          at com.mongodb.operation.CreateCollectionOperation.execute(CreateCollectionOperation.java:381)
  
          at com.mongodb.operation.CreateCollectionOperation.execute(CreateCollectionOperation.java:57)
  
          at com.mongodb.client.internal.MongoClientDelegate$DelegateOperationExecutor.execute(MongoClientDelegate.java:206)
  
          at com.mongodb.client.internal.MongoDatabaseImpl.executeCreateCollection(MongoDatabaseImpl.java:304)
  
          at com.mongodb.client.internal.MongoDatabaseImpl.createCollection(MongoDatabaseImpl.java:261)
  
          at com.mongodb.client.internal.MongoDatabaseImpl.createCollection(MongoDatabaseImpl.java:256)
  
          at com.connections.MongoConnection.init(MongoConnection.java:52)
  
          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.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:389)
  
          at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:333)
  
          at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:157)
  
          ... 26 common frames omitted
  
  Caused by: javax.security.sasl.SaslException: GSS initiate failed
  
          at jdk.security.jgss/com.sun.security.sasl.gsskerb.GssKrb5Client.evaluateChallenge(GssKrb5Client.java:211)
  
          at com.mongodb.internal.connection.SaslAuthenticator$1.run(SaslAuthenticator.java:53)
  
          ... 55 common frames omitted
  
  Caused by: org.ietf.jgss.GSSException: No valid credentials provided (Mechanism level: Server not found in Kerberos database (7))
  
          at java.security.jgss/sun.security.jgss.krb5.Krb5Context.initSecContext(Krb5Context.java:773)
  
          at java.security.jgss/sun.security.jgss.GSSContextImpl.initSecContext(GSSContextImpl.java:266)
  
          at java.security.jgss/sun.security.jgss.GSSContextImpl.initSecContext(GSSContextImpl.java:196)
  
          at jdk.security.jgss/com.sun.security.sasl.gsskerb.GssKrb5Client.evaluateChallenge(GssKrb5Client.java:192)
  
          ... 56 common frames omitted
  
  Caused by: sun.security.krb5.KrbException: Server not found in Kerberos database (7)
  
          at java.security.jgss/sun.security.krb5.KrbTgsRep.&lt;init&gt;(KrbTgsRep.java:70)
  
          at java.security.jgss/sun.security.krb5.KrbTgsReq.getReply(KrbTgsReq.java:226)
  
          at java.security.jgss/sun.security.krb5.KrbTgsReq.sendAndGetCreds(KrbTgsReq.java:237)
  
          at java.security.jgss/sun.security.krb5.internal.CredentialsUtil.serviceCredsSingle(CredentialsUtil.java:482)
  
          at java.security.jgss/sun.security.krb5.internal.CredentialsUtil.serviceCreds(CredentialsUtil.java:340)
  
          at java.security.jgss/sun.security.krb5.internal.CredentialsUtil.serviceCreds(CredentialsUtil.java:314)
  
          at java.security.jgss/sun.security.krb5.internal.CredentialsUtil.acquireServiceCreds(CredentialsUtil.java:169)
  
          at java.security.jgss/sun.security.krb5.Credentials.acquireServiceCreds(Credentials.java:490)
  
          at java.security.jgss/sun.security.jgss.krb5.Krb5Context.initSecContext(Krb5Context.java:697)
  
          ... 59 common frames omitted
  
  Caused by: sun.security.krb5.Asn1Exception: Identifier doesn't match expected value (906)
  
          at java.security.jgss/sun.security.krb5.internal.KDCRep.init(KDCRep.java:140)
  
          at java.security.jgss/sun.security.krb5.internal.TGSRep.init(TGSRep.java:65)
  
          at java.security.jgss/sun.security.krb5.internal.TGSRep.&lt;init&gt;(TGSRep.java:60)
  
          at java.security.jgss/sun.security.krb5.KrbTgsRep.&lt;init&gt;(KrbTgsRep.java:55)
  
          ... 67 common frames omitted
  </code></pre>