title: Java Spring Security - Application Role Based Active Directory User Authentication
body: <p>I am developing a web application with spring mvc framework and java. I am using active directory user authentication in the application.</p>
  
  <p>Now i want to assign role to these users from a sql database. So the whole authentication process will be done by taking information from two sources: [ user id &amp; password from active directory &amp; roles of user from sql database ].</p>
  
  <p>I have searched on internet to get any tutorial/samples to do so but I'm unable to get any useful stuff. So any help on the issue will be appreciated.</p>
  
  <p>Here is the current code of my spring security file,</p>
  
  <pre><code>&lt;bean id="adAuthenticationProvider"
        class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider"&gt;
      &lt;constructor-arg value="abbl.org"/&gt;
      &lt;constructor-arg value="LDAP://abbl.org"/&gt;
      &lt;property name="convertSubErrorCodesToExceptions" value="true"/&gt;
      &lt;property name="useAuthenticationRequestCredentials" value="true"/&gt;
  &lt;/bean&gt;
  
  &lt;bean id="customAuthenticationProvider"
        class="org.abbl.exhbp.templates.CustomAuthorityProvider"&gt;
      &lt;constructor-arg ref="adAuthenticationProvider"/&gt;
  &lt;/bean&gt;
  
  &lt;security:authentication-manager&gt;
      &lt;security:authentication-provider ref="customAuthenticationProvider"/&gt;
  &lt;/security:authentication-manager&gt;
  </code></pre>
  
  <p>my authenticate function in AuthenticationProvider class,</p>
  
  <pre><code>public Authentication authenticate(Authentication authentication) {
  
      final Authentication a = delegate.authenticate(authentication);
  
      // Load additional authorities and create an Authentication object
      final List&lt;GrantedAuthority&gt; authorities = getGrantedAuthorities(a.getName());
  
      return new AbstractAuthenticationToken(authorities) {
          @Override
          public Object getCredentials() {
              //return null;  //To change body of implemented methods use File | Settings | File Templates.
              throw new UnsupportedOperationException();
          }
  
          @Override
          public Object getPrincipal() {
              //return null; //To change body of implemented methods use File | Settings | File Templates.
              return a.getPrincipal();
          }
      };
  }
  </code></pre>
  
  <p>After login , i am getting this exception,</p>
  
  <pre><code>java.lang.UnsupportedOperationException
  org.abbl.exhbp.templates.CustomAuthorityProvider$1.getCredentials(CustomAuthorityProvider.java:41)
  org.springframework.security.authentication.AbstractAuthenticationToken.eraseCredentials(AbstractAuthenticationToken.java:108)
  org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:186)
  org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:174)
  org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:94)
  org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:195)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:105)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
  org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
  org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
  org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
  org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
  org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
  </code></pre>
  
  <p>What modification or implementation I need to do here to achieve what i mentioned above?</p>