title: Spring security without LDAP password
body: <p>I want to implement an LDAP login and search user with spring security without any password. I tried a lot but no luck, I used <code>ActiveDirectoryLdapAuthenticationProvider</code>  <code>DirContextOperations</code> but no luck, right now I am doing it with password:</p>
  
  <p>code For Login:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @PropertySource("classpath:/application.properties")
  public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private AppUserDetailService userDetailService;
  
      /*@Autowired
      private ActiveDirectoryLdapAuthenticationProvider a;*/
  
      private Logger logger = Logger.getLogger(WebSecurityConfig.class);
  
      @Value("${spring.ldap.userDnPatterns}")
      private String userDnPatterns;
  
      @Value("${spring.ldap.ldapUrl}")
      private String ldapUrl;
  
      @Value("${spring.ldap.managerDn}")
      private String managerDn;
  
      @Value("${spring.ldap.managerPassword}")
      private String managerPassword;
  
      @Override
      public void configure(AuthenticationManagerBuilder auth) throws Exception {
  
          try{
          //auth.authenticationProvider(a).userDetailsService(userDetailsService());
  
          auth.userDetailsService(userDetailService);
  
          auth.ldapAuthentication().userSearchFilter(userDnPatterns).contextSource()
          .url(ldapUrl)
          .managerDn(managerDn).managerPassword(managerPassword);
  
          }catch(Exception e){
              logger.error("erorr connecting LDAP" + e);
          }
      }
  
      @Override
        protected void configure(HttpSecurity http) throws Exception {
  
  
      //   Collection&lt;? extends GrantedAuthority&gt; userName= SecurityContextHolder.getContext().getAuthentication().getAuthorities();
  
          http.cors().and().csrf().disable().authorizeRequests()
              .antMatchers("/").permitAll()
              .antMatchers("/swagger-ui.html").permitAll()   // only to test swagger
              .antMatchers(HttpMethod.POST,"/multipleSave").permitAll()  // only to test file upload from UI /savefile
              .antMatchers("/index.jsp").permitAll()
              .antMatchers("/dist/**").permitAll()// only to test file upload from UI /savefile
              .antMatchers("/lib/**").permitAll()
              .antMatchers("/css/**").permitAll()
              .antMatchers("/webjars/**").permitAll()
              .antMatchers("/images/**").permitAll()
              .antMatchers("/configuration/**").permitAll()
              .antMatchers("/swagger-resources/**").permitAll()
              .antMatchers("/v2/**").permitAll()
              .antMatchers(HttpMethod.POST, "/login").permitAll()
             // .antMatchers("/users").hasAnyAuthority("ADMIN")
              //.antMatchers("/updateGroup").hasAnyAuthority("GROUPADMIN")
             // .antMatchers("/groups").hasAnyAuthority("GROUPADMIN")
              .antMatchers("/**").permitAll()
             // .anyRequest().authenticated()
              .and()
              // We filter the api/login requests
              .addFilterBefore(new JWTLoginFilter("/login", authenticationManager()),
                      UsernamePasswordAuthenticationFilter.class)
              // And filter other requests to check the presence of JWT in header
              .addFilterBefore(new JWTAuthenticationFilter(userDetailService),
                      UsernamePasswordAuthenticationFilter.class).exceptionHandling().accessDeniedPage("/403");
  
        }
  
  }
  </code></pre>
  
  <p>AppUserDetailService.java</p>
  
  <pre><code>@Component
  public class AppUserDetailService implements UserDetailsService {
  
      @Autowired
      private UserDAO userRepository;
  
      private Logger logger = Logger.getLogger(AppUserDetailService.class);
  
      @Override
      public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
  
          com.reminder.model.User user = userRepository.getUserByName(username);
          if (user == null) {
               //userRepository.updateUnSuccessLoginDate(user.getUserId());
              throw new UsernameNotFoundException("User '" + username + "' not found");
          }
          if(!user.getActive()){
  
              throw new UserInactiveException("User '" + username + "' Inactive");
          }
  
          userRepository.updateLoginDate(user.getUserId());
  
          List&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;&gt;();
  
          if(user.getGroupAdmin()!=null &amp;&amp; user.getGroupAdmin() ){
              authorities.add(new SimpleGrantedAuthority("GROUPADMIN"));
          }
  
  
          if(user.getUserAdmin()!=null &amp;&amp; user.getUserAdmin() ){
              authorities.add(new SimpleGrantedAuthority("ADMIN"));
          }
          UserDetails u = org.springframework.security.core.userdetails.User
          .withUsername(username)
          .password("")
          .authorities(authorities)
          .accountExpired(false)
          .accountLocked(false)
          .credentialsExpired(false)
          .disabled(false)
          .build();
  
          final UsernamePasswordAuthenticationToken authentication =
                  new UsernamePasswordAuthenticationToken(user, null, u.getAuthorities());
          SecurityContextHolder.getContext().setAuthentication(authentication);
  
          return u;
  
      }
  
  
  
      private List&lt;GrantedAuthority&gt; getGrantedAuthorities( List&lt;GrantedAuthority&gt; authorities) {
              authorities.add(new SimpleGrantedAuthority("ADMIN"));
          return authorities;
      }
  
  
  }
  </code></pre>
  
  <p>code for ldap search user:</p>
  
  <pre><code>public LdapUser getUserByUsername(String userName) {
          List&lt;LdapUser&gt; list = ldapTemplate.search(
                   query()
                   .attributes("cn","mail")
                   .where("objectclass").is("person").and("CN").is(userName),
                    new UserAttributesMapper());
          if (list != null &amp;&amp; !list.isEmpty()) {
              return list.get(0);
          }
          return null;
      }
  </code></pre>
  
  <p>LdapTemplet is defined in xml:</p>
  
  <pre><code>&lt;bean id="contextSource" class="org.springframework.ldap.core.support.LdapContextSource"&gt;
  
          &lt;property name="url" value="ldap://localhost:389/dc=psa365,dc=com" /&gt;
          &lt;property name="userDn" value="cn=admin,dc=test,dc=com" /&gt;
          &lt;property name="password" value="password" /&gt;
  
      &lt;/bean&gt;
  
      &lt;bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate"&gt;
          &lt;constructor-arg ref="contextSource" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p>Expectation: I want to implement the above without using password.</p>