title: Custom login form. Configure Spring security to get a JSON response
body: <p>I have a simple application which is splitted into 2 parts : </p>
  
  <ul>
  <li>A backend which exposes REST services with Spring-boot / Spring-security</li>
  <li>A frontend which contains only static files.</li>
  </ul>
  
  <p>The requests are received by a nginx server which listens on port 80. </p>
  
  <ul>
  <li>If the request URL begins with /api/, the request is redirected to the backend. </li>
  <li>Else, the request is handled by nginx which serves the static files.</li>
  </ul>
  
  <p>I created a custom login form (in the frontend part) and I am trying to configure the Spring-boot server.</p>
  
  <p>There are a lot of examples where I can see how to define a "login success" url and a "login error" url but I do not want Spring-security to redirect the user. I want Spring-security to answer with HTTP 200 if the login succeeded or HTTP 40x is the login failed.</p>
  
  <p>In other words : I want the backend to only answer with JSON, never HTML.</p>
  
  <p>Up to now, when I submit the login form, the request is redirected and I get the default Spring login form as an answer.</p>
  
  <p>I tried to use <code>.formLogin().loginProcessingUrl("/login");</code> instead of <code>loginPage("")</code> :</p>
  
  <pre><code>@Configuration
  @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
  protected static class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.inMemoryAuthentication()
        .withUser("user").password("password").roles("ADMIN");
    }
  
    @Override
    public void configure(HttpSecurity http) throws Exception {
      http
        .authorizeRequests()
          .anyRequest().authenticated()
          .and()
        .formLogin()
          .loginProcessingUrl("/login");
  </code></pre>