title: Return RESTful/json response instead of login form in Spring boot OAUTH2
body: <p>I'm struggling to return a json response when the user is nog logged in, instead of returning a html login form. </p>
  
  <p>The application is only using @RestController's and I do not want any web support. </p>
  
  <pre><code>    http
          .authorizeRequests()
                  .anyRequest().authenticated()
                  .and()
                  .formLogin()
                  .and()
                  .httpBasic().disable();
  </code></pre>
  
  <p>When I do a get request using the browser or postman, I receive back the default spring HTML login form.</p>
  
  <pre><code>security.basic.enabled=true
  </code></pre>
  
  <p>I'm using OATH2, and when I request a token it works great and I get a response like this:</p>
  
  <pre><code>{
    "access_token": "227803d1-fe94-4e31-b496-3a03bafb9479",
    "token_type": "bearer",
    "refresh_token": "322f4ee9-4c48-4e06-a129-f4b6af93d664",
    "expires_in": 43199,
    "scope": "read write"
  }
  </code></pre>
  
  <p>Somehow, it works correct in this example:</p>
  
  <p><a href="https://github.com/royclarkson/spring-rest-service-oauth" rel="nofollow noreferrer">https://github.com/royclarkson/spring-rest-service-oauth</a></p>
  
  <p>And when I'm not authorized, I want back something like this:</p>
  
  <pre><code>{
    "error": "unauthorized",
    "error_description": "An Authentication object was not found in the SecurityContext"
  }
  </code></pre>
  
  <p>But I'm getting a login form, and I want the response above. What am I doing wrong? I litterally can't uderstand why it works in the GitHub example, and not in my case. Is there a way to completely disable the login form, and force the json response?</p>