Skip to content

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

8 Comments

  1. partha

    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.

    Posted on 01-Jul-10 at 11:27 am | Permalink
  2. admin

    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

    Posted on 01-Jul-10 at 12:50 pm | Permalink
  3. Pranxas

    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

    Posted on 07-Jan-11 at 11:07 am | Permalink
  4. Administrator

    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

    Posted on 11-Jan-11 at 8:34 am | Permalink
  5. Corneliu

    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

    Posted on 09-Feb-11 at 5:23 am | Permalink
  6. Administrator

    Kindly post your question to our forum

    http://weblogic-wonders.com/weblogic/forum/#/

    Posted on 06-Apr-11 at 4:42 am | Permalink
  7. André Simões

    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é

    Posted on 16-Jun-11 at 10:36 am | Permalink
  8. anandraj

    You would require to follow the below steps.
    1. Install / Configure WebLogic Server.
    2. Create a domain.

    Later on you can make use of the procedure to create users.

    Refer the below link for configuration of WebLogic Server.

    http://download.oracle.com/docs/cd/E13179_01/common/docs103/install/prepare.html#wp1130320

    Do let us , if that resolves your query. We would be more than happy to help you.

    Regards,
    Anandraj

    Posted on 23-Jun-11 at 1:47 am | Permalink

Post a Comment

Your email is never published nor shared. Required fields are marked *
*
*