1.下载Go语言操作LDAP的第三方库gopkg.in/ldap
go get github.com/go-ldap/ldap/v3
2.连接LDAP
l, err := ldap.DialURL("ldap://localhost:389")
if err != nil {
log.Fatal(err)
} else {
log.Printf("Connect LDAP Server succeeded")
}
defer l.Close()
3.身份认证
在这里插入代码片
_,err=l.SimpleBind(&ldap.SimpleBindRequest{
Username: "cn=admin,dc=example,dc=com",
Password: "123456",
})
if err!=nil {
log.Fatal("Failed to bind: %s\n", err)
} else {
log.Printf("Login succeeded")
}
4.执行查询
searchRequest := ldap.NewSearchRequest(
"cn=admin,dc=example,dc=com",
ldap.ScopeWholeSubtree,
ldap.NeverDerefAliases,
0,
0,
false,
"(objectClass=*)",
[]string{"DN", "cn","dc","ou","objectClasss"},
nil,
)
sr, err := l.Search(searchRequest)
if err != nil {
log.Fatal(err)
}
for index,entry:=range sr.Entries{
fmt.Println(index,entry.DN)
}