authentication - 获取经过身份验证的 ldap 用户的 CN 条目
问题描述
我正在使用 Spring Boot 在 LDAP 上迈出第一步。我已经设法验证了我的 ldif 文件中列出的用户:
dn: dc=parascus,dc=de
objectclass: top
objectclass: domain
objectclass: extensibleObject
dc: parascus
dn: ou=groups,dc=parascus,dc=de
objectclass: top
objectclass: organizationalUnit
ou: groups
dn: ou=people,dc=parascus,dc=de
objectclass: top
objectclass: organizationalUnit
ou: people
dn: uid=jsmith,ou=people,dc=parascus,dc=de
objectclass: top
objectclass: person
objectclass: inetOrgPerson
cn: Smith, John
sn: Smith
uid: jsmith
userPassword: scrambled
dn: cn=developers,ou=groups,dc=parascus,dc=de
objectclass: top
objectclass: groupOfUniqueNames
cn: developers
ou: developer
uniqueMember: uid=jsmith,ou=people,dc=parascus,dc=de
现在我在我的控制器方法中并尝试获取 cn 属性“Smith, John”:
@GetMapping("/profile")
public String index(Authentication authentication) {
return "Profile of " + authentication.getName();
}
但我只得到 uid“jsmith”。有人可以提示我如何获取所有信息或最后的 cn 条目吗?
亲切的问候
帕拉斯库斯
解决方案
您将需要提供一个UserDetailsContextMapper
告诉 Spring Security 如何从DirContext
.
你在暴露LdapAuthenticationProvider
时这样做:
@Bean
LdapAuthenticationProvider ldap(LdapAuthenticator authenticator) {
LdapAuthenticationProvider ldap = new LdapAuthenticationProvider(authenticator);
ldap.setUserDetailsContextMapper(new PersonContextMapper());
return ldap;
}
Spring Security 附带了几个内置的上下文映射器,一个用于person
模式 ( PersonContextMapper
),另一个用于inetOrgPerson
模式 ( InetOrgPersonContextMapper
)。
使用上述配置,您可以执行以下任一操作:
public String index(Authentication authentication) {
Person person = (Person) authentication.getPrincipal();
String[] cn = person.getCn();
return "Profile of " + cn[cn.length - 1];
}
或者
public String index(@AuthenticationPrincipal Person person) {
String[] cn = person.getCn();
return "Profile of " + cn[cn.length - 1];
}
如果您的条目既不使用person
也不使用inetOrgPerson
架构,您可以创建自己的UserDetailsContextMapper
.
推荐阅读
- pdo - Eclipse/PHP:PDO 无法解析为类型
- python-3.x - 将 UTC 转换为 CST
- sql - UserStoredProcedure 的代码有问题吗?
- javascript - Laravel Mix 更新后无法在 Vue 中将父组件的对象作为道具传递
- java - SQLite 文件打开器 Android Studio
- cmake - Ubuntu 18.04 上的 val3dity 安装问题 - 找不到 GMP(缺少:GMP_LIBRARIES GMP_INCLUDE_DIR)
- php - 将文件夹指向其父文件夹之外的文件夹或仅包含文件夹 PHP
- c# - 在 C# 中从 SQL Server 数据库加载大量数据
- python - 等到使用 boto3 和 lambda 完成快照
- php - php拆分pdf文件