title: Spring Security 3.1 - LdapAuthenticationProvider + GrantedAuthoritiesMapper
body: <p>I created an AuthoritiesMapper for DefaultLdapAuthoritiesPopulator. Roles are being mapped correctly and working with intercept-url.</p>
  
  <p>If I try to access the roles in the controller using getUserPrincipal(), I'm still getting the LDAP groups.</p>
  
  <p>Had a look at the code in the populator and it should work.</p>
  
  <p>Any idea what wrong or how it can be fixed? i need to show/hide parts of views depending on roles</p>
  
  <pre><code>/*************************************************************************************
   * Maps Spring Security GrantedAuthorities 
   * e.g. AD groups populated using LdapAuthoritiesPopulator mapped to fixed role names
   * as defined in a Map instance (e.g. populated from a property file)
   * 
   * Sample roleMap:
   * Key          Value
   * Group1       ROLE_USER
   * Group2       ROLE_ADMIN
   * Group3       ROLE_ADMIN,ROLE_USER
   *************************************************************************************/
  public class MapBasedGrantedAuthorityMapper implements GrantedAuthoritiesMapper {
      private Map&lt;String,String&gt; roleMap;
      private String stringSeparator = ",";
      private SimpleGrantedAuthority unknownAuthorithy = new SimpleGrantedAuthority("ROLE_UNKNOWN");
      private boolean keepUnknownAuthorities = false;
      public MapBasedGrantedAuthorityMapper(Map&lt;String,String&gt; roleMap){
          this.roleMap = roleMap;
      }
  
      public Collection&lt;? extends GrantedAuthority&gt; mapAuthorities(Collection&lt;? extends GrantedAuthority&gt; authorities) {
          String[] mappedValues; 
          Set&lt;GrantedAuthority&gt; mapped = new HashSet&lt;GrantedAuthority&gt;(authorities.size());
          for (GrantedAuthority auth : authorities) {
              if (roleMap.containsKey(auth.getAuthority())) {
                  mappedValues = StringUtils.split(roleMap.get(auth.getAuthority()),stringSeparator);
                  for (String mappedValue: mappedValues) {                    
                      mapped.add(new SimpleGrantedAuthority(StringUtils.trimToEmpty(mappedValue)));
                  }
              } else if (keepUnknownAuthorities){ 
                  mapped.add(auth);
              } else if (unknownAuthorithy != null){
                  mapped.add(unknownAuthorithy);
              }
          }       
          return mapped;
      }
               // getters and setters
  
  }
  </code></pre>