List Users and Groups in Weblogic using JMX

There are times when an Application Needs to interact with the Weblogic Server Embedded LDAP Server and Add/Modify/List users or groups. Weblogic Server provides UserReaderMBean, UserRemoverMBean, GroupReaderMBean, GroupRemoverMBean for this purpose.

Below you will find a sample to list users and groups present in the Embedded Ldap Server. This program will give you an idea which you can further develop on.

import javax.naming.*;
import javax.management.MBeanInfo;
import weblogic.jndi.Environment;
import weblogic.management.runtime.ServerRuntimeMBean;
import weblogic.security.providers.authentication.DefaultAuthenticatorMBean;
import weblogic.management.security.authentication.UserReaderMBean;
import weblogic.management.security.authentication.GroupReaderMBean;
import weblogic.management.MBeanHome;
import weblogic.management.WebLogicMBean;
import weblogic.management.tools.Info;
import weblogic.management.Helper;
import weblogic.management.security.authentication.*;

public class ListUsersAndGroups
{
public static void main(String[] args)
{

MBeanHome home = null;
try
{

Environment env = new Environment();
env.setProviderUrl(“t3://localhost:7001?);
env.setSecurityPrincipal(“weblogic”);
env.setSecurityCredentials(“weblogic”);
Context ctx = env.getInitialContext();

home = (MBeanHome)ctx.lookup(“weblogic.management.adminhome”);

weblogic.management.security.RealmMBean rmBean = home.getActiveDomain().getSecurityConfiguration().getDefaultRealm();

AuthenticationProviderMBean[] authenticationBeans = rmBean.getAuthenticationProviders();
DefaultAuthenticatorMBean defaultAuthenticationMBean = (DefaultAuthenticatorMBean)authenticationBeans[0];
UserReaderMBean userReaderMBean = (UserReaderMBean)defaultAuthenticationMBean;
GroupReaderMBean groupReaderMBean = (GroupReaderMBean)defaultAuthenticationMBean;

String userCurName = userReaderMBean.listUsers(“*”, 100);

while (userReaderMBean.haveCurrent(userCurName) )
{
String user = userReaderMBean.getCurrentName(userCurName);
System.out.println(“\n User: ” + user);
userReaderMBean.advance(userCurName);
}

String cursorName = groupReaderMBean.listGroups(“*”, 100);
while (groupReaderMBean.haveCurrent(cursorName) )
{

String group = groupReaderMBean.getCurrentName(cursorName);
System.out.println(“\n Group: ” + group);
groupReaderMBean.advance(cursorName);
}

}
catch (Exception e)
{
e.printStackTrace();
}
}
}

Latest Comments

  1. RC2 March 15, 2012
  2. Kishi April 25, 2012
    • Administrator May 9, 2012
  3. Sathish Kannan December 10, 2014
  4. Nasir August 5, 2015
    • Administrator August 11, 2015

Leave a Reply