gpt4 book ai didi

Golang 与 LDAP 通信

转载 作者:IT王子 更新时间:2023-10-29 01:12:56 25 4
gpt4 key购买 nike

我正在尝试使用 golang 将用户连接到 ldap 并对其进行身份验证。

我正在使用 go-ldap-client使用以下示例代码:

package main

import (
"log"
"github.com/jtblin/go-ldap-client"
)

func main() {
client := &ldap.LDAPClient{
Base: "dc=example,dc=com",
Host: "ldap.example.com",
Port: 389,
UseSSL: false,
BindDN: "uid=readonlysuer,ou=People,dc=example,dc=com",
BindPassword: "readonlypassword",
UserFilter: "(uid=%s)",
GroupFilter: "(memberUid=%s)",
Attributes: []string{"givenName", "sn", "mail", "uid"},
}
# It is the responsibility of the caller to close the connection
defer client.Close()

ok, user, err := client.Authenticate("username", "password")
if err != nil {
log.Fatalf("Error authenticating user %s: %+v", "username", err)
}
if !ok {
log.Fatalf("Authenticating failed for user %s", "username")
}
log.Printf("User: %+v", user)

groups, err := client.GetGroupsOfUser("username")
if err != nil {
log.Fatalf("Error getting groups for user %s: %+v", "username", err)
}
log.Printf("Groups: %+v", groups)
}

已安装对 gopkg.in/ldap.v2 的依赖。

问题是我收到以下错误:

2016/01/15 17:34:55 Error authenticating user username: LDAP Result Code 2 "Protocol Error": ldap: cannot StartTLS (unsupported extended operation)
exit status 1

关于这个错误有什么提示吗?

最佳答案

好的,让我们尝试使用 github.com/go-ldap/ldap 进行身份验证。首先你需要创建一个*ldap.Conn。如果您的 LDAP 服务器支持,我建议使用 TLS:

// TLS, for testing purposes disable certificate verification, check https://golang.org/pkg/crypto/tls/#Config for further information.
tlsConfig := &tls.Config{InsecureSkipVerify: true}
l, err := ldap.DialTLS("tcp", "ldap.example.com:636", tlsConfig)

// No TLS, not recommended
l, err := ldap.Dial("tcp", "ldap.example.com:389")

现在您应该与 LDAP 服务器建立了事件连接。使用此连接,您必须执行绑定(bind):

err := l.Bind("user@test.com", "password")
if err != nil {
// error in ldap bind
log.Println(err)
}
// successful bind

关于Golang 与 LDAP 通信,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34814834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com