12 Replies Latest reply: May 13, 2013 2:50 PM by 1008668 RSS

    How to create user from one domain to remote domain

    Roshni Shankar
      Hi All,

      I want to create user in Security Realm from my own domain to a remote domain programatically. Can you suggest the entire process.

      Thanks in Advance.
        • 1. Re: How to create user from one domain to remote domain
          Akshay Koul
          Do you want to achieve something like this:

          http://docs.oracle.com/cd/E17904_01/web.1111/e13707/domain.htm#autoId1

          -Akshay
          • 2. Re: How to create user from one domain to remote domain
            murali veligeti - oracle
            Please try with the below code

            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();
            • 3. Re: How to create user from one domain to remote domain
              Roshni Shankar
              Thanx Murali.

              I started with this code only. I created a java class in JDeveloper and pasted this code in that class. But I am getting a red mark at the line,

              Hashtable env = new Hashtable();
              env.put(Context.INITIAL_CONTEXT_FACTORY,”weblogic.jndi.WLInitialContextFactory”); env.put(Context.SECURITY_PRINCIPAL, “weblogic”);
              Its asking me to create env class but env is an object of Hashtable I believe.
              Please suggest something.
              • 4. Re: How to create user from one domain to remote domain
                Roshni Shankar
                Thanks Akshay,

                I did the steps provided in that link. But now I am not able to understand that how can I create a user in a remote domain from my local domain. I guess that is not mentioned in the link you provided. Can you help me with this?? Also we have not mentioned the remote server URL anywhere in this link only the domain name is mentiones in the Credential Mapping tab. Would then the remote domain be accessible from the local domain.

                Edited by: Roshni Shankar on Apr 20, 2012 2:57 AM
                • 5. Re: How to create user from one domain to remote domain
                  Akshay Koul
                  Not sure whether this will solve your purpose however it works as below.

                  A credential map is a mapping of credentials used by WebLogic Server to credentials used in a legacy or remote system, which tell WebLogic Server how to connect to a given resource in that system. In other words, credential maps allow WebLogic Server to log into a remote system on behalf of a subject that has already been authenticated.

                  So, you need to create a user on Remote Domain or use an already existing user on remote domain for mapping to local domain.

                  The Credential Mapper identifies domains by their names. Therefore, it is important that the domains involved have unique names.

                  -Akshay
                  • 6. Re: How to create user from one domain to remote domain
                    murali veligeti - oracle
                    Not sure why but for me all the errors were resolved .


                    import java.util.Hashtable;

                    import javax.management.AttributeNotFoundException;
                    import javax.management.InstanceNotFoundException;
                    import javax.management.IntrospectionException;
                    import javax.management.MBeanException;
                    import javax.management.MBeanServer;
                    import javax.management.MalformedObjectNameException;
                    import javax.management.ObjectName;
                    import javax.management.ReflectionException;
                    import javax.management.modelmbean.ModelMBeanInfo;
                    import javax.naming.Context;
                    import javax.naming.InitialContext;
                    import javax.naming.NamingException;

                    public class Test {

                         /**
                         * @param args
                         * @throws NamingException
                         * @throws NullPointerException
                         * @throws MalformedObjectNameException
                         * @throws ReflectionException
                         * @throws MBeanException
                         * @throws InstanceNotFoundException
                         * @throws AttributeNotFoundException
                         * @throws IntrospectionException
                         */
                         public static void main(String[] args) throws NamingException, MalformedObjectNameException, NullPointerException, AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IntrospectionException {
                              // TODO Auto-generated method stub

                              Hashtable env = new Hashtable();
                              env.put(Context.INITIAL_CONTEXT_FACTORY,"weblogic.jndi.WLInitialContextFactory");
                              env.put(Context.SECURITY_PRINCIPAL, "weblogic");
                              env.put(Context.SECURITY_CREDENTIALS, "weblogic1");
                              env.put(Context.PROVIDER_URL, "t3://localhost: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{

                              System.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"});
                              System.out.println("Created User : testuser");
                              }

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

                    }
                    • 7. Re: How to create user from one domain to remote domain
                      Roshni Shankar
                      Thanks Murali,

                      But now when I am executing this class I am getting the following exception.

                      Exception in thread "main" javax.naming.NoInitialContextException: Cannot instantiate class: weblogic.jndi.WLInitialContextFactory [Root exception is java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory]
                           at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:657)
                           at javax.naming.InitialContext.getDefaultInitCtx(InitialContext.java:288)
                           at javax.naming.InitialContext.init(InitialContext.java:223)
                           at javax.naming.InitialContext.<init>(InitialContext.java:197)
                           at createuserwebserviceproj.Class1.main(Class1.java:29)
                      Caused by: java.lang.ClassNotFoundException: weblogic.jndi.WLInitialContextFactory
                           at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
                           at java.security.AccessController.doPrivileged(Native Method)
                           at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
                           at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
                           at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
                           at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
                           at java.lang.Class.forName0(Native Method)
                           at java.lang.Class.forName(Class.java:247)
                           at com.sun.naming.internal.VersionHelper12.loadClass(VersionHelper12.java:46)
                           at javax.naming.spi.NamingManager.getInitialContext(NamingManager.java:654)
                           ... 4 more
                      Process exited with exit code 1.

                      Can you help me with this??
                      • 8. Re: How to create user from one domain to remote domain
                        murali veligeti - oracle
                        While executing this class weblogic.jar should be in the class path.
                        • 9. Re: How to create user from one domain to remote domain
                          Roshni Shankar
                          Thanks a lot Murali,

                          I included weblogic.jar in my classpath and then executed. Still it was giving exception as can't lookup the java:comp/env/jmx/runtime , then I included wljmxclient.jar in my classpath but still on execution its giving the following exception,

                          Exception in thread "main" javax.naming.NameNotFoundException: remaining name: env/jmx/runtime
                               at weblogic.j2eeclient.SimpleContext.resolve(SimpleContext.java:35)
                               at weblogic.j2eeclient.SimpleContext.resolve(SimpleContext.java:39)
                               at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:59)
                               at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:59)
                               at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:64)
                               at javax.naming.InitialContext.lookup(InitialContext.java:392)
                               at createuserwebserviceproj.Test.main(Test.java:30)
                          Process exited with exit code 1.

                          Do I need to include some more jars in my classpath?
                          Please sugggest something.
                          • 10. Re: How to create user from one domain to remote domain
                            MohtashimShaikh
                            I am facing the same exception. Any clue ?

                            Exception in thread "main" javax.naming.NameNotFoundException: remaining name: env/jmx/runtime
                            at weblogic.j2eeclient.SimpleContext.resolve(SimpleContext.java:35)
                            at weblogic.j2eeclient.SimpleContext.resolve(SimpleContext.java:39)
                            at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:59)
                            at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:59)
                            at weblogic.j2eeclient.SimpleContext.lookup(SimpleContext.java:64)
                            at weblogic.jndi.factories.java.ReadOnlyContextWrapper.lookup(ReadOnlyContextWrapper.java:45)
                            at weblogic.jndi.internal.AbstractURLContext.lookup(AbstractURLContext.java:130)
                            at javax.naming.InitialContext.lookup(InitialContext.java:351)
                            at WLITimerTest.main(WLITimerTest.java:52)
                            • 11. Re: How to create user from one domain to remote domain
                              souvik_das
                              You need more jars in your classpath as the java code you are executing is trying to get the JNDI context of the remote weblogic server.
                              The list I am providing below is required for getting the JNDI context for a database connection.
                              Perhaps you can add the jars one after another and see if the runtime exceptions are resolved.
                              I would suggest using a java class finder and run it against you weblogic server libraries to identify all the dependent jars required against each runtime exception.

                              Thanks,
                              Souvik.

                              com.bea.core.antlr_2.7.5.jar
                              com.bea.core.datasource_1.0.1.0.jar
                              com.bea.core.descriptor_1.0.1.0.jar
                              com.bea.core.diagnostics.core_1.0.1.0.jar
                              com.bea.core.diagnostics.instrumentor_1.0.1.0.jar
                              com.bea.core.management.core_1.0.1.0.jar
                              com.bea.core.repackaged.asm_1.5.2.jar
                              com.bea.core.resourcepool_1.0.1.0.jar
                              com.bea.core.store_1.0.1.0.jar
                              com.bea.core.transaction_2.0.1.0.jar
                              com.bea.core.weblogic.rmi.client_1.0.1.0.jar
                              com.bea.core.weblogic.security_2.0.1.0.jar
                              com.bea.core.weblogic.security.wls_2.0.1.0.jar
                              com.bea.core.weblogic.workmanager_1.0.1.0.jar
                              com.bea.core.xml.beaxmlbeans_2.3.1.0.jar
                              com.bea.oracle.ojdbc14_10.2.0.jar
                              ojdbc14.jar
                              weblogic.jar
                              wlclient.jar
                              wljmsclient.jar
                              wls-api.jar
                              • 12. Re: How to create user from one domain to remote domain
                                1008668
                                Dear helpers,

                                Did any one resolve this issue? If so, could you please let me know how did you resolve it?

                                Thanks
                                Babu