Creating Users in Weblogic Server Embedded LDAP Programatically.

I have received many requests from Clients for code snippet to create users programmatically.Below is a sample code by which we can create users in the Embedded LDAP of Weblogic Server.Put simple, this program creates an MBean Server connection, traverses to the relevant Mbean and invokes the right method to create the user.

Details can be found at this link.
http://download.oracle.com/docs/cd/E13222_01/wls/docs90/jmx/accessWLS.html
Use the following code is a JSP and do the necessary import and acess the jsp page.

User testuser will be created in the embedded ldap with the password as password.

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://10.10.71.52:7001”);
InitialContext ctx = new InitialContext(env);
MBeanServer wls = (MBeanServer) ctx.lookup(“java:comp/env/jmx/runtime”);
ObjectName userEditor = null;
ObjectName MBTservice = 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[] atnProviders = (ObjectName[]) wls.getAttribute(defaultRealm,”AuthenticationProviders”);

for (ObjectName providerName : atnProviders) {
if (userEditor == null) {
ModelMBeanInfo info = (ModelMBeanInfo) wls.getMBeanInfo(providerName);
String className = (String) info.getMBeanDescriptor().getFieldValue(“interfaceClassName”);
if (className != null) {
String[] mba = (String[]) wls.invoke( MBTservice, “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{

out.println(“Creating User : testuser”);
wls.invoke(userEditor,”createUser”,new Object[] {“testuser”,”password”,”test user”},new String[] {“java.lang.String”, “java.lang.String”,”java.lang.String”});
out.println(“Created User : testuser”);
}

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

9 comments

  1. Hi,
    I am working with WebLogic 8.1,I am trying to create a user from remote client by using the above mentioned code,
    but i am getting the following exception:

    javax.management.InstanceNotFoundException: com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean
    at weblogic.rjvm.BasicOutboundRequest.sendReceive(BasicOutboundRequest.java:108)
    at weblogic.rmi.internal.BasicRemoteRef.invoke(BasicRemoteRef.java:164)
    at weblogic.management.internal.RemoteMBeanServerImpl_813_WLStub.getAttribute(Unknown Source)
    at com.neotel.cramer.common.WebLogicTest.crUser(WebLogicTest.java:76)
    at com.neotel.cramer.common.WebLogicTest.main(WebLogicTest.java:48)
    Caused by: javax.management.InstanceNotFoundException: com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean
    at com.sun.management.jmx.MBeanServerImpl.getMBean(MBeanServerImpl.java:1680)
    at com.sun.management.jmx.MBeanServerImpl.getAttribute(MBeanServerImpl.java:1152)
    at weblogic.management.internal.RemoteMBeanServerImpl.getAttribute(RemoteMBeanServerImpl.java:288)
    at weblogic.management.internal.RemoteMBeanServerImpl_WLSkel.invoke(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:492)
    at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:435)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:147)
    at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:430)
    at weblogic.rmi.internal.BasicExecuteRequest.execute(BasicExecuteRequest.java:35)
    at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:224)
    at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:183)

    plzzz help me out,it’s very urgent.

    1. This code is for version 9.2 and above.
      It needs some modification to work in 8.1 since there has been change in JMX specification since 8.1.

      I’ll modify it and update the comment once I get time..

      -Faisal

  2. Hi,

    Using the example above I was able to create users/groups, validate users, reset passwords, etc. But how can I get the user’s attributes (displayname, employeenumber, an so on) from de DefaultAuthenticationProvider? What class or interface should I use?

    Thanks in advance

    Pranxas

    1. Hi Pranxas,

      There is no standard api that Weblogic Provides to access attributes of the embedded ldap.
      You need to use the api provided by the jdk.

      The schema of the Embedded LDAP doesnt have the attributes like employee number.

      Are you using the Default Authenticator or you have some external LDAP Authenticator configured?

      -Faisal

  3. Hi,

    I am using WLS 11g and I was able to add a user using your code.
    Can you please tell me how should the folowing line look if I want to modify the password for user “testuser”?
    wls.invoke(userEditor,”createUser”,new Object[] {“testuser”,”password”,”test user”},new String[] {“java.lang.String”, “java.lang.String”,”java.lang.String”});

    Thanks
    Regards Corneliu

  4. Hi,

    For the first time I’m working with Weblogic 10.0. I really want to use the example above, to create users, validadate users, reset passwords.

    But i don’t know if a need add additional configuration to Weblogic server. Did you know if exist any tutorial to configure Weblogic server or step list.

    Thanks, Regards André

  5. Using WL 10.3.6.0, running your code, I’m getting –
    [..]
    javax.naming.NameNotFoundException: While trying to lookup ‘java:comp.env/jmx/runtime’ didn’t find subcontext ‘java:comp’. Resolved ” [Root exception is javax.naming.NameNotFoundException: While trying to lookup ‘java:comp.env/jmx/runtime’ didn’t find subcontext ‘java:comp’. Resolved ”]; remaining name ‘java:comp/env/jmx/runtime’
    at weblogic.rjvm.ResponseImpl.unmarshalReturn(ResponseImpl.java:237)
    at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:348)
    at weblogic.rmi.cluster.ClusterableRemoteRef.invoke(ClusterableRemoteRef.java:259)
    at weblogic.jndi.internal.ServerNamingNode_1036_WLStub.lookup(Unknown Source)
    at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:424)
    at weblogic.jndi.internal.WLContextImpl.lookup(WLContextImpl.java:412)
    at javax.naming.InitialContext.lookup(Unknown Source)
    at sandbox.createWLusers.createWeblogicUser2(createWLusers.java:44)
    at sandbox.createWLusers.createUser(createWLusers.java:29)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.runners.BlockJUnit4ClassRunner.runNotIgnored(BlockJUnit4ClassRunner.java:79)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:71)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:49)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
    Caused by: javax.naming.NameNotFoundException: While trying to lookup ‘java:comp.env/jmx/runtime’ didn’t find subcontext ‘java:comp’. Resolved ”
    at weblogic.jndi.internal.BasicNamingNode.newNameNotFoundException(BasicNamingNode.java:1139)
    at weblogic.jndi.internal.BasicNamingNode.lookupHere(BasicNamingNode.java:247)
    at weblogic.jndi.internal.ServerNamingNode.lookupHere(ServerNamingNode.java:182)
    at weblogic.jndi.internal.BasicNamingNode.lookup(BasicNamingNode.java:206)
    at weblogic.jndi.internal.RootNamingNode_WLSkel.invoke(Unknown Source)
    at weblogic.rmi.internal.BasicServerRef.invoke(BasicServerRef.java:667)
    at weblogic.rmi.cluster.ClusterableServerRef.invoke(ClusterableServerRef.java:230)
    at weblogic.rmi.internal.BasicServerRef$1.run(BasicServerRef.java:522)
    at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:363)
    at weblogic.security.service.SecurityManager.runAs(SecurityManager.java:146)
    at weblogic.rmi.internal.BasicServerRef.handleRequest(BasicServerRef.java:518)
    at weblogic.rmi.internal.wls.WLSExecuteRequest.run(WLSExecuteRequest.java:118)
    at weblogic.work.ExecuteThread.execute(ExecuteThread.java:256)
    at weblogic.work.ExecuteThread.run(ExecuteThread.java:221)
    [..]

    Any idea?

Comments are closed.