title: Adding Authorities in AuthenticationSuccessHandler
body: <p>I'm working with a Spring MVC website and adding authentication with Active Directory over LDAP. The company doesn't want to use the AD authorities to map permissions for the website, we have a database that lists each user's permissions so I'm trying to connect to that, get the permissions, and add them to the user's Authentication token.</p>
  
  <p>When I first started I was mapping the AD user group's authorities with a <code>GrantedAuthoritiesMapper</code> and I had that working. It looked like this:</p>
  
  <pre><code>public class ActiveDirectoryGrantedAuthoritiesMapper implements GrantedAuthoritiesMapper {
  
      private static final String ROLE_ADMIN = "adminUserGroup";
  
      public ActiveDirectoryGrantedAuthoritiesMapper()
      { }
  
      public Collection&lt;? extends GrantedAuthority&gt; mapAuthorities(
              final Collection&lt;? extends GrantedAuthority&gt; authorities)
      {
  
          Set&lt;CustomAuthority&gt; roles = EnumSet.noneOf(CustomAuthority.class);
  
          for (GrantedAuthority authority : authorities)
          {
              if (ROLE_ADMIN.equals(authority.getAuthority()))
              {
                  roles.add(CustomAuthority.ROLE_ADMIN);
              }
              //Default role for all users.
              roles.add(CustomAuthority.ROLE_EMPLOYEE);
          }
          return roles;
      }
  }
  </code></pre>
  
  <p>Now I'm trying to convert it to query our database for permissions. I moved away from the <code>GrantedAuthoritiesMapper</code> to do this for two reasons. First off I'm not using the Authorities from LDAP so why even intercept them? And also because I couldn't figure out how to get the name of the user logging in inside of the <code>GrantedAuthoritiesMapper</code>. I tried using the <code>SecurityContext</code> but it was giving me a <code>NullPointerException</code> whenever I tried to call <code>context.getAuthentication().getName()</code> I assume because the user wasn't fully authenticated yet.</p>
  
  <p>So I switched to using an <code>AuthenticationSuccessHandler</code>. I tried to keep the logic just about the same. I'm trying to add the roles to the user's Authentication token with <code>authentication.getAuthorities().add(...);</code> but I'm getting errors that my <code>CustomAuthority</code> doesn't extend <code>GrantedAuthority</code>. It doesn't extend it, but it implements the interface. I was wondering if this was because it was an enum, so I changed it to a class and I'm still getting the error. Here's the code for the custom <code>AuthenticationSuccessHandler</code> as I have it now:</p>
  
  <pre><code>public class CustomAuthoritiesMapper implements AuthenticationSuccessHandler 
  {
      private CustomPermissionDAO permissionsDao = new CustomPermissionDAO();
  
      private static final String ROLE_ADMIN = "ADMIN_ACCOUNT";
  
      @Override
      public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
              throws IOException, ServletException
      {
          List&lt;GrantedAuthority&gt; roles = new ArrayList&lt;GrantedAuthority&gt;();
  
          List&lt;DatabasePermission&gt; permissionsForUser = permissionsDao.getPermissionByUsername(authentication.getName());
  
          for (DatabasePermission permission : permissionsForUser)
          {
              if (ROLE_ADMIN.equals( permission.getTag() ))
              {
                  roles.add(new CustomAuthority("ROLE_ADMIN"));
              }
              //Default role for all users.
              roles.add(new DashboardAuthority("ROLE_EMPLOYEE"));
          }
          for(GrantedAuthority auth : roles)
          {
              authentication.getAuthorities().add(auth);
          }
      }
  }
  </code></pre>
  
  <p>I've tried just about every combination of everything that I can think of.. I've changed the <code>List&lt;GrantedAuthority&gt;</code> to a List of CustomAuthority objects. I've tried using <code>addAll(roles)</code> instead of adding individual ones.. Each time I get some variation of this same error:</p>
  
  <p><em>The method add(capture#1-of ? extends GrantedAuthority) in the type Collection is not applicable for the arguments (GrantedAuthority)</em></p>
  
  <p>And the CustomAuthority code:</p>
  
  <pre><code>public class CustomAuthority implements GrantedAuthority
  {
      private String name;
  
      public CustomAuthority(String name)
      {
          this.name = name;
      }
  
      public String getAuthority() {
          return name;
      }
  }
  </code></pre>
  
  <p>Any help would be much appreciated.</p>
  
  <p>From looking at the "related questions" it looks like authentication.getName() might not work here but I want to figure out why I can't add the permissions I want to add to the user's authorities before I tackle that issue.</p>