spring-security - 使用自己的 ldif 文件时出现 LDAP 异常
问题描述
昨天我问了一个问题,因为我试图访问一个 ldap 对象的 CN 属性。感谢jzheaux,我认为我现在走对了。但是我遇到了另一个问题。一旦我尝试将自己的 ldap 路径放入 ldif 文件中,就会出现以下异常:
原因:com.unboundid.ldap.sdk.LDAPException:服务器中已存在 DN 'dc=parascus,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
//
}
应用程序属性:
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 信息的行时,我可以登录到页面并在浏览器中获取文本“主页”。我想我错过了一个小细节,或者我迷失在这个概念中......一切皆有可能。我希望有人能给我一个提示是什么原因。
顺便说一句,当我删除 dc=parascus,dc=de 的部分时,希望它现在会添加一次此条目,我得到以下异常:
原因:com.unboundid.ldap.sdk.LDAPException:无法添加条目 'ou=groups,dc=parascus,dc=de' 因为其父条目 'dc=parascus,dc=de' 在服务器中不存在。
可能是我的程序尝试添加我的 ldif 文件两次,第二次检测到重复。但是我怎么能告诉我的程序只添加一次呢?
亲切的问候
帕拉斯库斯
解决方案
好的,就像我上面描述的那样,现在它正在工作。问题是我通过 application.configuration 文件构建了 LDAP 树。之后我的配置尝试构建一个新的 DefaultSpringSecurityContextSource ,它引用了同一个文件。所以它试图再次插入相同的条目,这导致了麻烦。
感谢您的努力。
推荐阅读
- python - 如何创建具有特定 dtype 的新 Pandas 列?
- go - 处理程序提供服务后,如何从请求对象中取回记录器对象?
- rails-activestorage - 如何缩短 Rails Active Storage url 链接太长
- java - 如何遍历列表列表以操作内部列表的内容?
- sql - 如何在 Snowflake 中重命名表的多个列?
- java - Apache Commons CSVParser - 如何将列拆分为单独的键?
- python - 监控估算器进度和 sklearn 包装器
- html - 用 v-html 绑定图片
- java - 如何更改 Apache Tomcat 9 的 Java 版本
- javascript - 角度服务设置中的值未定义