java weblogic,从独立的Java客户端以编程方式创建WebLogic用户

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 env = new Hashtable();

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();

}

}

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.

解决方案

Got it to work.

private void createWeblogicUser(String username) {

try {

Hashtable env = new Hashtable();

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();

}

}

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.

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值