java - LDAP:在身份验证事件期间获取自定义值

标签 java spring-boot spring-security ldap spring-ldap

我已经让 Springboot 成功通过 LDAP 进行身份验证,并将 CustomUserDetails 模型添加到 View 中。

在身份验证 Activity 期间,我还想带回其他详细信息,例如电子邮件、电话和城市。

根据这个SO answer这是可能的,我认为this也解决了这个问题。

尽管阅读了这两个答案,但我仍然不确定如何继续执行此操作 - 特别是关于第二个答案中的步骤 2 和 3 之间的关系,以及电子邮件、城市和电话等详细信息的映射位置?

我的安全等级:

安全

@SpringBootApplication
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Override
public void configure(HttpSecurity http) throws Exception {
    http
            .authorizeRequests()
                .anyRequest().fullyAuthenticated()
                .and()
            .formLogin()
                .defaultSuccessUrl("/newExhibit");
}

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
            .userDetailsService(userDetailsService)
            .and()
            .ldapAuthentication()
            .userDnPatterns("uid={0},ou=people")
            .groupSearchBase("ou=groups")
            .contextSource()
            .url("ldap://localhost:8389/dc=springframework,dc=org")
            .and()
            .passwordCompare()
            .passwordEncoder(passwordEncoder())
            .passwordAttribute("userPassword");
}

来自 test-server.ldif 的我的用户对象

dn: uid=abc123,ou=people,dc=springframework,dc=org
objectclass: top
objectclass: person
objectclass: organizationalPerson
objectclass: inetOrgPerson
cn: Tom Smith
sn: Smith
uid: abc123
userPassword: pass
mail: tom@contoso.com
mobile: +212 307 12345
st: CA

已关注 this answer我需要添加到身份验证
(a) 上下文源和
(b) userDetailsContextMapper?

最终,我想将这些来自 ldap 的详细信息映射到我的 java 用户类。

更新

更新的 AuthenticationManagerBuilder

@SpringBootApplication
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {

@Autowired
private UserDetailsService userDetailsService;

@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
        .userDetailsService(userDetailsService)
        .and()
        .ldapAuthentication()
        .userDnPatterns("uid={0},ou=people")
        .groupSearchBase("ou=groups")
        .contextSource()
        .userDetailsContextMapper(???) // what object in here and how initalised
        .url("ldap://localhost:8389/dc=springframework,dc=org")
        .and()
        .passwordCompare()
        .passwordEncoder(passwordEncoder())
        .passwordAttribute("userPassword");
}

最佳答案

查看您的 LDIF 文件,我看起来您正在尝试获取定义为 inetOrgPerson 的一部分的属性。 。 Spring Security 为此提供了一个开箱即用的映射器 InetOrgPersonContextMapper 。这会将定义的属性映射到 InetOrgPerson,它将充当所需的 UserDetails

要进行配置,您只需创建一个新实例并将其连接到您的配置。

@SpringBootApplication
public class ApplicationSecurity extends WebSecurityConfigurerAdapter {


@Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth
      .ldapAuthentication()
        .userDnPatterns("uid={0},ou=people")
        .groupSearchBase("ou=groups")
        .contextSource()
        .url("ldap://localhost:8389/dc=springframework,dc=org")
        .and()
        .userDetailsContextMapper(new InetOrgPersonContextMapper())
        .passwordCompare()
        .passwordEncoder(passwordEncoder())
        .passwordAttribute("userPassword");
}

这将相应地映射属性。由于您使用的是 LDAP,因此不需要注入(inject) UserDetailsS​​ervice,因为它会自动在下面配置 LdapUserDetailsS​​ervice

关于java - LDAP:在身份验证事件期间获取自定义值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56323625/

相关文章:

maven - ProGuard + Spring Boot + Maven 插件

java - Grails Spring 安全认证提供程序和自定义过滤器

spring - 如何强制 Spring Boot 重定向到 https 而不是 http?

java - 如何在不同计算机上且不在同一 WiFi 上的两个 Java 应用程序之间进行通信

java - 将 Nutch 网络爬虫功能集成到 Java 应用程序中

java - 如何在 Android Room 中写一个到(两个嵌入式实体的许多 pojo)

java - Spring登录后如何重定向到请求的页面?

java - 将struts web项目升级到java 1.7

java - Vavr 对象的序列化器/反序列化器

java - 注入(inject) JpaRepository : Error creating bean with name