title: Display error messages in Spring login
body: <p>I am using Spring security for authenticating users. I created a custom authentication provider and now I am wondering how I can get error messages from the provider into my form. This is the authenticate() method in my custom authentication provider:</p>
  
  <pre><code>@Override
  public Authentication authenticate(Authentication authentication) throws AuthenticationException {
      UserProfile profile = userProfileService.findByEmail(authentication.getPrincipal().toString());
  
      if(profile == null){
          throw new UsernameNotFoundException(String.format("Invalid credentials", authentication.getPrincipal()));
      }
  
      String suppliedPasswordHash = DigestUtils.shaHex(authentication.getCredentials().toString());
  
      if(!profile.getPasswordHash().equals(suppliedPasswordHash)){
          throw new BadCredentialsException("Invalid credentials");
      }
  
      UsernamePasswordAuthenticationToken token = new UsernamePasswordAuthenticationToken(profile, null, profile.getAuthorities());
  
      return token;
  }
  </code></pre>
  
  <p>This is my form:</p>
  
  <pre><code>&lt;form name='f' action="&lt;c:url value='j_spring_security_check' /&gt;" method='POST'&gt;
  &lt;div id="data-entry-form"&gt;
  
      &lt;div class="form-entry"&gt;
          &lt;label&gt;&lt;spring:message code="login.form.label.email"/&gt;&lt;/label&gt;
          &lt;input type='text' name='j_username' value=''&gt;
      &lt;/div&gt;
      &lt;div class="form-entry"&gt;
          &lt;label&gt;&lt;spring:message code="login.form.label.password"/&gt;&lt;/label&gt;
          &lt;input type='password' name='j_password'/&gt;
      &lt;/div&gt;
      &lt;div class="form-entry"&gt;
          &lt;input type="submit" value="Verzenden"/&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p></p>
  
  <p>How would I get error messages into my form? From the moment I press the login button, Spring takes over, so the only method I could generate error messages in would be the authenticate() method...</p>