forked from mongodb/mongo-java-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGSSAPICredentialsExample.java
More file actions
78 lines (68 loc) · 3.73 KB
/
GSSAPICredentialsExample.java
File metadata and controls
78 lines (68 loc) · 3.73 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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/**
* Copyright (c) 2008 - 2012 10gen, Inc. <http://10gen.com>
* <p/>
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p/>
* http://www.apache.org/licenses/LICENSE-2.0
* <p/>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
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 java.net.UnknownHostException;
import java.security.Security;
import java.util.Arrays;
/**
* Example usage of Kerberos (GSSAPI) credentials.
* <p>
* Usage:
* </p>
* <pre>
* java GSSAPICredentialsExample server userName databaseName
* </pre>
*/
public class GSSAPICredentialsExample {
// Steps:
// 1. Install unlimited strength encryption jar files in jre/lib/security
// (e.g. http://www.oracle.com/technetwork/java/javase/downloads/jce-6-download-429243.html)
// 2. run kinit
// 3. Set system properties, e.g.:
// -Djava.security.krb5.realm=10GEN.ME -Djavax.security.auth.useSubjectCredsOnly=false -Djava.security.krb5.kdc=kdc.10gen.me
// auth.login.defaultCallbackHandler=name of class that implements javax.security.auth.callback.CallbackHandler
public static void main(String[] args) throws UnknownHostException, InterruptedException {
// Set this property to avoid the default behavior where the program prompts on the command line for username/password
// Security.setProperty("auth.login.defaultCallbackHandler", "DefaultSecurityCallbackHandler");
String server = args[0];
String user = args[1];
String databaseName = args[2];
System.out.println("javax.security.auth.useSubjectCredsOnly: " + System.getProperty("javax.security.auth.useSubjectCredsOnly"));
System.out.println("java.security.krb5.realm: " + System.getProperty("java.security.krb5.realm"));
System.out.println("java.security.krb5.kdc: " + System.getProperty("java.security.krb5.kdc"));
System.out.println("auth.login.defaultCallbackHandler: " + Security.getProperty("auth.login.defaultCallbackHandler"));
System.out.println("login.configuration.provider: " + Security.getProperty("login.configuration.provider"));
System.out.println("java.security.auth.login.config: " + Security.getProperty("java.security.auth.login.config"));
System.out.println("login.config.url.1: " + Security.getProperty("login.config.url.1"));
System.out.println("login.config.url.2: " + Security.getProperty("login.config.url.2"));
System.out.println("login.config.url.3: " + Security.getProperty("login.config.url.3"));
System.out.println("server: " + server);
System.out.println("user: " + user);
System.out.println("database: " + databaseName);
System.out.println();
MongoClient mongoClient = new MongoClient(new ServerAddress(server),
Arrays.asList(MongoCredential.createGSSAPICredential(user).withMechanismProperty("SERVICE_NAME", "mongodb")),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
DB testDB = mongoClient.getDB(databaseName);
System.out.println("Insert result: " + testDB.getCollection("test").insert(new BasicDBObject()));
System.out.println("Count: " + testDB.getCollection("test").count());
}
}