我想创建一个具有accountExpires属性的AD用户。我这样做
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属性添加用户。有没有人可以帮助我。没有这一行
属性accountExpires = new BasicAttribute(" accountExpires",new Date());
一切顺利,但我也想要到期日。
答案 0 :(得分: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);
答案 1 :(得分: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)));