简体   繁体   English

如何从java创建具有accountExpires属性的活动目录用户

[英]how to create active directory users with accountExpires attribute from java

I want to create an AD user with accountExpires attribute.我想创建一个具有 accountExpires 属性的 AD 用户。 I am doing like this我这样做

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;
            }
        }
    }

How can I add user with accountExpires attribute.如何添加具有 accountExpires 属性的用户。 Is there anybody can help me.有没有人可以帮助我。 Without this line没有这条线

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

Everything goes fine but I want the expiry date as well.一切顺利,但我也想要到期日。

You are setting the attribute to the current date, but this is not correct.您正在将该属性设置为当前日期,但这不正确。

First of all because the attribute is an interval of 100-nanoseconds, according to theMicrosoft documentation andthis .首先,根据Microsoft 文档.

What you have to do is set your desired expiration date, then convert to the value of 100-nanoseconds, that in Java are represented by long.您需要做的是设置您想要的到期日期,然后转换为 100 纳秒的值,在 Java 中用 long 表示。

Here is a trivial code example that show you how to do it with Java 8:这是一个简单的代码示例,向您展示如何使用 Java 8 执行此操作:

UPDATED:更新:

    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);

Thanks for your help what gave me the idea and this code worked for me感谢您的帮助给了我这个想法,这段代码对我有用

/**
     * 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)));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM