添加用户、修改ad密码

java 代码
 
  1. /**  
  2.  *   
  3.  */  
  4. package ldap;   
  5.   
  6. import java.util.Properties;   
  7.   
  8. import javax.naming.*;   
  9. import javax.naming.ldap.*;   
  10. import javax.naming.directory.*;   
  11.   
  12. /**  
  13.  * @author Keven Chen  
  14.  * @version $Revision 1.0 $  
  15.  *   
  16.  */  
  17. public class AddAdUser {   
  18.     private static final String SUN_JNDI_PROVIDER = "com.sun.jndi.ldap.LdapCtxFactory";   
  19.   
  20.     public static void main(String[] args) throws Exception {   
  21.         String keystore = "F:\\jdk1.5.0_08\\jre\\lib\\security\\cacerts";   
  22.         System.setProperty("javax.net.ssl.trustStore", keystore);   
  23.   
  24.         Properties env = new Properties();   
  25.   
  26.         env.put(Context.INITIAL_CONTEXT_FACTORY, SUN_JNDI_PROVIDER);// java.naming.factory.initial   
  27.         env.put(Context.PROVIDER_URL, "ldap://192.168.1.32:636");// java.naming.provider.url   
  28.         env.put(Context.SECURITY_AUTHENTICATION, "simple");// java.naming.security.authentication   
  29.         env.put(Context.SECURITY_PRINCIPAL,   
  30.                 "cn=Administrator,cn=Users,dc=comwave,dc=com");// java.naming.security.principal   
  31.         env.put(Context.SECURITY_CREDENTIALS, "password");// java.naming.security.credentials   
  32.         env.put(Context.SECURITY_PROTOCOL, "ssl");   
  33.   
  34.         String userName = "CN=test,CN=Users,DC=comwave,DC=com";   
  35.         String groupName = "CN=Domain Admins,CN=Users,DC=comwave,DC=com";   
  36.   
  37.         LdapContext ctx = new InitialLdapContext(env, null);   
  38.   
  39.         // Create attributes to be associated with the new user   
  40.         Attributes attrs = new BasicAttributes(true);   
  41.   
  42.         // These are the mandatory attributes for a user object   
  43.         // Note that Win2K3 will automagically create a random   
  44.         // samAccountName if it is not present. (Win2K does not)   
  45.         attrs.put("objectClass""user");   
  46.         attrs.put("sAMAccountName""test");   
  47.         attrs.put("cn""test");   
  48.   
  49.         // These are some optional (but useful) attributes   
  50.         attrs.put("sn""test");   
  51.         attrs.put("displayName""test");   
  52.         attrs.put("description""测试");   
  53.         attrs.put("userPrincipalName""test@comwave.com");   
  54.         attrs.put("mail""test@comwave.com");   
  55.         attrs.put("telephoneNumber""1234568999");   
  56.   
  57.         // some useful constants from lmaccess.h   
  58.         int UF_ACCOUNTDISABLE = 0x0002;   
  59.         int UF_PASSWD_NOTREQD = 0x0020;   
  60.         int UF_PASSWD_CANT_CHANGE = 0x0040;   
  61.         int UF_NORMAL_ACCOUNT = 0x0200;   
  62.         int UF_DONT_EXPIRE_PASSWD = 0x10000;   
  63.         int UF_PASSWORD_EXPIRED = 0x800000;   
  64.   
  65.         // Note that you need to create the user object before you can   
  66.         // set the password. Therefore as the user is created with no   
  67.         // password, user AccountControl must be set to the following   
  68.         // otherwise the Win2K3 password filter will return error 53   
  69.         // unwilling to perform.   
  70.   
  71.         attrs.put("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT   
  72.                 + UF_PASSWD_NOTREQD + UF_PASSWORD_EXPIRED + UF_ACCOUNTDISABLE));   
  73.   
  74.         // Create the context   
  75.         Context result = ctx.createSubcontext(userName, attrs);   
  76.         System.out.println("Created disabled account for: " + userName);   
  77.   
  78.         ModificationItem[] mods = new ModificationItem[2];   
  79.   
  80.         // Replace the "unicdodePwd" attribute with a new value   
  81.         // Password must be both Unicode and a quoted string   
  82.         String newQuotedPassword = "\"Password2000\"";   
  83.         byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");   
  84.   
  85.         mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,   
  86.                 new BasicAttribute("unicodePwd", newUnicodePassword));   
  87.         mods[1] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,   
  88.                 new BasicAttribute("userAccountControl", Integer   
  89.                         .toString(UF_NORMAL_ACCOUNT + UF_PASSWORD_EXPIRED)));   
  90.   
  91.         // Perform the update   
  92.         ctx.modifyAttributes(userName, mods);   
  93.         System.out.println("Set password & updated userccountControl");   
  94.         // now add the user to a group.   
  95.   
  96.         try {   
  97.             ModificationItem member[] = new ModificationItem[1];   
  98.             member[0] = new ModificationItem(DirContext.ADD_ATTRIBUTE,   
  99.                     new BasicAttribute("member", userName));   
  100.   
  101.             ctx.modifyAttributes(groupName, member);   
  102.             System.out.println("Added user to group: " + groupName);   
  103.   
  104.         } catch (NamingException e) {   
  105.             System.err.println("Problem adding user to group: " + e);   
  106.         }   
  107.         // Could have put tls.close() prior to the group modification   
  108.         // but it seems to screw up the connection or context ?   
  109.   
  110.         ctx.close();   
  111.   
  112.         System.out.println("Successfully created User: " + userName);   
  113.   
  114.     }   
  115.   
  116. }   
java 代码
 
  1. /**  
  2.  *   
  3.  */  
  4. package ldap;   
  5.   
  6. import java.io.IOException;   
  7. import java.io.UnsupportedEncodingException;   
  8. import java.util.Hashtable;   
  9.   
  10. import javax.naming.Context;   
  11. import javax.naming.NamingException;   
  12. import javax.naming.directory.BasicAttribute;   
  13. import javax.naming.directory.DirContext;   
  14. import javax.naming.directory.ModificationItem;   
  15. import javax.naming.ldap.InitialLdapContext;   
  16. import javax.naming.ldap.LdapContext;   
  17. import javax.naming.ldap.StartTlsRequest;   
  18. import javax.naming.ldap.StartTlsResponse;   
  19.   
  20. /**  
  21.  * @author Keven Chen  
  22.  * @version $Revision 1.0 $  
  23.  *  
  24.  */  
  25. public class UpdatePasswordTLS {   
  26.     public static void main (String[] args)   
  27.     {   
  28.        
  29.         Hashtable env = new Hashtable();   
  30.         String adminName = "CN=Administrator,CN=Users,DC=comwave,DC=com";   
  31.         String adminPassword = "aadsasdfasd";   
  32.         String userName = "CN=keven,CN=Users,DC=comwave,DC=com";   
  33.         String newPassword = "aaaaaaaa";   
  34.            
  35.         String keystore = "F:\\jdk1.5.0_08\\jre\\lib\\security\\cacerts";   
  36.         System.setProperty("javax.net.ssl.trustStore",keystore);   
  37.            
  38.         //Access the keystore, this is where the Root CA public key cert was installed   
  39.         //Could also do this via command line java -Djavax.net.ssl.trustStore....   
  40.         //String keystore = "/usr/java/jdk1.5.0_01/jre/lib/security/cacerts";   
  41.         //System.setProperty("javax.net.ssl.trustStore",keystore);   
  42.     
  43.         env.put(Context.INITIAL_CONTEXT_FACTORY,"com.sun.jndi.ldap.LdapCtxFactory");   
  44.     
  45.         //set security credentials, note using simple cleartext authentication   
  46.         env.put(Context.SECURITY_AUTHENTICATION,"simple");   
  47.         env.put(Context.SECURITY_PRINCIPAL,adminName);   
  48.         env.put(Context.SECURITY_CREDENTIALS,adminPassword);   
  49.         env.put(Context.SECURITY_PROTOCOL,"ssl");   
  50.     
  51.         //connect to my domain controller   
  52.         String ldapURL = "ldap://192.168.1.32:636";   
  53.         env.put(Context.PROVIDER_URL,ldapURL);   
  54.            
  55.         try {   
  56.     
  57. //           Create the initial directory context   
  58.             LdapContext ctx = new InitialLdapContext(env,null);   
  59.            
  60.             //set password is a ldap modfy operation   
  61.             ModificationItem[] mods = new ModificationItem[1];   
  62.     
  63.             //Replace the "unicdodePwd" attribute with a new value   
  64.             //Password must be both Unicode and a quoted string   
  65.             String newQuotedPassword = "\"" + newPassword + "\"";   
  66.             byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");   
  67.     
  68.             //注意:如果是当前用户自行修改密码,需要先删除oldpassword,然后在添加新的password   
  69.             /*  
  70.             ModificationItem[] mods = new ModificationItem[2];  
  71.             //Firstly delete the "unicdodePwd" attribute, using the old password  
  72.             //Then add the new password,Passwords must be both Unicode and a quoted string   
  73.             String oldQuotedPassword = "\"" + sOldPassword + "\"";  
  74.             byte[] oldUnicodePassword = oldQuotedPassword.getBytes("UTF-16LE");  
  75.             String newQuotedPassword = "\"" + sNewPassword + "\"";  
  76.             byte[] newUnicodePassword = newQuotedPassword.getBytes("UTF-16LE");  
  77.             mods[0] = new ModificationItem(DirContext.REMOVE_ATTRIBUTE, new BasicAttribute("unicodePwd", oldUnicodePassword));  
  78.             mods[1] = new ModificationItem(DirContext.ADD_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword));  
  79.             // Perform the update  
  80.             ctx.modifyAttributes(sUserName, mods);  
  81.             */  
  82.                
  83.             mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE, new BasicAttribute("unicodePwd", newUnicodePassword));   
  84.     
  85.             // Perform the update   
  86.             ctx.modifyAttributes(userName, mods);   
  87.            
  88.             System.out.println("Reset Password for: " + userName);     
  89.             ctx.close();   
  90.   
  91.     
  92.         }    
  93.         catch (NamingException e) {   
  94.             System.out.println("Problem resetting password: " + e);   
  95.         }   
  96.         catch (UnsupportedEncodingException e) {   
  97.             System.out.println("Problem encoding password: " + e);   
  98.         }   
  99.         catch (IOException e) {   
  100.             System.out.println("Problem with TLS: " + e);   
  101.         }   
  102.     
  103.     }   
  104.   
  105. }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值