2010-04-20 85 views
3

是否可以通過JNDI在AD rom中創建新用戶?如何從Java(通過JNDI)創建新的Active Directory帳戶?

我試着通過可靠的谷歌,但沒有出現 - 也許我是使用錯誤的術語(JNDI活動目錄創建用戶)googling。

任何提示將被創建讚賞。

當前狀態:我已通過Java代碼連接到AD,並且可以更改現有AD帳戶的屬性;接下來我想能夠從Java/JNDI創建AD用戶。

我使用的是http://forums.sun.com/thread.jspa?threadID=582103,我確定我的帳戶具有創建AD帳戶的正確特權,並且我正在使用LDAPS。

回答

-1

http://forums.sun.com/thread.jspa?threadID=581444&messageID=3313188

編輯:上面的鏈接似乎打破作爲sunoracle合併的結果。下面似乎是新位置線程 http://forums.oracle.com/forums/thread.jspa?threadID=1155430&start=0&tstart=0

public void addUserToGroup(LdapContext ctx, String userDN, String groupDN) 
    throws NamingException { 
    ModificationItem[] mods = new ModificationItem[1]; 
    mods[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE, 
      new BasicAttribute("member", userDN)); 

    ctx.modifyAttributes(groupDN, mods); 
} 

public void removeUserFromGroup(LdapContext ctx, String userDN, 
    String groupDN) throws NamingException { 
    ModificationItem[] mods = new ModificationItem[1]; 
    mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, 
      new BasicAttribute("member", userDN)); 

    ctx.modifyAttributes(groupDN, mods); 
} 
+0

它不回答這個問題嗎? – BrunoJCM 2011-08-29 17:52:00

+0

@BrunoJCM什麼部分沒有回答? – 2011-08-30 02:24:06

+1

用戶詢問如何創建新帳戶,並且此代碼正在管理用戶的組。它似乎沒有回答這個問題。對不起,如果我想念什麼。 – BrunoJCM 2012-10-29 23:41:41

0

這是棘手。您不能將密碼設置爲未加密,並且如果您尚未設置所有加密結構,則無法使用LDAPS,因此您需要改用Kerberos。

我得到它的工作是這樣的: - 做一個簡單的綁定AD - kerberise會話 - 創建一個帶有密碼的用戶帳戶,但它設置過期) 現在你可以使用正常的連接設置其他屬性。

// KRB5 connection details: 
    System.setProperty("java.security.krb5.kdc", "domain.com"); 
    String username = "[email protected]"; 
    String realm = "DOMAIN.COM"; 
    System.setProperty("java.security.krb5.realm", realm); 
    System.setProperty("java.security.krb5.debug", "true"); 
    System.setProperty("sun.security.krb5.debug", "true"); 

// standard JNDI LDAP stuff: 
    env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory"); 
    env.put(Context.SECURITY_AUTHENTICATION, "simple"); 
    env.put(Context.PROVIDER_URL, "ldap://dc01.domain.com:389"); 
    env.put(Context.SECURITY_PRINCIPAL, username);  
    env.put(Context.SECURITY_CREDENTIALS, "abcd1234"); 

    ctxt = new InitialLdapContext(env, new Control [0]); 

// kerberised connection details: 
    LoginModule module; 
    module = (LoginModule) Class.forName("com.sun.security.auth.module.Krb5LoginModule").newInstance(); 
    Subject subject = new Subject(); 
    Map<String, String> options = new HashMap<String, String>(); 
    Map<String, Object> sharedState = new HashMap<String, Object>(); 

    sharedState.put("javax.security.auth.login.password", properties.getProperty("ad.passwd").toCharArray()); 
    sharedState.put("javax.security.auth.login.name", username); 
    options.put("principal", username); 
    options.put("storeKey", "true"); 
    options.put("useFirstPass", "true"); 

    options.put("debug", "true"); 

    module.initialize(subject, null, sharedState, options); 
    module.login(); 
    module.commit();  


// now create a user account: 
    Subject.doAs(svc.getSubject(), new PrivilegedExceptionAction<Object>() { 

      @Override 
      public Object run() throws Exception { 

       try { 
        String password = "\"Password1\""; 
        final Hashtable<String, String> env = svc.getEnvironment(); 
        env.put(Context.SECURITY_AUTHENTICATION, "GSSAPI"); 
        LdapContext ctxt = new InitialLdapContext(env, new Control[0]); 
        ModificationItem[] mods = new ModificationItem[1]; 
        mods[0] = new  
        ModificationItem(DirContext.REPLACE_ATTRIBUTE, new    
        BasicAttribute("userPassword", password.getBytes("UTF-16LE"))); 
ModificationItem(DirContext.REPLACE_ATTRIBUTE, new  BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWORD_EXPIRED))); 
        ctxt.modifyAttributes(dn, mods); 
       } 
       catch (NamingException e) { 
        System.out.println("Failed to set password."); 
        e.printStackTrace(); 
       } 
       return null; 
      } 
     }); 

現在您可以像平常一樣更改其他設置。

希望有所幫助。

Jim

相關問題