Skip to content

Instantly share code, notes, and snippets.

@tristantarrant
Last active August 29, 2015 14:04
Show Gist options
  • Select an option

  • Save tristantarrant/880a120ee3f67758be30 to your computer and use it in GitHub Desktop.

Select an option

Save tristantarrant/880a120ee3f67758be30 to your computer and use it in GitHub Desktop.
AuthenticatedHotRodClient.java
import java.util.Collections;
import javax.security.sasl.Sasl;
import org.infinispan.client.hotrod.RemoteCache;
import org.infinispan.client.hotrod.RemoteCacheManager;
import org.infinispan.client.hotrod.configuration.ConfigurationBuilder;
public class AuthenticatedHotRodClient {
public static void main(String[] args) {
ConfigurationBuilder builder = new ConfigurationBuilder();
builder
.connectionPool()
.maxTotal(1)
.security()
.authentication()
.enable()
.serverName("localhost")
.saslMechanism("PLAIN")
.callbackHandler(new TestCallbackHandler("user", "ApplicationRealm", "qwer1234!".toCharArray()));
RemoteCacheManager rcm = new RemoteCacheManager(builder.build());
RemoteCache<String, String> cache = rcm.getCache("secured");
cache.getVersion();
cache.put("key", "value");
cache.get(key);
rcm.stop();
}
public static class TestCallbackHandler implements CallbackHandler {
final private String username;
final private char[] password;
final private String realm;
public TestCallbackHandler(String username, String realm, char[] password) {
this.username = username;
this.password = password;
this.realm = realm;
}
@Override
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
for (Callback callback : callbacks) {
if (callback instanceof NameCallback) {
NameCallback nameCallback = (NameCallback) callback;
nameCallback.setName(username);
} else if (callback instanceof PasswordCallback) {
PasswordCallback passwordCallback = (PasswordCallback) callback;
passwordCallback.setPassword(password);
} else if (callback instanceof AuthorizeCallback) {
AuthorizeCallback authorizeCallback = (AuthorizeCallback) callback;
authorizeCallback.setAuthorized(authorizeCallback.getAuthenticationID().equals(
authorizeCallback.getAuthorizationID()));
} else if (callback instanceof RealmCallback) {
RealmCallback realmCallback = (RealmCallback) callback;
realmCallback.setText(realm);
} else {
throw new UnsupportedCallbackException(callback);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment