使用自己的ldif文件时出现ldap异常

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

昨天我问了一个问题,因为我试图访问ldap对象的cn属性。多亏了jzheaux,我想我现在走对了。但我遇到了另一个问题。当我尝试将自己的ldap路径放入ldif文件时,会出现以下异常:
原因:com.unboundd.ldap.sdk.ldapexception:服务器中已存在dn为“dc=paracus,dc=de”的条目。
我正在使用以下代码:
ldaptestapplication.java:

@SpringBootApplication
public class LdapTestApplication {
    public static void main(String[] args) {
        SpringApplication.run(LdapTestApplication.class, args);
    }
}

homeresource.java:

@RestController
public class HomeResource {
    @GetMapping("/")
    public String index(Authentication authentication, Principal principal) {
        // Access of CN property
        Person person = (Person) authentication.getPrincipal();
        String[] cn = person.getCn();
        return "Home page of " + cn[cn.length - 1] + " alias " + principal.getName();
//        return "home";
    }
}

springsecurityconfig.java—确定此人的部分来自jzheaux(感谢您帮助我解决了最初的问题):

@Configuration
@EnableWebSecurity
public class SpringSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .anyRequest().fullyAuthenticated()
            .and()
            .formLogin();
    }

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

    //
    // configuration for access of CN property
    // start
    //
    @Bean
    LdapAuthenticationProvider ldap(LdapAuthenticator authenticator) {
        LdapAuthenticationProvider ldap = new LdapAuthenticationProvider(authenticator);
        ldap.setUserDetailsContextMapper(new PersonContextMapper());
        return ldap;
    }

    @Bean
    UnboundIdContainer ldapContainer() {
        UnboundIdContainer container = new UnboundIdContainer("dc=parascus,dc=de", "classpath:ldap-data.ldif");
        container.setPort(0);
        return container;
    }

    @Bean
    ContextSource contextSource(UnboundIdContainer container) {
        int port = container.getPort();
        return new DefaultSpringSecurityContextSource("ldap://localhost:" + port + "/dc=parascus,dc=de");
    }

    @Bean
    BindAuthenticator authenticator(BaseLdapPathContextSource contextSource) {
        BindAuthenticator authenticator = new BindAuthenticator(contextSource);
        authenticator.setUserDnPatterns(new String[] { "uid={0},ou=people" });
        return authenticator;
    }
    //
    // configuration for access of CN property
    // end
    //
}

application.properties:

spring.ldap.embedded.port=8389
spring.ldap.embedded.ldif=classpath:ldap-data.ldif
spring.ldap.embedded.base-dn=dc=parascus,dc=de

ldap-data.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信息的行时,我可以登录到页面并在浏览器中获取文本“home”。我想我错过了一点细节,或者我迷失在这个概念中。。。一切皆有可能。我希望有人能给我一个提示,原因是什么。
顺便说一句,当我删除dc=parascus,dc=de的部分,希望它现在可以添加这个条目一次时,我得到了以下异常:
原因:com.unboundd.ldap.sdk.ldapexception:无法添加条目“ou=groups,dc=paracus,dc=de”,因为其父条目“dc=paracus,dc=de”在服务器中不存在。
可能是我的程序两次尝试添加我的ldif文件,第二次检测到重复。但是我怎么能告诉我的程序只添加一次呢?
问候
副肌

vlf7wbxs

vlf7wbxs1#

好的,正如我上面所描述的,现在它开始工作了。问题是我通过application.configuration文件建立了ldap树。之后,我的配置尝试构建一个引用同一文件的新defaultspringsecuritycontextsource。因此,它试图再次插入相同的条目,这导致了麻烦。
谢谢你的努力。

相关问题