Saturday, October 31, 2015
WebSphere Applcation Server - how to get J2C authentication alias in Java code
Sometimes I need securely connect to external system from WebSphere Application server application. The secure connection usualy requires password as a credential. The WebSphere Application Server allows store the username and password inside its configuration as so called J2C Authentication Alias. If I need use the cofigured J2C Auth Alias I can use the following Java code fragment:
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
/** Return username and pasword for specific J2C authentication alias
*
* @param authenticationAlias
* @return Map with "username" and "password" keys
* @throws NotImplementedException
* @throws LoginException
*/
public Map<String,String> getCredentials(String authenticationAlias) throws NotImplementedException, LoginException {
Map<String,String> map = new HashMap<String,String>();
map.put(Constants.MAPPING_ALIAS, authenticationAlias);
CallbackHandler callbackHandler = WSMappingCallbackHandlerFactory.getInstance().getCallbackHandler(map, null);
LoginContext loginContext = new LoginContext("DefaultPrincipalMapping", callbackHandler);
loginContext.login();
Subject subject = loginContext.getSubject();
Set credentials = subject.getPrivateCredentials();
PasswordCredential passwordCredential = (PasswordCredential) credentials.iterator().next();
String user = passwordCredential.getUserName();
String password = new String(passwordCredential.getPassword());
Map<String,String> result = new HashMap<String,String>();
result.put(USERNAME, user);
result.put(PASSWORD, password);
return result;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment