WIL*_*MAN 1 java client keystore azure
问题:我的演示代码客户端无法连接到Windows 10上的Azure Cosmos模拟器。不确定为什么不这样做。
步骤:
1.在Windows上安装cosmossdb仿真器-看起来不错
2.按照文档,启动Windows cert manage.msc。选择了具有友好名称“ DocumentDBEmulatorCertificate”的私有证书作为base64编码的x.509.cer文件到本地磁盘
3。以管理员身份在Windows中启动了cmd控制台,并以cd到本地JAVA_HOME / lib / security目录(我正在使用Java 8.0.131 )
4.与此一起运行keytool
keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -alias azureCosmossDBEmulator -file“ D:\ exported certificate \ cosmossDB-emulator-cert.cer”
azurecosmossdbemulator,2017年8月30日,trustCertEntry,证书指纹(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。
在调试中启动常规测试脚本并逐步执行代码。我可以创建DocumentClient。
最终字符串键=“ 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
}
}
Run Code Online (Sandbox Code Playgroud)
当我到达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
Run Code Online (Sandbox Code Playgroud)
基本上说它找不到我的cacert条目并拒绝连接。
出了什么问题(我还没有重新启动Windows)。证书看起来还可以,导入似乎可以通过keytool进入cacerts,我使用的是单个jdk ref,但是代码无法连接。
任何人都可以帮忙解开我尚未正确完成的事情,现在必须做一下才能使代码从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());
}
}
Run Code Online (Sandbox Code Playgroud)
因此,我导出了cosmosDB证书,并将其命名为,并将其documentdbemulatorcert.cer
存储在官方教程提供的D盘中。看来您做过同样的事情。
然后,我尝试将cosmosdb的证书导入到JVM信任证书列表中。您还可以参考以下步骤:
步骤1:打开CMD命令窗口并输入%JAVA_HOME%/jdk/jre/lib/security
目录。请注意,必须使用以下命令打开命令窗口administrator privileges
对我来说,目录看起来像 C:\Program Files\Java\jdk1.8.0_131\jre\lib\security
步骤2:在打开的命令窗口中,输入以下命令:
keytool -import -alias cacerts -keystore cacerts -file d:\documentdbemulatorcert.cer
Run Code Online (Sandbox Code Playgroud)
输入默认密码:changeit
,然后输入y或反对 Trust this certificate? [no]:
第三步 您可以查看Certificate was added to keystore
证书是否已成功导入。
您可以参考下面的屏幕截图了解整个过程吗?
最后,我的代码有效!
您还可以参考下面的SO线程?
1. 无法找到到请求目标的有效证书路径-即使导入证书后也出错
2. “ PKIX路径构建失败”和“无法找到到所请求目标的有效认证路径”
希望对您有帮助。
归档时间: |
|
查看次数: |
1406 次 |
最近记录: |