获取经过身份验证的ldap用户的cn条目

pod7payv  于 2021-09-30  发布在  Java
关注(0)|答案(1)|浏览(408)

我正在使用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条目?
问候
副肌

mm5n2pyu

mm5n2pyu1#

您需要提供一个 UserDetailsContextMapper 告诉spring security如何从 DirContext .
您在暴露对象时执行此操作 LdapAuthenticationProvider :

@Bean
LdapAuthenticationProvider ldap(LdapAuthenticator authenticator) {
    LdapAuthenticationProvider ldap = new LdapAuthenticationProvider(authenticator);
    ldap.setUserDetailsContextMapper(new PersonContextMapper());
    return ldap;
}

SpringSecurity附带了两个内置的上下文Map器,其中一个用于 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 .

相关问题