0

I have the following web security in a Spring Boot application:

    @Override
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
                .contextSource(contextSource)
                .userSearchBase("OU=users,DC=example,DC=com")
                .userSearchFilter("userName={0}")
                .ldapAuthoritiesPopulator(authoritiesPopulator)
                .and()
            // fall back to the admin group if not found
            .ldapAuthentication()
                .contextSource(contextSource)
                .userSearchBase("OU=admins,DC=example,DC=com")
                .userSearchFilter("userName={0}")
                .ldapAuthoritiesPopulator(authoritiesPopulator)
            ;
    }

The idea is pretty straightforward: try searching in the users group and if the user is not found, then try the admin group. All of this works great until something goes wrong with the first lookup. If the users group suddenly goes away, for example, the first lookup will generate an exception and the second lookup is never attempted. Is there a way to configure LdapAuthenticationProviderConfigurer or perhaps AuthenticationManagerBuilder to not abort the whole process when one of the authentication provider exceptions out?

matt forsythe
  • 3,863
  • 1
  • 19
  • 29

1 Answers1

0

following this answer - maybe this (untested code)


    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.ldapAuthentication()
                .contextSource(contextSource)
                .userSearchBase("DC=example,DC=com")
                //.userSearchFilter("&((userName={0}))")
                .userDnPatterns("userName={0},OU=users", "userName={0},OU=admins")
                .ldapAuthoritiesPopulator(authoritiesPopulator))
    }
indybee
  • 1,507
  • 13
  • 17
  • This could be a workaround in some cases, but I think only in the case where you can use the `userDnPatterns` to produce a fully qualified DN, which is not the case in my situation. I actually have to perform a full-on LDAP search to locate the user. – matt forsythe Feb 09 '22 at 20:52
  • [This](https://stackoverflow.com/a/42441496/1211547) also sounds similar, maybe could be adopted for userSearchBase – indybee Feb 10 '22 at 13:19