LdapAuthUtil.java
1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package com.ui.ldaputil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.AttributesMapper;
import org.springframework.ldap.core.LdapTemplate;
import org.springframework.ldap.filter.AndFilter;
import org.springframework.ldap.filter.EqualsFilter;
import org.springframework.stereotype.Component;
import javax.naming.directory.SearchControls;
import java.util.List;
/**
* Created by jimi on 2017/12/26.
*/
@Component
public class LdapAuthUtil {
private static final Logger logger= LoggerFactory.getLogger(LdapAuthUtil.class);
@Autowired
LdapTemplate ldapTemplate;
public boolean login(String userName, String passWord){
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person"))
.and(new EqualsFilter("sAMAccountName", userName));
String[] urls = ((YhLdapContextSource) ldapTemplate.getContextSource()).getUrls();
logger.info("ldap urls :{}",urls);
return ldapTemplate.authenticate("", filter.toString(), passWord);
}
public LdapUser getUser(String userName){
AndFilter filter = new AndFilter();
filter.and(new EqualsFilter("objectclass", "person"))
.and(new EqualsFilter("sAMAccountName", userName));
LdapUser user = null;
int SearchScope = SearchControls.SUBTREE_SCOPE;
AttributesMapper<LdapUser> attr = new LdapMapUser();
List<LdapUser> users=ldapTemplate.search("",filter.toString(), SearchScope, attr);
if (users.size() !=0){
user=users.get(0);
}
return user;
}
}