title: How to configure Spring for LDAP and JDBC?
body: <p>In my web application, I have to use Spring Security and get the user's authentication details using LDAP and the user's authorization details using JDBC. The user submits a form and I get the username and password from it.</p>
  
  <ol>
  <li>How Do I get the username and password in my <code>WebSecurityConfig</code> file?</li>
  <li>How do I configure the authorization and authentication?</li>
  </ol>
  
  <p>My <code>WebSecurityConfig</code>:</p>
  
  <pre><code>@Configuration
  @EnableWebMvcSecurity
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.authorizeRequests().anyRequest().authenticated().and().formLogin()
          .loginPage("/").permitAll();
      }
  
      @Autowired
      public void configureGlobal(AuthenticationManagerBuilder auth)
              throws Exception {
          auth.ldapAuthentication().userDnPatterns("uid={0},ou=people")
                  .groupSearchBase("ou=groups").contextSource(contextSource());
      }
  
      @Bean
      public DefaultSpringSecurityContextSource contextSource() {
          DefaultSpringSecurityContextSource contextSource = new DefaultSpringSecurityContextSource(
                  "ldap://mjkoldc-03.red.com");
          contextSource.setUserDn("mj\\" + email);
          contextSource.setPassword(password);
          contextSource.setReferral("follow");
          contextSource.afterPropertiesSet();
          return contextSource;
  
      }
  }
  </code></pre>
  
  <p>Previously I was getting details using <code>LDAPTemplate</code>:</p>
  
  <pre><code>LdapQuery query = query().base("dc=metaljunction,dc=com")
              .attributes("GivenName", "sn", "mail", "MobilePhone")
              .where("ObjectClass").is("user").and("SamAccountName")
              .is(email);
  </code></pre>