go - 如何使用 gopkg.in/ldap.v3 ldap.NewAddRequest 函数添加多个 objectClass 属性?
问题描述
在我的应用程序中,我将默认配置参数保存在 toml 配置文件中。在我存储的配置文件中:
LdapUserDefaultObjectClass = '"inetOrgPerson","extensibleObject","posixAccount"'
但是,当我使用如下代码所示的变量时,用户添加事务失败并显示:
RESULT tag=105 err=21 text=objectClass: value #0 invalid per syntax
从视觉上看,从 toml 文件传递的参数和键入的字符串是相同的。是否需要对 objectClass 对象进行硬编码?在我见过的几个例子中,没有一个使用 objectClass 的变量,但确实使用了其他属性的变量。
无法添加用户的代码(不起作用)
userAdd := ldap.NewAddRequest("uid=tester007@example,ou=users,dc=example", nil)
userAdd.Attribute("objectClass", []string{ldapClient.LDAP.LdapUserDefaultObjectClas})
// bunch of other attributes that satisfy the object classes below this line
// ...
ldapConn.Add(userAdd)
返回错误:
RESULT tag=105 err=21 text=objectClass: value #0 invalid per syntax
成功添加用户的代码(有效!)
userAdd := ldap.NewAddRequest("uid=tester007@example,ou=users,dc=example", nil)
userAdd.Attribute("objectClass", []string {"inetOrgPerson","extensibleObject","posixAccount"})
/// bunch of other attributes that satisfy the object classes below this line
...
ldapConn.Add(userAdd)
进一步调试后,我可以看到利用 TOML 变量的代码的解析方式与我硬编码文字时的方式不同。我不完全理解这里发生了什么以及为什么它们被更改/翻译和转义。我确实明白这不是 TOML 问题,而是将字符串馈送到 userAdd.Attribute("objectClass", []string {}) 时发生的事情。
我是 Go 新手,我觉得这太糟糕了!类型问题,但我尝试以多种方式转换和引用内容,结果总是错误的......
使用 TOML 变量时调试失败输出
Add Request: (Application, Constructed, 0x08) Len=368 "<nil>"
DN: (Universal, Primitive, Octet String) Len=39 "uid=tester007@example,ou=users,dc=example"
Attributes: (Universal, Constructed, Sequence and Sequence of) Len=323 "<nil>"
Attribute: (Universal, Constructed, Sequence and Sequence of) Len=66 "<nil>"
Type: (Universal, Primitive, Octet String) Len=11 "objectClass"
AttributeValue: (Universal, Constructed, Set and Set OF) Len=51 "<nil>"
Vals: (Universal, Primitive, Octet String) Len=49 "\"inetOrgPerson\",\"extensibleObject\",\"posixAccount\""
显示成功添加的调试输出。这是值被硬编码的时候。
Add Request: (Application, Constructed, 0x08) Len=364 "<nil>"
DN: (Universal, Primitive, Octet String) Len=39 "uid=tester007@example,ou=users,dc=example"
Attributes: (Universal, Constructed, Sequence and Sequence of) Len=319 "<nil>"
Attribute: (Universal, Constructed, Sequence and Sequence of) Len=62 "<nil>"
Type: (Universal, Primitive, Octet String) Len=11 "objectClass"
AttributeValue: (Universal, Constructed, Set and Set OF) Len=47 "<nil>"
Vals: (Universal, Primitive, Octet String) Len=13 "inetOrgPerson"
Vals: (Universal, Primitive, Octet String) Len=16 "extensibleObject"
Vals: (Universal, Primitive, Octet String) Len=12 "posixAccount"
解决方案
我想我想通了(n00b)。
TOML 定义很好。问题在于我如何尝试填充 objectClass 属性。我试图将 a 传递给string
([]string
字符串结构?)。我不明白的是我可以将我自己的传递[]string
给函数。
基本上我不明白[]string{}
可以用我自己的[]string
变量替换:
错误的:
userAdd.Attribute("objectClass", []string{ldapClient.LDAP.LdapUserDefaultObjectClass})
正确的:
- 将 ldapClient.LDAP.LdapUserDefaultObjectClass 分配给 objClass - 不需要很好
objClass := ldapClient.LDAP.LdapUserDefaultObjectClass
- 将 objClass 拆分为切片
userObjectClasses := (strings.Split(objClass, ","))
- 使用 userObjectClasses 传递 AttVals。
userAdd.Attribute("objectClass", userObjectClasses)
调试输出:
Add Request: (Application, Constructed, 0x08) Len=364 "<nil>"
DN: (Universal, Primitive, Octet String) Len=39 "uid=tester007@example,ou=users,dc=example"
Attributes: (Universal, Constructed, Sequence and Sequence of) Len=319 "<nil>"
Attribute: (Universal, Constructed, Sequence and Sequence of) Len=62 "<nil>"
Type: (Universal, Primitive, Octet String) Len=11 "objectClass"
AttributeValue: (Universal, Constructed, Set and Set OF) Len=47 "<nil>"
Vals: (Universal, Primitive, Octet String) Len=13 "inetOrgPerson"
Vals: (Universal, Primitive, Octet String) Len=16 "extensibleObject"
Vals: (Universal, Primitive, Octet String) Len=12 "posixAccount"
我希望这可以帮助其他人学习 Go 和 LDAP 包。
推荐阅读
- go - 结构内的结构切片/数组
- python - 我不能使用 subprocess.call() [AttributeError: module 'subprocess' has no attribute 'call']
- testing - Impala 中是否有空运行选项?
- firebase - 通过 /__/firebase/init.js 为多个 Firebase 项目自动配置 Vue 应用
- vue-test-utils - 如何在测试 vuejs 组件时触发“模糊”事件?
- mysql - 在事先不知道行的情况下,MySQL 中的行到列
- apache-spark - Spark Streaming 中的 groupby 理想策略
- java - Java TCP 客户端-服务器连接:始终打开以侦听服务器
- php - 在多维json数组中添加所有具有匹配键的值
- java - Kotlin 可以和 openjdk 一起使用吗?