Source:
<code>private void searchNestedGroupMembershipRoles( Consumer<Consumer<DirContext>> ctxConsumer, String groupDn, String groupName, Set<GrantedAuthority> authorities, int depth) { if (logger.isDebugEnabled()) { logger.debug( "Searching for roles for nested group '" + groupName + "', DN = " + "'" + groupDn + "', with filter " + nestedGroupSearchFilter + " in search base '" + getGroupSearchBase() + "'"); } final List<Pair<String, String>> groupRolesNameDn = new ArrayList<>(); ctxConsumer.accept( (ctx) -> { SpringSecurityLdapTemplate authTemplate = (SpringSecurityLdapTemplate) LDAPUtils.getLdapTemplateInContext(ctx, ldapTemplate); final String formattedFilter = MessageFormat.format(nestedGroupSearchFilter, groupDn, groupName); groupRolesNameDn.addAll( authTemplate.search( getGroupSearchBase(), formattedFilter, new AbstractContextMapper<Pair<String, String>>() { @Override protected Pair<String, String> doMapFromContext( DirContextOperations ctx) { String name = ctx.getStringAttribute(groupRoleAttribute); String dn = ctx.getNameInNamespace(); return Pair.of(name, dn); } })); }); if (logger.isDebugEnabled()) { logger.debug("Roles from search: " + groupRolesNameDn); } for (Pair<String, String> roleNameDn : groupRolesNameDn) { String role = roleNameDn.getLeft(); String dn = roleNameDn.getRight(); if (convertToUpperCase) { role = role.toUpperCase(); } boolean addedSuccesfuly = authorities.add(new SimpleGrantedAuthority(rolePrefix + role)); <START> if (maxGroupSearchLevel == -1 || (depth > 0 && addedSuccesfuly)) <END> searchNestedGroupMembershipRoles( ctxConsumer, dn, roleNameDn.getLeft(), authorities, depth - 1); } }</code><technical_language>if is depth limit, care circular references</technical_language>

Target:
private void searchNestedGroupMembershipRoles( Consumer<Consumer<DirContext>> ctxConsumer, String groupDn, String groupName, Set<GrantedAuthority> authorities, int depth) { if (logger.isDebugEnabled()) { logger.debug( "Searching for roles for nested group '" + groupName + "', DN = " + "'" + groupDn + "', with filter " + nestedGroupSearchFilter + " in search base '" + getGroupSearchBase() + "'"); } final List<Pair<String, String>> groupRolesNameDn = new ArrayList<>(); ctxConsumer.accept( (ctx) -> { SpringSecurityLdapTemplate authTemplate = (SpringSecurityLdapTemplate) LDAPUtils.getLdapTemplateInContext(ctx, ldapTemplate); final String formattedFilter = MessageFormat.format(nestedGroupSearchFilter, groupDn, groupName); groupRolesNameDn.addAll( authTemplate.search( getGroupSearchBase(), formattedFilter, new AbstractContextMapper<Pair<String, String>>() { @Override protected Pair<String, String> doMapFromContext( DirContextOperations ctx) { String name = ctx.getStringAttribute(groupRoleAttribute); String dn = ctx.getNameInNamespace(); return Pair.of(name, dn); } })); }); if (logger.isDebugEnabled()) { logger.debug("Roles from search: " + groupRolesNameDn); } for (Pair<String, String> roleNameDn : groupRolesNameDn) { String role = roleNameDn.getLeft(); String dn = roleNameDn.getRight(); if (convertToUpperCase) { role = role.toUpperCase(); } boolean addedSuccesfuly = authorities.add(new SimpleGrantedAuthority(rolePrefix + role)); if ((maxGroupSearchLevel == -1 || depth > 0) && addedSuccesfuly) searchNestedGroupMembershipRoles( ctxConsumer, dn, roleNameDn.getLeft(), authorities, depth - 1); } }