javaweblogicjmx

List all the user in weblogic by java


Does anyone know how to list all the weblogic users in java? For instance, there is 5 users in security realm, and I want to get all of them. How do I do?


Solution

  • It's pretty easy. For future reference, if you want to look up something like "how do I do X with weblogic and Java..." use JMX in your google search. Here is an example from weblogic wonders. Note you will need to change your URL and user/password in the code:

    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;
    
        String userCurName = userReaderMBean.listUsers(“*”, 100);
    
        while (userReaderMBean.haveCurrent(userCurName) )
        {
          String user = userReaderMBean.getCurrentName(userCurName);
          System.out.println(“\n User: ” + user);
          userReaderMBean.advance(userCurName);
        }
    
      }
      catch (Exception e)
      {
        e.printStackTrace();
      }
      }
    }
    

    EDIT


    There isn't really any way around have to know the user/password to look up the users. You can do it via WLST scripting as well if that sounds like a better option. See an example here.

    Last but not least, you could set anonymous bind on the embedded ldap for Weblogic to allow anonymous lookups (which is generally not recommended for production). This sample shows how to do it with an external client: Weblogic w/External Ldap Client

    The key settings are:

    Your Domain -> Security -> Embedded LDAP
    Change the (default generated) password (for example: weblogic)
    Enable “Anonymous Bind Allowed”