亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使用 golang 綁定到帶有 TLS 的 LDAP 服務器?

如何使用 golang 綁定到帶有 TLS 的 LDAP 服務器?

Go
倚天杖 2022-07-11 16:57:03
我有以下代碼在綁定到沒有 TLS/SSL 的 LDAP 服務器時完美運行,但是當我嘗試綁定到具有 TLS 設置的 LDAP 服務器時,它不會綁定。/*In order to use this program, the user needs to get the package by running the following command:go get gopkg.in/ldap.v2*/package mainimport (  "fmt"  "strings"  "gopkg.in/ldap.v2"  "os")//Gives constants to be used for binding to and searching the LDAP server.const (  ldapServer = "ldaps://test.com:636"  ldapBind = "CN=ad_binder,CN=Users,DC=dev,DC=test,DC=com"  ldapPassword = "Password"  filterDN = "(objectclass=*)"  baseDN = "dc=dev,dc=test,dc=com"  loginUsername = "ad_binder"  loginPassword = "Password")//Main function, which is executed.func main() {  conn, err := connect()  //If there is an error connecting to server, prints this  if err != nil {    fmt.Printf("Failed to connect. %s", err)    return  }  //Close the connection at a later time.  defer conn.Close()  //Declares err to be list(conn), and checks if any errors. It prints the error(s) if there are any.  if err := list(conn); err != nil {    fmt.Printf("%v", err)    return  }    //Declares err to be auth(conn), and checks if any errors. It prints the error(s) if there are any.  if err := auth(conn); err != nil {    fmt.Printf("%v", err)    return  }}//This function is used to connect to the LDAP server.func connect() (*ldap.Conn, error) {  conn, err := ldap.Dial("tcp", ldapServer)  if err != nil {    return nil, fmt.Errorf("Failed to connect. %s", err)  }  if err := conn.Bind(ldapBind, ldapPassword); err != nil {    return nil, fmt.Errorf("Failed to bind. %s", err)  }  return conn, nil}//This function is used to search the LDAP server as well as output the attributes of the entries.func list(conn *ldap.Conn) error {  //This gets the command line argument and saves it in the form "(argument=*)"  arg := ""  filter := ""  if len(os.Args) > 1{    arg = os.Args[1]    fmt.Println(arg)這很奇怪,因為它可以與不同的 LDAP 服務器一起正常工作,但具有以下功能:ldapServer = "10.1.30.47:389"代替ldapServer = "ldaps://test.com:636"所以任何幫助將不勝感激,謝謝!
查看完整描述

1 回答

?
慕碼人8056858

TA貢獻1803條經驗 獲得超6個贊

gopkg.in/ldap.v2不支持 URI 尋址(即您不能將方案放在ldap://網絡ldaps://地址之前)。


注意: 通過DialURLgopkg.in/ldap.v3支持 URI 撥號 ( ) 。ldaps://test.com


如果您使用gopkg.in/ldap.v2,您仍然可以建立直接 TLS 連接,但您必須使用該功能DialTLS,并使用網絡地址(不是 URI):


// addr    = "10.1.30.47:389"

// tlsAddr = "10.1.30.47:636"


// conn, err = ldap.Dial("tcp", addr)           // non-TLS


// serverName = "test.com"   TLS cert name will be verified

// serverName = ""           TLS verify will be disabled (DON'T DO THIS IN PRODUCTION)

tlsConf, err := getTLSconfig(serverName)


if err != nil { /* */ }


conn, err = ldap.DialTLS("tcp", tlsAddr, tlsConf)

上面需要 a *tls.Config,所以使用一個輔助函數,如:


func getTLSconfig(tlsName string) (tlsC *tls.Config, err error) {

    if tlsName != "" {

        tlsC = &tls.Config{

            ServerName: tlsName,

        }

        return

    }


    log.Println("No TLS verification enabled! ***STRONGLY*** recommend adding a trust file to the config.")

    tlsC = &tls.Config{

        InsecureSkipVerify: true,

    }

    return

}


查看完整回答
反對 回復 2022-07-11
  • 1 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號