我必须从 LDAP 服务器读取操作属性(createTimeStamp、entryUUID 等)。
我试图实现自己的 UserDetailContextMapper 但没有成功。
目前我的代码看起来像这样
Override
public void configure(AuthenticationManagerBuilder auth) throws Exception {
auth
.ldapAuthentication()
.userSearchFilter("uid={0}")
.groupSearchBase("ou=users")
.userDetailsContextMapper(userContextMapper())
.contextSource(contextSource());
}
@Bean
public LdapContextSource contextSource() {
LdapContextSource contextSource = new LdapContextSource();
contextSource.setUrl("ldap://ldap.company.pl:389");
contextSource.setBase("dc=company,dc=com");
contextSource.afterPropertiesSet();
return contextSource;
}
@Bean
public UserDetailsContextMapper userContextMapper() {
return new CustomUserDetailContextMapper();
}
我的 UserDetailContextMapper 实现:
public class CustomUserDetailContextMapper implements UserDetailsContextMapper {
@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<?
extends GrantedAuthority> authorities) {
AutoUser user = new AutoUser();
user.setCreateTimeStamp(ctx.getStringAttribute("createTimestamp"));
user.setUUID(ctx.getStringAttribute("entryUUID"));
user.setEmail(ctx.getStringAttribute("mail"));
return user;
}
@Override
public void mapUserToContext(UserDetails user, DirContextAdapter ctx) {
//default impl
}
}
我试过了Attributes attributes = ctx.getAttributes(username, new String[] {"*", "+"});
在方法中,但如果我通过了mapUserFromContext(),我就会收到。NamingExceptionusername
我正在考虑实现 ldapTemplate.lookup(),但我不确定在哪里实现它,我尝试用 ldapTemplate 做 userRepo 类,但我得到NameNotFoundException了我通过的任何 DN。
鸿蒙传说
相关分类