在验证Mongo DB时获取异常

时间:2015-07-07 05:50:28

标签: java spring mongodb sha

我在Mongo DB中插入记录时遇到异常。

org.springframework.dao.DataAccessResourceFailureException: IOException authenticating the connection; nested exception is com.mongodb.MongoException$Network: IOException authenticating the connection
    at org.springframework.data.mongodb.core.MongoExceptionTranslator.translateExceptionIfPossible(MongoExceptionTranslator.java:56)
    at org.springframework.data.mongodb.core.MongoTemplate.potentiallyConvertRuntimeException(MongoTemplate.java:1791)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:404)
    at org.springframework.data.mongodb.core.MongoTemplate.insertDBObject(MongoTemplate.java:879)
    at org.springframework.data.mongodb.core.MongoTemplate.doInsert(MongoTemplate.java:709)
    at org.springframework.data.mongodb.core.MongoTemplate.insert(MongoTemplate.java:663)
    at com.yatra.product.seo.service.mongo.MongoJob.singleInsert(MongoJob.java:76)
    at com.yatra.product.seo.service.mongo.MongoJob.myRun(MongoJob.java:96)
    at com.yatra.platform.commons.YatraRunable.run(YatraRunable.java:9)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: com.mongodb.MongoException$Network: IOException authenticating the connection
    at com.mongodb.DBPort$SaslAuthenticator.authenticate(DBPort.java:915)
    at com.mongodb.DBPort.authenticate(DBPort.java:432)
    at com.mongodb.DBPort.checkAuth(DBPort.java:443)
    at com.mongodb.DBTCPConnector.doOperation(DBTCPConnector.java:207)
    at com.mongodb.DBTCPConnector.say(DBTCPConnector.java:181)
    at com.mongodb.DBCollectionImpl.insertWithWriteProtocol(DBCollectionImpl.java:565)
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:195)
    at com.mongodb.DBCollectionImpl.insert(DBCollectionImpl.java:167)
    at com.mongodb.DBCollection.insert(DBCollection.java:93)
    at com.mongodb.DBCollection.insert(DBCollection.java:78)
    at com.mongodb.DBCollection.insert(DBCollection.java:120)
    at org.springframework.data.mongodb.core.MongoTemplate$8.doInCollection(MongoTemplate.java:884)
    at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:402)
    ... 9 more
Caused by: javax.security.sasl.SaslException: Unable to find PBKDF2WithHmacSHA1. [Caused by java.security.NoSuchAlgorithmException: PBKDF2WithHmacSHA1 SecretKeyFactory not available]
    at com.mongodb.DBPort$ScramSha1Authenticator$ScramSha1SaslClient.Hi(DBPort.java:829)
    at com.mongodb.DBPort$ScramSha1Authenticator$ScramSha1SaslClient.computeClientFinalMessage(DBPort.java:747)
    at com.mongodb.DBPort$ScramSha1Authenticator$ScramSha1SaslClient.evaluateChallenge(DBPort.java:681)
    at com.mongodb.DBPort$SaslAuthenticator.authenticate(DBPort.java:904)
    ... 21 more
Caused by: java.security.NoSuchAlgorithmException: PBKDF2WithHmacSHA1 SecretKeyFactory not available
    at javax.crypto.SecretKeyFactory.<init>(SecretKeyFactory.java:122)
    at javax.crypto.SecretKeyFactory.getInstance(SecretKeyFactory.java:160)
    at com.mongodb.DBPort$ScramSha1Authenticator$ScramSha1SaslClient.Hi(DBPort.java:826)
    ... 24 more

任何身体可以帮忙吗?

谢谢

2 个答案:

答案 0 :(得分:1)

如果你的java版本低于1.6,那么使用@Charlie的解决方案。 否则,必须缺少java.ext.dirs的系统属性 你可以像这样执行java:

  

java -Djava.ext.dirs = $ JAVA_HOME / jre / lib / ext yourApp

享受它。

答案 1 :(得分:0)

尽管我们看不到您的代码。有两种方法可以解决这个问题。

解决方法:添加以下依赖项

    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcpkix-jdk15on</artifactId>
        <version>1.52</version>
    </dependency>
    <dependency>
        <groupId>org.bouncycastle</groupId>
        <artifactId>bcprov-jdk15</artifactId>
        <version>1.46</version>
    </dependency>

在使用MongoDB进行身份验证之前添加此指令Security.addProvider(new BouncyCastleProvider())

正确的方式:

因为,PBKDF2WithHmacSHA1应该包含在您的java安装文件夹中。在我看来,我会检查我的环境变量是否正确。如果您使用Windows,我还会检查regedit HKLM/Software/JavaSoft/Java Runtime Environment/以确保路径也正确。