Last active
August 29, 2015 14:04
-
-
Save tristantarrant/880a120ee3f67758be30 to your computer and use it in GitHub Desktop.
AuthenticatedHotRodClient.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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