Skip to content

Impl setting a custom LdapAuthenticationProvider w/ AbstractLdapAuthenticationManagerFactory #13100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,7 @@
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.ldap.authentication.AbstractLdapAuthenticator;
import org.springframework.security.ldap.authentication.LdapAuthenticationProvider;
import org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider;
import org.springframework.security.ldap.search.FilterBasedLdapUserSearch;
import org.springframework.security.ldap.userdetails.LdapAuthoritiesPopulator;
import org.springframework.security.ldap.userdetails.UserDetailsContextMapper;
Expand All @@ -33,6 +34,7 @@
* Creates an {@link AuthenticationManager} that can perform LDAP authentication.
*
* @author Eleftheria Stein
* @author Roman Zabaluev
* @since 5.7
*/
public abstract class AbstractLdapAuthenticationManagerFactory<T extends AbstractLdapAuthenticator> {
Expand All @@ -45,6 +47,8 @@ public abstract class AbstractLdapAuthenticationManagerFactory<T extends Abstrac

private String[] userDnPatterns;

private LdapAuthenticationProvider customAuthenticationProvider;

private LdapAuthoritiesPopulator ldapAuthoritiesPopulator;

private GrantedAuthoritiesMapper authoritiesMapper;
Expand Down Expand Up @@ -72,6 +76,16 @@ protected final BaseLdapPathContextSource getContextSource() {
return this.contextSource;
}

/**
* Set a custom {@link LdapAuthenticationProvider} which will be used
* for creation of {@link LdapAuthenticationProvider} rather than constructed automatically.
* Useful for custom implementations, like {@link ActiveDirectoryLdapAuthenticationProvider}
* @param provider A custom {@link LdapAuthenticationProvider} implementation
*/
public void customAuthenticationProvider(final LdapAuthenticationProvider provider) {
this.customAuthenticationProvider = provider;
}

/**
* Sets the {@link LdapAuthoritiesPopulator} used to obtain a list of granted
* authorities for an LDAP user.
Expand Down Expand Up @@ -145,11 +159,14 @@ public final AuthenticationManager createAuthenticationManager() {
private LdapAuthenticationProvider getProvider() {
AbstractLdapAuthenticator authenticator = getAuthenticator();
LdapAuthenticationProvider provider;
if (this.ldapAuthoritiesPopulator != null) {
provider = new LdapAuthenticationProvider(authenticator, this.ldapAuthoritiesPopulator);

if (this.customAuthenticationProvider != null) {
provider = this.customAuthenticationProvider;
}
else {
provider = new LdapAuthenticationProvider(authenticator);
provider = (this.ldapAuthoritiesPopulator != null)
? new LdapAuthenticationProvider(authenticator, this.ldapAuthoritiesPopulator)
: new LdapAuthenticationProvider(authenticator);
}
if (this.authoritiesMapper != null) {
provider.setAuthoritiesMapper(this.authoritiesMapper);
Expand Down