Skip to content

Commit f10d379

Browse files
committed
ldap tls support test
1 parent 88e8d36 commit f10d379

File tree

1 file changed

+45
-1
lines changed

1 file changed

+45
-1
lines changed

Diff for: main.go

+45-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"crypto/tls"
45
"fmt"
56
"github.com/go-ldap/ldap/v3"
67
"log"
@@ -10,6 +11,47 @@ import (
1011

1112
var DefaultTimeout = 5 * time.Second
1213

14+
func demo_tls(ldapURL string) {
15+
lc, err := ldap.DialURL(ldapURL, ldap.DialWithTLSDialer(&tls.Config{InsecureSkipVerify: true}, &net.Dialer{Timeout: DefaultTimeout}))
16+
if err != nil {
17+
log.Fatalf("DialURL => %v", err)
18+
return
19+
}
20+
defer lc.Close()
21+
22+
// filter := fmt.Sprintf("(&(objectClass=User)(%s=%s))", "mail", "[email protected]")
23+
filter := "(&(objectClass=User))" // 【5】 查询的筛选条件
24+
attributes := []string{"dn", "cn"} // 【6】 查询的包含属性
25+
// baseDn := "DC=example,DC=com"
26+
baseDn := "DC=example,DC=com" // 【2】 DC
27+
searchRequest := ldap.NewSearchRequest(
28+
baseDn, // The base dn to search
29+
ldap.ScopeWholeSubtree, ldap.NeverDerefAliases, 0, 0, false,
30+
filter, // The filter to apply
31+
attributes, // A list attributes to retrieve
32+
nil,
33+
)
34+
// account := "cn=admin,dc=example,dc=com"
35+
// account := "cn=administrator,dc=example,dc=com"
36+
account := "[email protected]" // 【3】 账号
37+
// password := "123456"
38+
password := "123+456=579" // 【4】 密码
39+
err = lc.Bind(account, password)
40+
if err != nil {
41+
log.Fatalf("Bind => %v", err)
42+
return
43+
}
44+
45+
sr, err := lc.Search(searchRequest)
46+
if err != nil {
47+
log.Fatalf("Search => %v", err)
48+
}
49+
50+
for _, entry := range sr.Entries {
51+
log.Printf("%s: %v\n", entry.DN, entry.GetAttributeValue("cn"))
52+
}
53+
}
54+
1355
func demo(ldapURL string) {
1456
lc, err := ldap.DialURL(ldapURL, ldap.DialWithDialer(&net.Dialer{Timeout: DefaultTimeout}))
1557
if err != nil {
@@ -55,6 +97,8 @@ func main() {
5597
log.Println("Debug go-ldap:")
5698

5799
ldapURL := fmt.Sprintf("ldap://%s:%d", "10.96.8.9", 389) // 【1】 地址
58-
59100
demo(ldapURL)
101+
102+
ldapsURL := fmt.Sprintf("ldaps://%s:%d", "10.96.8.9", 636) // 【1】 地址
103+
demo_tls(ldapsURL)
60104
}

0 commit comments

Comments
 (0)