Friday, June 27, 2014

(J2EE) WebSphere User Registry lookup and password check

try 
{
 // Establish connection with the WebSphere user registry
 javax.naming.InitialContext initialContext = new javax.naming.InitialContext();
 com.ibm.websphere.security.UserRegistry registry = (com.ibm.websphere.security
             .UserRegistry) initialContext.lookup("UserRegistry");

 
 // Retrieves the user from the UserRegistry, if the user is not found
 // an EntryNotFoundException will be encountered
 String uniqueID = registry.getUniqueUserId(username);
 String uid = com.ibm.wsspi.security.token.WSSecurityPropagationHelper
             .getUserFromUniqueID (uniqueID);
   
 // Retrieve the securityName for the checkPassword method
 String securityName = registry.getUserSecurityName(uid);
 /*
  * Ignore warning, checkPassword returns string if securityName and 
  * password are correct otherwise, it throws a PasswordCheckFailedException
  */
 @SuppressWarnings("unused")
 String passwordCheck = registry.checkPassword(securityName, password);
   
 // IF and when no exceptions were encountered, authentication is successful
 // --- insert things-to-do-once-authenticated code here ---
catch (EntryNotFoundException enfe)
{
 // User is not in the registry
 System.out.println("Invalid username/password");
}
catch (PasswordCheckFailedException pcfe)
{
 // User is in the registry but the password is not right
 System.out.println("Invalid username/password");
}
catch (Exception e)
{
 // Catch all scenario
 System.out.println("Oh snap! Some shit just happened.");
}


No comments: