2016-10-13 11 views
0

accountExpires属性を使用してADユーザーを作成したいとします。私はaccountExpires属性を持つユーザーを追加するにはどうすればよい javaからaccountExpires属性を持つアクティブなディレクトリユーザーを作成する方法

このよう
public boolean addUser(
      String firstName, 
      String lastName, 
      String userName, 
      String password, 
      String organisationUnit) throws NamingException { 
     if (findUser(userName, firstName, lastName, organisationUnit)) { 
      return false; 
     } else { 
      // Create a container set of attributes 
      BasicAttributes container = new BasicAttributes(); 

      // Create the objectclass to add 
      Attribute objClasses = new BasicAttribute("objectClass"); 
      objClasses.add("top"); 
      objClasses.add("person"); 
      objClasses.add("organizationalPerson"); 
      objClasses.add("user"); 

      // Assign the username, first name, and last name 
      String cnValue = new StringBuffer(firstName).append(" ").append(lastName).toString(); 
      Attribute cn = new BasicAttribute("cn", cnValue); 
      Attribute sAMAccountName = new BasicAttribute("sAMAccountName", userName); 
      Attribute mac = new BasicAttribute("msNPCallingStationID", "ab-ab-ab-b7-6t"); 
      Attribute principalName = new BasicAttribute("userPrincipalName", userName + "@atamunet.com"); 
      Attribute givenName = new BasicAttribute("givenName", firstName); 
      Attribute sn = new BasicAttribute("sn", lastName); 
      Attribute uid = new BasicAttribute("uid", userName); 
      Attribute fullName = new BasicAttribute("displayName", "fullName"); 
      Attribute gender = new BasicAttribute("initials", "gender"); 
      Attribute dob = new BasicAttribute("description", "dob"); 
      Attribute FatherName = new BasicAttribute("physicalDeliveryOfficeName", "FatherName"); 
      Attribute Email = new BasicAttribute("mail", "Email"); 
      Attribute mobile = new BasicAttribute("mobile", "mobile"); 
      Attribute department = new BasicAttribute("department", "department"); 
      Attribute HallName = new BasicAttribute("streetAddress", "HallName"); 
      Attribute FacultyName = new BasicAttribute("company", "FacultyName"); 
      Attribute CourseName = new BasicAttribute("title", "CourseName"); 

      Attribute accountExpires = new BasicAttribute("accountExpires", new Date()); 

      //some useful constants from lmaccess.h 
      int UF_ACCOUNTENABLE = 0x0001; 
      //int UF_ACCOUNTDISABLE = 0x0002; 
      int UF_PASSWD_NOTREQD = 0x0020; 
      int UF_PASSWD_CANT_CHANGE = 0x0040; 
      int UF_NORMAL_ACCOUNT = 0x0200; 
      int UF_DONT_EXPIRE_PASSWD = 0x10000; 
      //int UF_PASSWORD_EXPIRED = 0x800000; 

      Attribute enabled = new BasicAttribute("userAccountControl", Integer.toString(UF_NORMAL_ACCOUNT + UF_PASSWD_NOTREQD + UF_DONT_EXPIRE_PASSWD + UF_ACCOUNTENABLE)); 
      // Add password 
      Attribute userPassword = new BasicAttribute("userpassword", password); 

      // Add these to the container 
      container.put(objClasses); 
      container.put(sAMAccountName); 
      container.put(principalName); 
      container.put(cn); 
      container.put(sn); 
      container.put(givenName); 
      container.put(uid); 
      container.put(userPassword); 
      container.put(mac); 
      container.put(gender); 
      container.put(dob); 
      container.put(FatherName); 
      container.put(Email); 
      container.put(mobile); 
      container.put(department); 
      container.put(HallName); 
      container.put(FacultyName); 
      container.put(CourseName); 
      container.put(fullName); 
      container.put(enabled); 
      container.put(accountExpires); 

      // Create the entry 
      try { 
       ctx.createSubcontext(getUserDN(cnValue, organisationUnit), container); 
       return true; 
      } catch (Exception e) { 
       System.out.println(e.getMessage() + "add"); 
       return false; 
      } 
     } 
    } 

をしています。誰かが私を助けることができるか?この行がなければ

属性accountExpires = new BasicAttribute( "accountExpires"、new Date());

すべてがうまくいっていますが、有効期限も欲しいです。

+0

あなたの問題はここでは分かりませんが、より良く説明できますか? –

+0

アクティブなディレクトリユーザーを追加します。上記のコードは私のために働いていますが、私はユーザーのアカウント有効期限を提供したいと思います。では、javaのaccountExpires属性の使い方を教えてください。アカウントの有効期限を持つユーザーを作成しようとしています。 –

+0

* Account-Expires *属性は、ドキュメントではなく、* Interval *の日付です。https://msdn.microsoft.com/en-us/library/ms675098(v=vs.85).aspx –

答えて

0

私のアイデアを与え、このコードは、のために働いていたものを、あなたの助けをありがとう私

/** 
    * Difference between Filetime epoch and Unix epoch (in ms). 
    */ 
    private static final long FILETIME_EPOCH_DIFF = 11644473600000L; 

    /** 
    * One millisecond expressed in units of 100s of nanoseconds. 
    */ 
    private static final long FILETIME_ONE_MILLISECOND = 10 * 1000; 

    public static long filetimeToMillis(final long filetime) { 
     return (filetime/FILETIME_ONE_MILLISECOND) - FILETIME_EPOCH_DIFF; 
    } 

    public static long millisToFiletime(final long millis) { 
     return (millis + FILETIME_EPOCH_DIFF) * FILETIME_ONE_MILLISECOND; 
    } 

SimpleDateFormat sdf = new SimpleDateFormat("dd-M-yyyy hh:mm:ss"); 
      String dateInString = "01-07-2017 10:20:56"; 
      Date date = sdf.parse(dateInString); 
      final long dd = date.getTime(); 
      Attribute accountExpires = new BasicAttribute("accountExpires", Long.toString(millisToFiletime(dd))); 
0

属性を現在の日付に設定していますが、これは正しくありません。

まず、属性は、Microsoft documentationおよびthisに従って、100ナノ秒の間隔であるためです。

あなたがしなければならないことは、希望する有効期限を設定して、100ナノ秒の値に変換することです。Javaの場合はlongで表されます。ここで

は、Java 8でそれを行う方法をお見せ些細なコードの例です:

更新日:

Calendar cal = Calendar.getInstance(); 

    // First you need to get the start date 
    // according to the documentation it is 
    // the 1th January of 1601 
    cal.set(1601, Calendar.JANUARY, 1); 

    // print the current date 
    System.out.println(String.format("Start date: %tc", cal)); 

    Date startDate = cal.getTime(); 

    // Reset the calendar to today 
    cal.set(Calendar.MILLISECOND, System.currentTimeMillis()); 

    // Set the desired expiration date 
    // here is 1 year in the future 
    cal.set(Calendar.YEAR, cal.get(Calendar.YEAR) + 1); 

    // print the current date 
    System.out.println(String.format("Expire date: %tc", cal)); 

    // Get the date from Calendar 
    Date expireDate = cal.getTime(); 

    // Create an interval from the startDate to the expireDate 
    // and convert it to nanoseconds interval 
    long expirationInterval = TimeUnit.NANOSECONDS.toNanos(expireDate.getTime()-startDate.getTime()); 

    // set the attribute value 
    Attribute accountExpires = new BasicAttribute("accountExpires", expirationInterval); 
+0

これはエラーです: 'Expire date:Sat Oct 14 09:31:51 IST 2017 'accountExpires'属性値が正しくありません。 –

+0

@MohammadAmir申し訳ありません、開始日のdiffを忘れました。私の更新を見てください。 –

関連する問題