Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs 菜单
Docs 主页
/ / /
Java (Sync) 驱动程序
/ /

x.509

X.509 身份验证机制中,服务器和客户端使用 TLS 协议交换 X.509 公钥证书。您可以使用此机制对MongoDB Atlas、 MongoDB Enterprise Advanced和MongoDB Community Edition进行身份验证。

提示

X.509 机制

要学习;了解如何将 TLS/SSL 与 Java 驾驶员结合使用,请参阅在连接上启用 TLS/SSL指南。

有关 X.509 证书的更多信息,请参阅MongoDB Server手册中的使用 x.509 证书对自管理部署上的客户端进行身份验证

本页上的代码示例使用以下占位符:

  • <hostname>: MongoDB 部署的网络解决。

  • <port>: MongoDB 部署的端口号。 如果省略此参数,驾驶员将使用默认端口号 (27017)。 连接MongoDB Atlas 群集时无需端口号。

  • <authenticationDb>:包含用户身份验证数据的MongoDB 数据库。如果省略此占位符,驾驶员将使用默认的admin数据库。

要使用代码示例,请将这些占位符替换为您自己的值。

选择下面的 Connection StringMongoCredential标签页,查看指定此身份验证机制的说明和示例代码:

要使用连接字符串指定 X.509身份验证机制,请将 authMechanism 参数分配给 MONGODB-X509,并将 tls 参数分配给 true 以启用TLS。 用于实例化 MongoClient 的代码如下所示:

MongoClient mongoClient = MongoClients.create("mongodb://<hostname>:<port>/?authSource=<authenticationDb>&authMechanism=MONGODB-X509&tls=true");

To specify the X.509 authentication mechanism by using the MongoCredential class, use the createMongoX509Credential() method. Also, enable TLS by calling the applyToSslSettings() method and setting the enabled property to true in the SslSettings.Builder block. The code to instantiate a MongoClient resembles the following:

MongoCredential credential = MongoCredential.createMongoX509Credential();
MongoClient mongoClient = MongoClients.create(
MongoClientSettings.builder()
.applyToClusterSettings(builder ->
builder.hosts(Arrays.asList(new ServerAddress("<hostname>", <port>))))
.applyToSslSettings(builder ->
builder.enabled(true);
)
.credential(credential)
.build());

要学习;了解有关此页面上使用的任何MongoDB方法和类型的更多信息,请参阅以下API文档:

后退

SCRAM

在此页面上