2

I have connected to Active Directory ,so that I was validate user name, but how can I know user is active or inactive in AD?

Sample Code:

            private Properties properties;
            private DirContext dirContext;
            private SearchControls searchCtls;
            private String[] returnAttributes = { "sAMAccountName", "givenName", "cn", "mail"};
            private String domainBase;
            private String baseFilter = "(&((&(objectCategory=Person)(objectClass=User)))";
    public ActiveDirectory(String username, String password, String domainController,String url) {
            properties = new Properties();        

            properties.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
            properties.put(Context.PROVIDER_URL, url);
            properties.put(Context.SECURITY_AUTHENTICATION,"simple");
            properties.put(Context.SECURITY_PRINCIPAL,username+"@"+domainController);
            properties.put(Context.SECURITY_CREDENTIALS, password);

            //initializing active directory LDAP connection
            try {

                dirContext = new InitialDirContext(properties);
                System.out.println("dirContext: "+dirContext);
            } catch (NamingException e) {
                e.printStackTrace();
                log.severe(e.getMessage());
            } catch (Exception e) {
                e.printStackTrace();
            }
               domainBase = getDomainBase(domainController);

            //initializing search controls
            searchCtls = new SearchControls();
            searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
            searchCtls.setReturningAttributes(returnAttributes);

        }



     public NamingEnumeration<SearchResult> searchUser(String searchValue, String searchBy, String searchBase) throws NamingException {
                System.out.println("search value :: "+searchValue);
                System.out.println("search base111 :: "+ ((null == searchBase) ? domainBase : getDomainBase(searchBase)));
                String filter = getFilter(searchValue, searchBy);       
                String base = (null == searchBase) ? domainBase : getDomainBase(searchBase); // for eg.: "DC=myjeeva,DC=com";
                System.out.println("this.dirContext : "+this.dirContext);



                return this.dirContext.search(base, filter, this.searchCtls);
            }
      private String getFilter(String searchValue, String searchBy) {
            String filter = this.baseFilter;        
            if(searchBy.equals("email")) {
                filter += "(mail=" + searchValue + "))";
            } else if(searchBy.equals("username")) {
                filter += "(samaccountname=" + searchValue + "))";
            }
            System.out.println("filter : "+filter);
            return filter;
        }

I have done search using this this.dirContext.search(base, filter, this.searchCtls);

How can I know user is active or inactive?

I found google to get active users (!(useraccountcontrol:1.2.840.113556.1.4.803:=2)) and deactive users (useraccountcontrol:1.2.840.113556.1.4.803:=2) How to Implements those two in JAVA code.

4

2 回答 2

2

1) 从 AD 中获取“userAccountControl”属性:

String s = (String) getAttribute(attrs, "userAccountControl");

2)将其解析为长值:

// 0 means not disabled (any value where value & 2 == 0 will do)
userAccountControl = StringUtils.isEmpty(s) ? 0 : Long.parseLong(s); 

3)如果用户被禁用,则返回:

public boolean isDisabled() {
    return ((userAccountControl & 2) != 0); 
}
于 2018-11-26T15:01:39.680 回答
1

谢谢,您的提示奏效了,我设法阅读了活跃和不活跃的用户。

我使用此过滤器成功地从 Active Directory 中读取了活动用户:

    (&(objectClass=user)(objectCategory=person)(!(useraccountcontrol:1.2.840.113556.1.4.803:=2)))

以及所有使用此过滤器的非活动用户:

    (&(objectClass=user)(objectCategory=person)(useraccountcontrol:1.2.840.113556.1.4.803:=2))
于 2013-09-12T14:45:59.487 回答