SEC-2982: Make org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider non-final
Jon Kranes (Migrated from SEC-2982) said:
Make this class non final so it can be extended by application code. The current final class forces application developers who need to override one or more methods from this class to copy and paste the entire class into application code, which is clearly an anti-pattern and seems contrary to general Spring openness to extension.
Bill Chen said:
Please also make some of those private methods called by doAuthentication(), etc. protected. Some of them are requried to make a useful extension.
I support this request and I am also wondering why there is no comment from the spring team? Nested groups (https://github.com/spring-projects/spring-security/issues/2053) are still not supported and this final prohibits this and possibly other enhancements.
I cannot agree more :( ...make the class non final please.
We try and prefer composition to inheritance. Would allowing an LdapAuthoritiesPopulator to be injected solve the issue for you?
Thank you @rwinch. Not sure LdapAuthoritiesPopulator is going to help us. We are looking for a way to augment the authentication piece to look in both db and ldap. For instance if the user is found in both db and ldap follow with next steps else reject the request. We would like to do minimal change and use the existing ActiveDirectoryLdapAuthenticationProvider for the most part. If possible we would like to avoid implementing AuthenticationProvider with ActiveDirectoryLdapAuthenticationProvider as delegate and overriding authenticate method to provide additional db checks before delegating. Any ideas or recommendation will be appreciated.
If we extract the existing logic into DefaultLdapAuthoriesPopulator and then you created a new implementation you could do something like this:
class MyLdapAuthoritiesPopulator {
LdapAuthoritiesPopulator delegate = new DefaultLdapAuthoritiesPopulator();
public Collection<? extends GrantedAuthority> getGrantedAuthorities(
DirContextOperations userData, String username) {
Collection<? extends GrantedAuthority> ldap = delegate.getGrantedAuthorities(userData, username);
Collection<? extends GrantedAuthority> db = youImplementThis(username);
// check if in both and throw exception if you need to
return ...some result...;
}
}
Does that work for you?
Thanks @rwinch. I believe your answer solves the problem when we would like to load roles from multiple places like db and ldap. My ask was for authentication part which I believe can be accomplished by extracting the existing logic inside doAuthentication into ActiveDirectoryLdapAuthenticator and I will create a class like this. Not sure this is right approach.
class MyLdapAuthenticator {
LdapAuthenticator delegate = new ActiveDirectoryLdapAuthenticator();
public DirContextOperations authenticate(UsernamePasswordAuthenticationToken authentication) {
DirContextOperations ldapAuth = delegate.authenticate(authentication);
DirContextOperations db = myAuthentication(authentication);
return ..;
}
}
I'm not sure I understand. In particular, I'm not sure how this issue (making ActiveDirectoryLdapAuthenticationProvider non-final) relates to your comment above.
I'd like to connect to an LDAP but using "short names" (e.g. uid, sAMAccountName, ...) instead of fully qualified DNs. In order to do so I'd like to overwrite bindAsUser() (which is private) and/or searchForUser() (which is private as well), but ActiveDirectoryLdapAuthenticationProvider is final.
Is there any advantage in making ActiveDirectoryLdapAuthenticationProvider final? Is there any advantage in making its methods private?
@crusy The advantages are it provides better encapsulation which makes updating Spring Security code easier. Making it final also encourages composition over inheritance.
I'm going to close this issue due to lack of feedback from the original reporter. If someone has a concrete thing they would like to do, please create a new ticket. Note I'm primarily looking for the problem you are having, not a suggestion of making ActiveDirectoryLdapAuthenticationProvider non-final.