forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathX509CredentialsExample.java
More file actions
32 lines (24 loc) · 1.21 KB
/
X509CredentialsExample.java
File metadata and controls
32 lines (24 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import com.mongodb.BasicDBObject;
import com.mongodb.DB;
import com.mongodb.MongoClient;
import com.mongodb.MongoClientOptions;
import com.mongodb.MongoCredential;
import com.mongodb.ServerAddress;
import javax.net.ssl.SSLSocketFactory;
import java.net.UnknownHostException;
import java.util.Arrays;
public class X509CredentialsExample {
public static void main(String[] args) throws UnknownHostException {
String server = args[0];
String user = "CN=client,OU=kerneluser,O=10Gen,L=New York City,ST=New York,C=US";
System.out.println("server: " + server);
System.out.println("user: " + user);
System.out.println();
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
Arrays.asList(MongoCredential.createMongoX509Credential(user)),
new MongoClientOptions.Builder().socketFactory(SSLSocketFactory.getDefault()).build());
DB testDB = mongoClient.getDB("test");
System.out.println("Count: " + testDB.getCollection("test").count());
System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));
}
}