使用java获取ldap属性PwdLastSet

时间:2016-07-08 23:00:14

标签: java ldap

我正在尝试使用java从LDAP检索PwdLastSet属性。它失败了,并没有抛出错误。这是代码:

    private String getPasswordLastSet() {
        int searchScope = LDAPConnection.SCOPE_BASE;
        int ldapVersion  = LDAPConnection.LDAP_V3;
        int ldapPort = 389;
        String ldapHost = "Adapps.domain.mycompany.com";
        String loginDN  = "cn=myNTusername,OU=users,OU=colorado,OU=corporate,dc=domain,dc=mycompany,dc=com";
        String password = "myNTpassword";
        String baseDn = "dc=mycompany,dc=com";

        LDAPConnection lc = new LDAPConnection();

        String attributes[] = {"PwdLastSet"};
        String pwdLastSet = null;

        try {
            lc.connect( ldapHost, ldapPort );
            lc.bind( ldapVersion, loginDN, password.getBytes("UTF8") );
            String filter = "(sAMAccountName=myNtusername)";
            LDAPSearchResults searchResults =
                    lc.search(  baseDn,
                                searchScope,
                                filter,
                                attributes,         
                                true);       // return attributes and values

            while ( searchResults.hasMore()) {
                LDAPEntry nextEntry = null;                 
                try {
                    actionlogger.debug("about to searchResults.next...");
                    nextEntry = searchResults.next();
                    actionlogger.debug("about to nextEntry.getAttribute...");
                    LDAPAttribute pwdLastSetAttribute = nextEntry.getAttribute("PwdLastSet");
                    pwdLastSet = pwdLastSetAttribute.getStringValue();
                } catch(LDAPException e) {
                    e.printStackTrace();
                } catch(Exception e){
                    e.printStackTrace();
                }
            }
        } catch( LDAPException e ) {
            actionlogger.error( "Error occured while LDAP Search : " +  e.getMessage(),e );
        } catch (Exception e) {
            e.printStackTrace();
        }

        return pwdLastSet;
    }

输出

  

即将搜索结果。下一页......

但是

  

about nextEntry.getAttribute ...

从未被击中。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

几乎是正确的。我只是

  1. 将searchScope更改为LDAPConnection.SCOPE_SUB;
  2. 更改了loginDN = DOMAIN \ MyNTusername;
  3. 更改了baseDN =“dc = mydomain,dc = mycompany,dc = com”;
  4. 在lc.search中将true更改为false。
  5. 不确定哪些更改会导致它开始工作。