3

我正在尝试从一个简单的独立Java客户端(一个类->两种方法:createWeblogicUser()和main())以编程方式在WebLogic(10.3.4)中创建用户。

public void createWeblogicUser() {
try {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
    env.put(Context.SECURITY_PRINCIPAL, "weblogic");
    env.put(Context.SECURITY_CREDENTIALS, "weblogic");
    env.put(Context.PROVIDER_URL, "t3://myserver:7001");

    InitialContext ctx = new InitialContext(env);
    MBeanServer wls = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime");

    ObjectName userEditor = null;
    ObjectName mBeanTypeService = new ObjectName( "com.bea:Name=MBeanTypeService, Type=weblogic.management.mbeanservers.MBeanTypeService");
    ObjectName rs = new ObjectName("com.bea:Name=RuntimeService, Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
    ObjectName domainMBean = (ObjectName) wls.getAttribute(rs, "DomainConfiguration");
    ObjectName securityConfig = (ObjectName) wls.getAttribute(domainMBean, "SecurityConfiguration");
    ObjectName defaultRealm = (ObjectName) wls.getAttribute(securityConfig, "DefaultRealm");
    ObjectName[] authProviders = (ObjectName[]) wls.getAttribute(defaultRealm, "AuthenticationProviders");

    for(ObjectName providerName : authProviders) {
        if(userEditor == null) {
            ModelMBeanInfo info = (ModelMBeanInfo) wls.getMBeanInfo(providerName);
            String className = (String) info.getMBeanDescriptor().getFieldValue("interfaceClassName");
            if(className != null) {
                String[] mba = (String[]) wls.invoke(mBeanTypeService
                                                    , "getSubtypes"
                                                    , new Object[] {"weblogic.management.security.authentication.UserEditorMBean"}
                                                    , new String[] {"java.lang.String"}
                                            );
                for(String mb : mba) {
                    if(className.equals(mb))
                        userEditor = providerName;
                }
            }
        }

        if(userEditor == null)
            throw new RuntimeException("Could not retrieve user editor");

        try {
            wls.invoke(userEditor
                        , "createUser"
                        , new Object[] {"wls_user", "password123","User created programmatically."}
                        , new String[] {"java.lang.String", "java.lang.String","java.lang.String"}
            );
        }
        catch(Exception e){
            e.printStackTrace();
        }

        ctx.close();
    }
}
catch(Exception ex) {
    ex.printStackTrace();
}

}

关于我应该进行什么上下文查找的任何想法?“java:comp”抛出 javax.naming.NameNotFoundException;看起来我只能在容器中使用它。

4

1 回答 1

2

让它工作。

private void createWeblogicUser(String username) {
try {
    Hashtable<String, String> env = new Hashtable<String, String>();
    env.put(Context.SECURITY_PRINCIPAL, "weblogic");
    env.put(Context.SECURITY_CREDENTIALS, "weblogic");

    String hostname = "myserver";
    int port = 7001;
    String protocol = "rmi";
    String url= new String("/jndi/iiop://myserver:7001/weblogic.management.mbeanservers.domainruntime");

    JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, url);
    JMXConnector connector = JMXConnectorFactory.connect(serviceURL, env);
    MBeanServerConnection connection = connector.getMBeanServerConnection();

    ObjectName userEditor = null;
    ObjectName mBeanTypeService = new ObjectName( "com.bea:Name=MBeanTypeService,Type=weblogic.management.mbeanservers.MBeanTypeService");
    ObjectName rs = new ObjectName("com.bea:Name=DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean");
    ObjectName domainMBean = (ObjectName) connection.getAttribute(rs, "DomainConfiguration");
    ObjectName securityConfig = (ObjectName) connection.getAttribute(domainMBean, "SecurityConfiguration");
    ObjectName defaultRealm = (ObjectName) connection.getAttribute(securityConfig, "DefaultRealm");
    ObjectName[] authProviders = (ObjectName[]) connection.getAttribute(defaultRealm, "AuthenticationProviders");

    for(ObjectName providerName : authProviders) {
        System.out.println("Auth provider is: " + providerName) ;

        if(userEditor == null) {
            ModelMBeanInfo info = (ModelMBeanInfo) connection.getMBeanInfo(providerName);
            String className = (String) info.getMBeanDescriptor().getFieldValue("interfaceClassName");
            System.out.println("className is: " + className) ;

            if(className != null) {
                String[] mba = (String[]) connection.invoke(mBeanTypeService
                                        , "getSubtypes"
                                        , new Object[] {"weblogic.management.security.authentication.UserEditorMBean"}
                                        , new String[] {"java.lang.String"}
                                    );
                for(String mb : mba) {
                    System.out.println("Model Bean is: " + mb) ;
                    if(className.equals(mb)) {
                        System.out.println("Found a macth for the model bean and class name!") ;
                        userEditor = providerName;
                    }
                }
            }
        }
    }

    if(userEditor == null)
        throw new RuntimeException("Could not retrieve user editor");

    try {
        connection.invoke(userEditor
                    , "createUser"
                    , new Object[] {username, "password123","User created programmatically."}
                    , new String[] {"java.lang.String", "java.lang.String","java.lang.String"}
        );

        System.out.println("User created successfully") ;
    }
    catch(Exception e){
        e.printStackTrace();
    }

    connector.close();

}
catch(Exception ex) {
    ex.printStacktrace();
}

}

您只需要类路径中的 weblogic.jar 和 wljmxclient.jar。我在 JDK 1.6.0_29 上运行了这个。我必须补充一点,我在一台也安装了 WebLogic 的机器上运行了它。所以类路径条目是 jar 文件的完全限定路径名。

我遇到了一个“陷阱”:在提供“com.bea:Name=XXXX,Type=XXXX”时,不要在任何东西之间留一个空格——不是冒号;不是逗号;什么都没有——我花了一些时间调试它,然后才终于成功。

于 2012-04-29T04:43:55.847 回答