How to test user / pass for Remote Naming JNDI in JBoss EAP 6.4 / 7.0
Environment
- Red Hat JBoss Enterprise Application Platform (EAP)
- 7.0
- 6.4
Issue
- How to test user / pass for Remote Naming JNDI in JBoss EAP 6.4 / 7.0
Resolution
Calling context.list("") can be used to validate the host / port and username / password. Checking the exception cause can differentiate the issue for example:
import javax.naming.*;
import java.util.*;
private static String validateConnectionInfo(String host, Integer port, String username, String password) {
Context ctx = null;
try {
ctx = getInitialContext(host, port, username, password);
NamingEnumeration en = ctx.list("/");
} catch(javax.naming.CommunicationException ce) {
return "wrong host or port";
} catch(javax.naming.AuthenticationException ae) {
if(ae.getCause() instanceof javax.security.sasl.SaslException)
return "wrong username or password";
} catch(NamingException ne) { // some other naming exception
return ne.getClass().getName() + ": " + ne.getMessage();
} finally {
// make sure to close the Remote Naming context to prevent connection leaks
if(ctx != null) {
try { ctx.close(); } catch(Exception e) { }
}
}
return "valid";
}
private static Context getInitialContext(String host, Integer port, String username, String password) throws NamingException {
Properties props = new Properties();
props.put(Context.INITIAL_CONTEXT_FACTORY, "org.jboss.naming.remote.client.InitialContextFactory");
props.put(Context.PROVIDER_URL, String.format("%s://%s:%d", "remote", host, port));
if(username != null && password != null) {
props.put(Context.SECURITY_PRINCIPAL, username);
props.put(Context.SECURITY_CREDENTIALS, password);
}
return new InitialContext(props);
}
See also: How to test host / port / user / pass for remote JNDI authenticates and is correct in JBoss EAP 7.1
SBR
Components
Category
This solution is part of Red Hat’s fast-track publication program, providing a huge library of solutions that Red Hat engineers have created while supporting our customers. To give you the knowledge you need the instant it becomes available, these articles may be presented in a raw and unedited form.