从独立的Java客户端以编程方式创建WebLogic用户 [英] Creating WebLogic users programmatically from a standalone Java client

查看:63
本文介绍了从独立的Java客户端以编程方式创建WebLogic用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to create users in WebLogic (10.3.4) programmatically from a simple standalone Java client (one class --> two methods: createWeblogicUser() & main()).

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;看起来我只能在容器中使用它.

Any ideas on what the context lookup I should be making? "java:comp" throws a javax.naming.NameNotFoundException; looks like I can use that only from w/in a container.

推荐答案

使其正常工作.

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的计算机上运行了此程序.因此,classpath条目是jar文件的完全限定路径名.

You need only weblogic.jar and wljmxclient.jar in classpath. I ran this against JDK 1.6.0_29. I have to add that I ran this on a machine on which WebLogic was installed as well. So the classpath entries were fully qualified path names to the jar files.

我遇到一个陷阱": 在提供"com.bea:Name = XXXX,Type = XXXX"的同时,DONOT在任何内容之间都留有空格-不是冒号;不是逗号;没什么-我花了一些时间调试它,直到它最终成功.

One "gotcha" I came across: While providing the "com.bea:Name=XXXX,Type=XXXX", DONOT give a space between anything - not the colon; not the comma; nothing - I spent sometime debugging this, before it finally hit it.

这篇关于从独立的Java客户端以编程方式创建WebLogic用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆