我在mongo服务器和客户端之间建立了SSL连接,我用来连接mongo服务器的命令行如下所示,效果很好。
mongo gs-server:27017 --ssl --sslPEMKeyFile E:\mongodb\ssl\ssl_test\client.pem --sslCAFile E:\mongodb\ssl\ssl_test\root-ca.pem
但是当我想用scala代码连接mongo和SSL身份验证时,我遇到了一些问题。我的工作如下:
keytool -import -keystore client.jks -file client.pem -alias client
keytool -import -keystore root-ca.jks -file root-ca.pem -alias root-ca
2.在scala中设置连接设置和系统属性:
val clusterSettings = ClusterSettings.builder().hosts(List(new ServerAddress("gs-server")).asJava).description("mongo Server").build()
System.setProperty("javax.net.ssl.trustStore", "E:\\mongodb\\ssl\\ssl_test\\root-ca.jks")
System.setProperty("javax.net.ssl.trustStorePassword", "password")
System.setProperty("javax.net.ssl.keyStore", "E:\\mongodb\\ssl\\ssl_test\\client.jks")
System.setProperty("javax.net.ssl.keyStorePassword", "password")
val setting = MongoClientSettings.builder().clusterSettings(clusterSettings)
.sslSettings(SslSettings.builder()
.enabled(true)
.invalidHostNameAllowed(true)
.build())
.streamFactoryFactory(NettyStreamFactoryFactory())
.build()
val client = MongoClient(setting)
val db = client.getDatabase("test")
val coll = db.getCollection("test1")
val first_res = coll.drop().printHeadResult()
当我运行这个程序时,我在mongo服务器上找到了日志:
2016-01-14T15:52:20.433+0800 I NETWORK [initandlisten] connection accepted from 10.200.200.72:64193 #11812 (1 connection now open)
2016-01-14T15:52:20.501+0800 E NETWORK [conn11812] no SSL certificate provided by peer; connection rejected
2016-01-14T15:52:20.502+0800 D NETWORK [conn11812] SocketException: remote: 10.200.200.72:64193 error: 9001 socket exception [CONNECT_ERROR] server [no SSL certificate provided by peer; connection rejected]
2016-01-14T15:52:20.502+0800 I NETWORK [conn11812] end connection 10.200.200.72:64193 (0 connections now open)
我之前生成的密钥库和信任库中似乎没有有效的证书。我做错了什么或错过了什么?任何帮助将不胜感激。
BTW,我使用的是Mongo 3.2.0,mongo scala驱动版本是: org.mongodb.scala 蒙戈 - 斯卡拉 - driver_2.11 1.1.0
答案 0 :(得分:0)
您是否尝试过标记allowConnectionsWithoutCertificates(https://docs.mongodb.org/manual/reference/configuration-options/#net.ssl.allowConnectionsWithoutCertificates)。如果您不使用客户端证书,则必须设置此选项。
编辑: 使用以下YAML-config SSL激活,客户端不得提供客户端证书:
...
net:
ssl:
mode: requireSSL
PEMKeyFile: /tmp/mongodb.pem
CAFile: /tmp/cert-chain.pem
**allowConnectionsWithoutCertificates: true**
...