简体   繁体   English

Spring LDAP 3.1自定义用户映射器

[英]Spring LDAP 3.1 Custom User Mapper

I'm developing an application with Spring Security and Spring LDAP. 我正在使用Spring Security和Spring LDAP开发应用程序。 This is part of my spring-security.xml: 这是我的spring-security.xml的一部分:

<authentication-manager alias="authenticationManager">
    <ldap-authentication-provider
            user-search-filter="sAMAccountName={0}"
            user-search-base="OU=UK,OU=Domain Objects,dc=test,dc=test1"
            group-search-filter="member={0}"
            group-search-base="OU=_Groups,OU=UK,OU=Domain Objects,dc=test,dc=test1"
            group-role-attribute="cn"
            role-prefix="ROLE_">
    </ldap-authentication-provider>
</authentication-manager>

<ldap-server url="ldap://host:389/"
                      manager-dn="managerUser"
                      manager-password="ManagerPassword" />

Now I need to do some logic on a user attribute. 现在,我需要对用户属性进行一些逻辑处理。 I was wondering if there is a way to get that attribute during the login phase or I need to do a search on LDAP everytime I need that attribute. 我想知道是否有一种方法可以在登录阶段获取该属性,或者每次需要该属性时都需要在LDAP上进行搜索。

Looking online now I'm a bit confused because I found online many ways to get custom attributes: extending the ContextMapper in the DAO or the AbstractContextMapper or extending LdapUserDetailsMapper. 现在看网上,我有点困惑,因为我在网上找到了许多获取自定义属性的方法:在DAO或AbstractContextMapper中扩展ContextMapper或扩展LdapUserDetailsMapper。

Could you help me to find the correct solution? 您能帮我找到正确的解决方案吗? I think the best way would be to have an object where I can put the attribute I need during the login phase instead of querying the LDAP everytime I need that attribute. 我认为最好的方法是拥有一个可以在登录阶段放置所需属性的对象,而不是每次需要该属性时都查询LDAP。 Thanks 谢谢

I'm not sure exactly what you're asking. 我不确定您要问的是什么。 What I can tell you is that once you're authenticated via LDAP spring security caches the user details so you won't need to make a call to LDAP with every subsequent request. 我可以告诉您的是,一旦您通过LDAP spring安全性进行了身份验证,便会缓存用户详细信息,因此您无需在每个后续请求中都调用LDAP。

This means that any subsequent methods called after the user is logged in can get the user details like so: 这意味着在用户登录后调用的任何后续方法都可以获取用户详细信息,如下所示:

Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
UserDetails ldapUser = (UserDetails) authentication.getPrincipal();

Or if you prefer you can cast it to a custom class you've created as long as you implement UserDetails. 或者,如果您愿意,可以将其转换为您创建的自定义类,只要实现UserDetails。

public class MyUser implements UserDetails {
.....
}

Does this help at all? 这有帮助吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM