3 回答

TA貢獻(xiàn)1936條經(jīng)驗 獲得超7個贊
好的,讓我們嘗試使用github.com/go-ldap/ldap. 首先,您需要創(chuàng)建一個*ldap.Conn. 如果您的 LDAP 服務(wù)器支持,我建議使用 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")
現(xiàn)在您應(yīng)該有一個到 LDAP 服務(wù)器的活動連接。使用此連接,您必須執(zhí)行綁定:
err := l.Bind("user@test.com", "password")
if err != nil {
// error in ldap bind
log.Println(err)
}
// successful bind

TA貢獻(xiàn)2019條經(jīng)驗 獲得超9個贊
問題是默認(rèn)情況下,上述 LDAP 客戶端中未設(shè)置“SkipTLS”標(biāo)志。您需要將參數(shù)顯式設(shè)置為 false:
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)",
SkipTLS: true,
Attributes: []string{"givenName", "sn", "mail", "uid"},
}
我將 SkipTLS 設(shè)置為 true 并且它起作用了。希望這可以幫助!

TA貢獻(xiàn)1752條經(jīng)驗 獲得超4個贊
使用go-guardian LDAP 策略的另一種可能的解決方案
基于在線 LDAP 測試服務(wù)器的工作示例
package main
import (
"fmt"
"github.com/shaj13/go-guardian/auth/strategies/ldap"
"net/http"
)
func main() {
cfg := ldap.Config{
BaseDN: "dc=example,dc=com",
BindDN: "cn=read-only-admin,dc=example,dc=com",
Port: "389",
Host: "ldap.forumsys.com",
BindPassword: "password",
Filter: "(uid=%s)",
}
r, _ := http.NewRequest("GET", "/", nil)
r.SetBasicAuth("tesla", "password")
user, err := ldap.New(&cfg).Authenticate(r.Context(), r)
fmt.Println(user, err)
}
- 3 回答
- 0 關(guān)注
- 399 瀏覽
添加回答
舉報