title: Bad Credentials response when using Spring Security in memory authentication
body: <p>I have configured my Java Spring backend to use in memory authentication but when I do a POST request through my angular2 frontend entering the right username/password in a login form, I always get the following errors:</p>
  
  <blockquote>
    <p>x POST <a href="http://localhost:8080/authenticate" rel="nofollow noreferrer">http://localhost:8080/authenticate</a> 401 ()    zone.js:1382</p>
    
    <p>x EXCEPTION: Response with status: 401 OK for URL: <a href="http://localhost:8080/authenticate" rel="nofollow noreferrer">http://localhost:8080/authenticate</a>     core.umd.js:3370</p>
    
    <p>x Uncaught Response {_body: "{"code":401,"message":"Authentication failed","err…reason":"authError","message":"Bad credentials"}}", status: 401, ok: false, statusText: "OK", headers: Headers…}    Subscriber.ts:241</p>
  </blockquote>
  
  <p>I have tried so many different security configurations but nothing seems to work. I may be doing something wrong... here's my code:</p>
  
  <p><strong>SecurityConfiguration.java</strong></p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
  private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);
  
  public static final String REMEMBER_ME_KEY = "rememberme_key";
  
  public SecurityConfig() {
      super();
      logger.info("loading SecurityConfig ................................................ ");
  }
  
  
  
  @Autowired
  private RestUnauthorizedEntryPoint restAuthenticationEntryPoint;
  
  @Autowired
  private AccessDeniedHandler restAccessDeniedHandler;
  
  @Autowired
  private AuthenticationSuccessHandler restAuthenticationSuccessHandler;
  
  @Autowired
  private AuthenticationFailureHandler restAuthenticationFailureHandler;
  
  
  
  
  
  
  @Override
  public void configure(WebSecurity web) throws Exception {
      web.ignoring().antMatchers("/","/client/**","/systemjs.config.js","/index.html");
  }
  
  @Override
  protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.inMemoryAuthentication().withUser("test").password("test").authorities("ADMIN");
  
  }
  
  @Override
  protected void configure(HttpSecurity http) throws Exception {
      http
          .headers().disable()
          .csrf().disable()
          .authorizeRequests()
              .antMatchers("/failure").permitAll()
              .antMatchers("/api/**").permitAll()
              .antMatchers("/users").hasAnyAuthority("ADMIN")
              .anyRequest().authenticated()
              .and()
          .exceptionHandling()
              .authenticationEntryPoint(restAuthenticationEntryPoint)
              .accessDeniedHandler(restAccessDeniedHandler)
              .and()
  
          .formLogin()
              .loginPage("/login")
              .loginProcessingUrl("/authenticate")
              .successHandler(restAuthenticationSuccessHandler)  
              .failureHandler(restAuthenticationFailureHandler) 
              .usernameParameter("username")
              .passwordParameter("password")
              .permitAll()
              .and()
          .logout()
              .logoutUrl("/logout")
              .logoutSuccessHandler(new HttpStatusReturningLogoutSuccessHandler())
              .deleteCookies("JSESSIONID")
              .permitAll()
              .and();
  
  
  }
  }
  </code></pre>
  
  <p><strong>login.service.ts</strong></p>
  
  <pre><code>@Injectable()
  export class LoginService {
  constructor(private http: Http) {
      console.log('Login service initialized...')
  }
  
  
  loginUser(data) {
      var headers = new Headers();  
      headers.append('Content-type', 'application/json');
  
       return this.http.post('/authenticate', JSON.stringify(data), { headers: headers })
          .map(res =&gt; res.json());
  
  }
  </code></pre>
  
  <p><strong>login.component.ts</strong></p>
  
  <pre><code>export class LoginComponent {
  
  username:String;
  password:String;
  data:any
  constructor(private LoginService: LoginService) {
  
  }
  
  
   loginUser(event) {
  
      this.data = {
    username: this.username,
    password: this.password
  };
  
         this.LoginService.loginUser(this.data).subscribe(data =&gt; {
  
              this.username = '';  
              this.password = ''; 
  
          });
  
   }
  </code></pre>
  
  <p><strong>login.html</strong></p>
  
  <pre><code>&lt;form class="container col-md-8" (ngSubmit)="loginUser()" style="margin:15px 15px"&gt;
  &lt;div class="row"&gt;
  &lt;div class="form group col-md-2 parent "&gt;
      &lt;button type="submit" class="btn btn-warning centerV_element"&gt;Log In&lt;/button&gt;
  &lt;/div&gt;
  &lt;div class="form group col-md-3 parent" style="margin:3px 3px"&gt;
  &lt;input type="text" [(ngModel)]="username" name="username" class="form control" placeholder="username"&gt;
  &lt;/div&gt;
  &lt;div class="form group col-md-3 parent" style="margin:3px 3px"&gt;
  &lt;input type="text" [(ngModel)]="password" name="password" class="form control" placeholder="password"&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/form&gt;
  </code></pre>