-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHazelcastClientMain.java
More file actions
57 lines (46 loc) · 2.21 KB
/
Copy pathHazelcastClientMain.java
File metadata and controls
57 lines (46 loc) · 2.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
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
package cz.cacek.kerberos.hazelcast;
import static cz.cacek.kerberos.Krb5Constants.KRB5_OID;
import java.time.LocalTime;
import java.util.Map;
import org.ietf.jgss.GSSContext;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;
import org.ietf.jgss.GSSName;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.security.SimpleTokenCredentials;
/**
* Sample GSS-API/Kerberos client authentication in Hazelcast Enterprise.
* <p>
* KDC has to be available/reachable for the authentication.
*/
public class HazelcastClientMain {
public static void main(String[] args) throws GSSException {
// System.setProperty("sun.security.krb5.debug", "true");
System.setProperty("java.security.auth.login.config", "jaas.conf");
System.setProperty("java.security.krb5.conf", "krb5.conf");
System.setProperty("javax.security.auth.useSubjectCredsOnly", "false");
GSSManager manager = GSSManager.getInstance();
GSSName servicePrincipalName = manager.createName("hazelcast/localhost@TEST.REALM", null);
GSSContext gssContext = manager.createContext(servicePrincipalName, KRB5_OID, null, GSSContext.DEFAULT_LIFETIME);
gssContext.requestMutualAuth(false);
byte[] token = gssContext.initSecContext(new byte[0], 0, 0);
if (!gssContext.isEstablished()) {
System.err.println("Multi-step GSS-API context initialization is not supported");
System.exit(1);
}
ClientConfig clientConfig = new ClientConfig();
clientConfig.getSecurityConfig().setCredentials(new SimpleTokenCredentials(token));
clientConfig.getNetworkConfig().addAddress("localhost");
HazelcastInstance hz = HazelcastClient.newHazelcastClient(clientConfig);
Map<String, String> testMap = hz.getMap("test");
String oldVal = testMap.put("lastRun", LocalTime.now().toString());
if (oldVal == null) {
System.out.println("This is the first run of the client application");
} else {
System.out.println("Last client application run was at " + oldVal);
}
hz.shutdown();
}
}