spring-security icon indicating copy to clipboard operation
spring-security copied to clipboard

SEC-2982: Make org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider non-final

Open spring-projects-issues opened this issue 11 years ago • 11 comments

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.

spring-projects-issues avatar May 19 '15 06:05 spring-projects-issues

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.

spring-projects-issues avatar Sep 16 '15 12:09 spring-projects-issues

Jon Osborn said:

Remove final from this class so we can extend it!

spring-projects-issues avatar Nov 19 '15 02:11 spring-projects-issues

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.

steigerm avatar Mar 26 '17 12:03 steigerm

I cannot agree more :( ...make the class non final please.

tomas-silhavy avatar Sep 06 '17 22:09 tomas-silhavy

We try and prefer composition to inheritance. Would allowing an LdapAuthoritiesPopulator to be injected solve the issue for you?

rwinch avatar Jan 31 '19 15:01 rwinch

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.

saagar2000 avatar Jan 31 '19 18:01 saagar2000

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?

rwinch avatar Feb 04 '19 20:02 rwinch

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 ..;
    }
}

saagar2000 avatar Feb 05 '19 01:02 saagar2000

I'm not sure I understand. In particular, I'm not sure how this issue (making ActiveDirectoryLdapAuthenticationProvider non-final) relates to your comment above.

rwinch avatar Feb 05 '19 20:02 rwinch

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 avatar Jun 25 '19 07:06 crusy

@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.

rwinch avatar Jun 27 '19 18:06 rwinch