Spring-Boot LDAP anpassen UserDetails
Ich verwende die LDAP-Authentifizierung in einer Spring-Boot-Anwendung (Konfiguration basiert auf Anmerkungen). Ich möchte das UserDetails-Objekt anpassen. Die Standardimplementierung von UserDetails ist LdapUserDetailsImpl. Ich möchte diese Klasse erweitern und einige zusätzliche Iterfaces hinzufügen, um die Frühlingssicherheit zu gewährleisten. Meine Konfigurationsklasse:
@Configuration
protected static class AuthenticationConfiguration extends GlobalAuthenticationConfigurerAdapter {
@Autowired
private UserService userService;
@Autowired
private Environment env;
@Override
public void init(AuthenticationManagerBuilder auth) throws Exception {
AuthMethod authMethod = AuthMethod.valueOf(env.getRequiredProperty("auth_method"));
switch (authMethod) {
case LDAP:
auth.ldapAuthentication()
.userDnPatterns(env.getRequiredProperty("ldap.user_dn_patterns"))
.groupSearchBase(env.getRequiredProperty("ldap.group_search_base"))
.contextSource()
.url(env.getRequiredProperty("ldap.url"));
break;
default:
auth.userDetailsService(userService);
break;
}
}
@Bean
public LdapContextSource contextSource () {
LdapContextSource contextSource= new LdapContextSource();
contextSource.setUrl(env.getRequiredProperty("ldap.url"));
contextSource.setUserDn(env.getRequiredProperty("ldap.user"));
contextSource.setPassword(env.getRequiredProperty("ldap.password"));
contextSource.afterPropertiesSet();
return contextSource;
}
}
UserService ist eine benutzerdefinierte Authentifizierungsmethode (Datenbank- / JPA-Authentifizierung). UserDetails-Accessor (bei LDAP-Authentifizierungsmethode wird das LdapUserDetailsImpl-Objekt zurückgegeben):
@Component("activeUserAccessor")
public class ActiveUserAccessorImpl implements ActiveUserAccessor
{
public UserDetails getActiveUser()
{
return (UserDetails) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
}
}
Danke für deine Hilfe