title: Login Authentication with Email using UserDetailService in SpringSecurity
body: <p>I am trying to login with Email instead of UserName using Spring Security UserDetailService authentication but I can't able to login and get </p>
  
  <blockquote>
    <p>org.springframework.security.authentication.InternalAuthenticationServiceException.</p>
  </blockquote>
  
  <p>When I debug codes, debugger does not go after </p>
  
  <blockquote>
    <p>Users user = userDao.findByUserName(useremail); from UserServiceImpl.java and occurred error.</p>
  </blockquote>
  
  <p>May be there is one step require more in somewhere.
  Configuration and Java codes and Error Logs are as below:-</p>
  
  <p><strong>ApplicationContext.xml</strong></p>
  
  <pre><code>&lt;security:http auto-config="true"&gt;
          &lt;security:intercept-url pattern="/admin/**" access="ROLE_ADMIN" /&gt;
          &lt;security:intercept-url pattern="/customer/**" access="ROLE_USER" /&gt;
          &lt;security:form-login login-page="/login"
              default-target-url="/product/productList/all" authentication-failure-url="/login?error"
              username-parameter="username" password-parameter="password" /&gt;
          &lt;security:logout logout-success-url="/login?logout" /&gt;
      &lt;/security:http&gt;
  
      &lt;security:authentication-manager&gt;
          &lt;security:authentication-provider user-service-ref="userServiceImpl"&gt;
                  &lt;security:password-encoder ref="bcryptEncoder" /&gt;
          &lt;/security:authentication-provider&gt;
      &lt;/security:authentication-manager&gt;
  
      &lt;bean id="userServiceImpl" class="com.emusicstore.service.impl.UserServiceImpl" /&gt;
  
      &lt;bean id="bcryptEncoder" class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" &gt;
      &lt;constructor-arg name="strength" value="11" /&gt;
      &lt;/bean&gt;
  </code></pre>
  
  <p><strong>Login.jsp</strong></p>
  
  <pre><code>&lt;form name="loginForm" action="&lt;c:url value="/j_spring_security_check" /&gt;" method="post"&gt;
  
                  &lt;c:if test="${not empty error}"&gt;
                      &lt;div class="error" style="color: #ff0000;"&gt;${error}&lt;/div&gt;
                  &lt;/c:if&gt;
  
                  &lt;div class="form-group"&gt;
                      &lt;label for="username"&gt;Email : &lt;/label&gt;
                      &lt;input type="text" id="username" name="username" class="form-control" /&gt;
                  &lt;/div&gt;
                  &lt;div class="form-group"&gt;
                      &lt;label for="password"&gt;Passwrod:&lt;/label&gt;
                      &lt;input type="password" id="password" name="password" class="form-control" /&gt;
                  &lt;/div&gt;
                  &lt;input type="submit" value="Submit" class="btn btn-default"&gt;
                  &lt;input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}" /&gt;
              &lt;/form&gt;
  </code></pre>
  
  <p><strong>UserServiceImpl.java</strong></p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collection;
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.beans.factory.annotation.Qualifier;
  import org.springframework.security.authentication.jaas.AuthorityGranter;
  import org.springframework.security.core.GrantedAuthority;
  import org.springframework.security.core.authority.SimpleGrantedAuthority;
  import org.springframework.security.core.userdetails.UserDetails;
  import org.springframework.security.core.userdetails.UserDetailsService;
  import org.springframework.security.core.userdetails.UsernameNotFoundException;
  import org.springframework.stereotype.Component;
  import org.springframework.stereotype.Service;
  import org.springframework.transaction.annotation.Transactional;
  
  import com.emusicstore.dao.UserDao;
  import com.emusicstore.model.Users;
  import com.emusicstore.service.UserService;
  @Service
  @Transactional
  @Qualifier("userServiceImpl")
  public class UserServiceImpl implements UserDetailsService{
  
      @Autowired
      private UserDao userDao;
  
      @Override
      public UserDetails loadUserByUsername(String useremail)
              throws UsernameNotFoundException {
          Users user = userDao.findByUserEmail(useremail);
          if(user == null){
              throw new UsernameNotFoundException("UserName or Password Invalid.");
          }
          return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.getEnabled(), true, true, true, getGrantedAuthorities(userDao.getUserRole(user.getUsersId())));
      }
  
      private final List&lt;GrantedAuthority&gt; getGrantedAuthorities(final List&lt;String&gt; roleList) {
          final List&lt;GrantedAuthority&gt; authorities = new ArrayList&lt;GrantedAuthority&gt;();
          for (final String role : roleList) {
              authorities.add(new SimpleGrantedAuthority(role));
          }
          return authorities;
  }
  }
  </code></pre>
  
  <p><strong>Console Error as below:</strong></p>
  
  <blockquote>
    <p>ERROR:
    org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter
    - An internal error occurred while trying to authenticate the user. org.springframework.security.authentication.InternalAuthenticationServiceException
      at
    org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:110)
      at
    org.springframework.security.authentication.dao.AbstractUserDetailsAuthenticationProvider.authenticate(AbstractUserDetailsAuthenticationProvider.java:132)
      at
    org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
      at
    org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:177)
      at
    org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter.attemptAuthentication(UsernamePasswordAuthenticationFilter.java:92)
      at
    org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:211)
      at
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at
    org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:110)
      at
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at
    org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:50)
      at
    org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
      at
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at
    org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
      at
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:342)
      at
    org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:192)
      at
    org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:160)
      at
    org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
      at
    org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
      at
    org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)
      at
    org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
      at
    org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:212)
      at
    org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)
      at
    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)
      at
    org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
      at
    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
      at
    org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)
      at
    org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at
    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)
      at
    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1096)
      at
    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:674)
      at
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1500)
      at
    org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1456)
      at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
      at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
      at
    org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
      at java.lang.Thread.run(Unknown Source) Caused by:
    java.lang.NullPointerException    at
    com.emusicstore.service.impl.UserServiceImpl.loadUserByUsername(UserServiceImpl.java:33)
      at
    org.springframework.security.authentication.dao.DaoAuthenticationProvider.retrieveUser(DaoAuthenticationProvider.java:102)
      ... 35 more</p>
  </blockquote>
  
  <p>Kindly help me overcome through above errors.</p>
  
  <p>Thank You Very Much.</p>