How to test host / port / user / pass for remote JNDI authenticates and is correct in JBoss EAP 7.1
Environment
Red Hat Enterprise Application Platform (EAP) 7.1
Issue
- How to test user / pass for remote JNDI in JBoss EAP 7.1
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:
private String validateConnectionInfo(String host, Integer port, String username, String password) {
try {
Context ctx = getInitialContext(host, port, username, password);
NamingEnumeration en = ctx.list("/");
} catch(javax.naming.CommunicationException ce) {
if(ce.getCause() instanceof javax.security.sasl.SaslException)
return "wrong username or password";
else if(ce.getCause() instanceof java.net.ConnectException)
return "wrong host or port";
} catch(NamingException ne) { // some other naming exception
return ne.getClass().getName() + ": " + ne.getMessage();
}
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.wildfly.naming.client.WildFlyInitialContextFactory");
props.put(Context.PROVIDER_URL, String.format("%s://%s:%d", "remote+http", 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 user / pass for Remote Naming JNDI in JBoss EAP 6.4 / 7.0
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.