Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6679763
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:23:47+00:00 2026-05-26T04:23:47+00:00

We are using spring security to authenticate users from LDAP in our application. The

  • 0

We are using spring security to authenticate users from LDAP in our application. The authentication part is working properly but the authorization part is not working.

We are not able to retrieve the roles of the user from the LDAP.

From the book “Spring Security 3” by Peter Mularien

“This is because Active Directory stores group membership as attributes on
the LDAP entries of users themselves. Out of the box (as of the time of publishing),
Spring Security does not offer an LdapAuthoritiesPopulator that can be
configured to support the structure of a typical Active Directory LDAP tree.”

Below is my spring-security configuration file.

<?xml version="1.0" encoding="UTF-8"?>

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                            http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                            http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.0.xsd">

       <http use-expressions="true" >
        <intercept-url pattern="/resources/**" filters="none" />
        <intercept-url pattern="/login" access="permitAll"/>
        <intercept-url pattern="/**" access="isAuthenticated()" />
        <form-login login-page="/login" 
                    default-target-url="/home" 
                    always-use-default-target="true"  
                    authentication-failure-url="/login?login_error=1" />
        <logout invalidate-session="true"
                logout-success-url="/"
                logout-url="/logout"/>
    </http>

    <authentication-manager alias="ldapAuthenticationManager">  
        <authentication-provider ref="ldapAuthenticationProvider"/>  
    </authentication-manager> 

    <beans:bean id="ldapAuthenticationProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">  
        <beans:constructor-arg ref="ldapBindAuthenticator"/>  
        <beans:constructor-arg ref="ldapAuthoritiesPopulator"/>  
        <beans:property name="userDetailsContextMapper" ref="ldapUserDetailsContextMapper"/>  
    </beans:bean> 

    <beans:bean id="ldapServer" class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">  
        <!-- MS Active Directory -->  
        <beans:constructor-arg value="ldap://localhost:389/dc=myOrg,dc=net"/>  
        <beans:property name="userDn" value="admin"/>  
        <beans:property name="password" value="admin"/>
        <beans:property name="baseEnvironmentProperties">
            <beans:map>
                <beans:entry key="java.naming.referral" value="follow" />
            </beans:map>
        </beans:property>
    </beans:bean>  

    <beans:bean id="ldapBindAuthenticator" class="org.springframework.security.ldap.authentication.BindAuthenticator">  
        <beans:constructor-arg ref="ldapServer"/>  
        <beans:property name="userSearch" ref="ldapSearchBean"/>  
    </beans:bean>  

    <beans:bean id="ldapSearchBean" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">  
        <!-- MS Active Directory -->  
        <!-- user-search-base; relative to base of configured context source -->  
        <beans:constructor-arg value="ou=Software OU"/>  
        <!-- user-search-filter -->  
        <beans:constructor-arg value="(sAMAccountName={0})"/>  
        <beans:constructor-arg ref="ldapServer"/>  
    </beans:bean>  

    <beans:bean id="ldapAuthoritiesPopulator" class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
        <beans:constructor-arg ref="ldapServer" />
        <beans:constructor-arg value="" />
        <beans:property name="groupSearchFilter" value="(sAMAccountName={0})"/>
        <beans:property name="groupRoleAttribute" value="memberOf" />
        <beans:property name="rolePrefix" value=""/>
        <beans:property name="searchSubtree" value="true"/>
        <beans:property name="convertToUpperCase" value="false"/>
        <beans:property name="ignorePartialResultException" value="true"/>
    </beans:bean>

    <beans:bean class="org.springframework.security.ldap.userdetails.InetOrgPersonContextMapper" id="ldapUserDetailsContextMapper"/> 

</beans:beans>

Please help.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:23:47+00:00Added an answer on May 26, 2026 at 4:23 am

    You might want to take a look here: https://jira.springsource.org/browse/SEC-876. Although this code contribution was declined, with a reasonable answer, it might give you hints.

    We use the following config:

    Spring XML

    <bean id="ldapUserService" class="MyUserDetailService">
      <constructor-arg ref="ldapUserSearch"/>
      <constructor-arg ref="ldapAuthoritiesPopulator"/>
    </bean>
    <bean id="ldapUserSearch" class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
      <constructor-arg value="OU=FOO-Accounts,OU=FOO,OU=OU-GLOBAL"/> <!-- user search base, RELATIVE TO SERVER CONTEXT (URL & base of configured LDAP server)! -->
      <constructor-arg value="(sAMAccountName={0})"/> <!-- user search filter -->
      <constructor-arg ref="ldapServer"/>
    </bean>
    <bean id="ldapAuthoritiesPopulator" class="MyLdapAuthoritiesPopulator">
      <constructor-arg ref="ldapServer" />
      <constructor-arg value="=OU=SomeFooBar,OU=FOO-Global-Security,OU=FOO-Groups,OU=FOO,OU=OU-GLOBAL" /> <!-- group search base, RELATIVE TO SERVER CONTEXT (URL & base of configured LDAP server)! -->
      <constructor-arg ref="roleMappings"/>
      <property name="groupRoleAttribute" value="cn" />
      <property name="groupSearchFilter" value="(member={0})" />
    </bean>
    

    Populator

    There’s a lot of proprietary code I cannot share because our customer has extra information in the AD we need to extract. I removed that as its of no concern for the question. Hence, this code won’t compile.

    public class MyLdapAuthoritiesPopulator extends DefaultLdapAuthoritiesPopulator {
    
      /**
       * Prefix assigned by Spring Security to each group/role from LDAP.
       */
      public static final String AUTHORITY_ROLE_PREFIX = "ROLE_";
    
      private Properties roleMappings;
      private Properties invertedRoleMappings;
    
      /**
       *
       * @param contextSource supplies the contexts used to search for user roles.
       * @param groupSearchBase if this is an empty string the search will be performed from the root DN
       * of the context factory. If null, no search will be performed.
       * @param roleMappings maps logical (internal) role names to names as delivered by LDAP
       */
      @SuppressWarnings("deprecation")
      public MyLdapAuthoritiesPopulator(final ContextSource contextSource,
          final String groupSearchBase,
          final Properties roleMappings) {
        super(contextSource, groupSearchBase);
        setConvertToUpperCase(false);
        setRolePrefix("");
        this.roleMappings = roleMappings;
        this.invertedRoleMappings = invertRoleMappings();
        logger.info("Processing LDAP roles based on the following mapping: {}.", roleMappings);
      }
    
      .....
    
      @Override
      public Set<GrantedAuthority> getGroupMembershipRoles(final String userDn, final String username) {
        final Set<GrantedAuthority> effectiveGroupMembershipRoles = super.getGroupMembershipRoles(
            userDn, username);
        return mapEffectiveRolesToApplicationRoles(effectiveGroupMembershipRoles);
      }
    
      /**
       * Maps effective LDAP roles such as 'foo_boston_dispatcher' or 'foo_boston_readonly' to
       * FOO internal roles. The internal role (i.e. the {@link GrantedAuthority}) is a combination
       * of the 'ROLE_' prefix and a {@link Role} enum value. .........
       */
      Set<GrantedAuthority> mapEffectiveRolesToApplicationRoles(final Set<GrantedAuthority> effectiveGroupMembershipRoles) {
        logger.info("Processing effective roles from LDAP: {}.", effectiveGroupMembershipRoles);
        final Set<GrantedAuthority> internalRoles = new HashSet<GrantedAuthority>();
        final List<String> effectiveRoleNames = extractRoleNamesFrom(effectiveGroupMembershipRoles);
        final List<String> unmappedGroupMembershipRoles = new ArrayList<String>();
        ......
        // in a method invoked here we do something like internalRoles.add(new GrantedAuthority(AUTHORITY_ROLE_PREFIX + role));
        ......
        logger.info("Created internal roles {}.", internalRoles);
        logger.trace(
            "The following group membership roles were not mapped to an internal equivalent: {}",
            unmappedGroupMembershipRoles);
        return internalRoles;
      }
    
      ......
    
      private List<String> extractRoleNamesFrom(final Collection<GrantedAuthority> authorities) {
        final List<String> authorityNames = new ArrayList<String>(authorities.size());
        for (GrantedAuthority authority : authorities) {
          authorityNames.add(authority.getAuthority());
        }
        return authorityNames;
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Spring Security 3.0.2 form based authentication. But I can't figure out how
I'm using Spring Security 3.04 to authenticate the users of my system using 3
My application uses Spring Security for handling authentication. So far I was using simple
Is it possible, using spring security plugin 0.5.3 with Grails 1.2.1, to authenticate a
I have a j2ee web application using spring web flow ang spring security. I
Hi I am now using spring security. It works fine. But if login failed,
I'm using spring-security and jQuery in my application. Main page uses loading content dynamically
When using spring security, specifically with @notation; what is the proper way to access
I'm using Spring Security 3.0.2 and I can't find a way to load roles
I'm using Spring, Spring Security, BlazeDS, Flex and spring-flex. I know that I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.