本文介紹了如何將Java客戶端連接到Windows 10上的Azure Cosmos數據庫模擬器,找不到證書的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
問題:我的演示代碼客戶端無法連接到Windows 10上的Azure Cosmos模擬器。
步驟:
我在Windows上安裝了Cosmosdb模擬器-看起來不錯
根據文檔,我啟動了Windows cert manage.msc。我選擇友好名稱為";DocumentDBEmulatorCertificate";的私有證書作為本地磁盤的base64編碼的x.509.cer文件
我以管理員身份啟動了Windows中的cmd控制臺,并將cmd控制臺cd轉到本地JAVA_HOME/lib/security目錄(我使用的是Java 8.0.131)
我使用此命令運行了keytool
keytool-import-trustcacerts-keystore cacerts-store pass changeit-nopt-alias azureCosmossDBEmulator-FILE";D:導出的certificatescosmossDB-emulator-cert.cer";
我將修改后的密鑰庫列出為dump.txt
文件。我可以在轉儲中看到我的條目
azurecosmossdbmulator,2017年8月30日,trustedCertEntry,
證書指紋(Sha1):5B:F4:14:BE:9F:2B:7F:6A:2B:C0:87:A4:3E:4D:9A:52:45:FA:2F:EA
并且這與X.509證書中的指紋值匹配。
我在生成時重新啟動了IntelliJ,并檢查Java 8.0.1.3.1是項目中唯一的JDK。
我在調試中啟動了Groovy測試腳本,并逐步執行代碼。我可以創建DocumentClient OK。
這只是測試連接代碼的粗略腳本,如下所示
final String key = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw=="
DocumentClient client = new DocumentClient("https://localhost:8081", key
, new ConnectionPolicy(), ConsistencyLevel.Session)
String dbname = "familyDB"
String dblink = "/dbs/$dbname"
//create db if not exists
try {
client.readDatabase(dblink,null)
println "found db $dbname"
} catch (DocumentClientException de) {
if (de.getStatusCode() == 404) {
Database db = new Database()
db.id = dbname
client.createDatabase(db, null)
println "created new DB $dbname"
} else {
throw de
}
}
當我到達client.readDatabase行時,我收到如下異常:
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Caught: java.lang.IllegalStateException: Http client execution failed.
java.lang.IllegalStateException: Http client execution failed.
at com.microsoft.azure.documentdb.internal.GatewayProxy.performGetRequest(GatewayProxy.java:234)
at com.microsoft.azure.documentdb.internal.GatewayProxy.doRead(GatewayProxy.java:89)
at com.microsoft.azure.documentdb.internal.GatewayProxy.processMessage(GatewayProxy.java:336)
at com.microsoft.azure.documentdb.DocumentClient$8.apply(DocumentClient.java:2985)
at com.microsoft.azure.documentdb.internal.RetryUtility.executeDocumentClientRequest(RetryUtility.java:58)
at com.microsoft.azure.documentdb.DocumentClient.doRead(DocumentClient.java:2991)
at com.microsoft.azure.documentdb.DocumentClient.readDatabase(DocumentClient.java:491)
at com.microsoft.azure.documentdb.DocumentClient$readDatabase.call(Unknown Source)
at com.softwood.azure.client.cosmossDBClientScript.run(cosmossDBClientScript.groovy:29)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.createLayeredSocket(SSLConnectionSocketFactory.java:394)
at org.apache.http.conn.ssl.SSLConnectionSocketFactory.connectSocket(SSLConnectionSocketFactory.java:353)
at org.apache.http.impl.conn.DefaultHttpClientConnectionOperator.connect(DefaultHttpClientConnectionOperator.java:141)
at org.apache.http.impl.conn.PoolingHttpClientConnectionManager.connect(PoolingHttpClientConnectionManager.java:353)
at org.apache.http.impl.execchain.MainClientExec.establishRoute(MainClientExec.java:380)
at org.apache.http.impl.execchain.MainClientExec.execute(MainClientExec.java:236)
at org.apache.http.impl.execchain.ProtocolExec.execute(ProtocolExec.java:184)
at org.apache.http.impl.client.InternalHttpClient.doExecute(InternalHttpClient.java:184)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:107)
at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:55)
at com.microsoft.azure.documentdb.internal.GatewayProxy.performGetRequest(GatewayProxy.java:231)
... 8 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
... 20 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
... 20 more
這基本上說明它找不到我的cacert條目并拒絕連接。
出了什么問題?(我還沒有重新啟動Windows)。證書看起來沒問題,似乎可以通過keytool導入cacerts,我使用的是單個JDK引用,但代碼無法連接。
我如何才能解開我沒有正確完成的、現在必須執行的操作才能使代碼從Java連接到在Windows 10計算機上本地運行的Azure DB模擬器?
推薦答案
根據您的描述,我還使用Document DB JavaSDK編寫了一段代碼連接到Cosmos DB仿真器,結果遇到了與您相同的問題。
package emulator;
import com.microsoft.azure.documentdb.ConnectionPolicy;
import com.microsoft.azure.documentdb.ConsistencyLevel;
import com.microsoft.azure.documentdb.Database;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentClientException;
public class TestEmlulator {
// Replace with your DocumentDB end point and master key.
private static final String END_POINT = "https://localhost:8081/";
private static final String MASTER_KEY = "C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
public static void main(String[] args) throws DocumentClientException {
// Connect to the Azure Cosmos DB Emulator running locally
DocumentClient client = new DocumentClient(END_POINT, MASTER_KEY, ConnectionPolicy.GetDefault(),
ConsistencyLevel.Session);
Database database = new Database();
database.setId("testEmulator");
database = client.createDatabase(database, null).getResource();
System.out.println(database.toJson());
}
}
所以我導出cosmosDB的證書,將其命名為documentdbemulatorcert.cer
,并按照official tutorial提供的方式存儲在我的D盤中。您似乎也做了同樣的事情。
然后,我嘗試將cosmosdb的證書導入到JVM信任證書列表中。您也可以參考下面的步驟:
步驟1:打開CMD命令窗口,進入%JAVA_HOME%/jre/lib/security
目錄。請注意,必須使用administrator privileges
打開命令窗口
對我來說,目錄看起來像C:Program FilesJavajdk1.8.0_131jrelibsecurity
步驟2:在打開的命令窗口中,輸入以下命令:
keytool -import -alias cacerts -keystore cacerts -file d:documentdbemulatorcert.cer
輸入默認密碼:changeit
,然后在Trust this certificate? [no]:
旁邊輸入Y或y
第3步:您可以看到Certificate was added to keystore
證書是否已成功導入。
您可以參考下面的截圖了解整個過程:
最后,我的代碼可以工作了!
您也可以參考下面的SO線程:
1.Unable to find valid certification path to requested target – error even after cert imported
2."PKIX path building failed" and "unable to find valid certification path to requested target"
希望它能幫助您。
這篇關于如何將Java客戶端連接到Windows 10上的Azure Cosmos數據庫模擬器,找不到證書的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,