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
71 lines (63 loc) · 2.96 KB
/
Copy pathGSSAPICredentialsExample.java
File metadata and controls
71 lines (63 loc) · 2.96 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
/**
* 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.MongoClientAuthority;
import com.mongodb.MongoClientCredentials;
import com.mongodb.MongoClientOptions;
import com.mongodb.ServerAddress;
import com.mongodb.WriteResult;
import java.net.UnknownHostException;
import java.security.Security;
/**
* Example usage of Kerberos (GSSAPI) credentials.
* <p>
* Usage:
* </p>
* <pre>
* java CramMd5CredentialsExample 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
// You may also need to define realms and domain_realm entries in your krb5.conf file (in /etc by default)
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 dbName = args[2];
MongoClient mongo = new MongoClient(
new MongoClientAuthority(new ServerAddress(server),
new MongoClientCredentials(user, MongoClientCredentials.GSSAPI_MECHANISM)),
new MongoClientOptions.Builder().socketKeepAlive(true).socketTimeout(30000).build());
DB testDB = mongo.getDB(dbName);
System.out.println("Find one: " + testDB.getCollection("test").findOne());
System.out.println("Count: " + testDB.getCollection("test").count());
WriteResult writeResult = testDB.getCollection("test").insert(new BasicDBObject());
System.out.println("Write result: " + writeResult);
System.out.println();
System.out.println("Count: " + testDB.getCollection("test").count());
}
}