title: Spring Security OAuth2 login redirect to error page despite being successful
body: <p>As to not reveal confidential information, the provider will be replaced by <code>$PROVIDER</code>.</p>
  
  <p>The authorization works, but instead of redirecting me to the index, it redirects me to <code>/error</code>.</p>
  
  <p><strong>Steps to reproduce</strong></p>
  
  <ol>
  <li>Start the application</li>
  <li>Go on any page, it will redirect me to <code>http://localhost/oauth_login</code> which displays a link <code>login</code>.</li>
  <li>Click on the link <code>login</code> (which links to <code>http://localhost/oauth2/authorization/$PROVIDER</code>)</li>
  <li>Get redirected to <code>http://localhost/error</code></li>
  </ol>
  
  <p>The error page displays the following (I formatted it for readability): </p>
  
  <pre><code>{
      "timestamp":"2018-04-05T14:18:47.720+0000", 
      "status":999, 
      "error":"None", 
      "message":"No message available"
  }
  </code></pre>
  
  <p>This is obviously the same parameters shown on the default <em>Whitelabel Error Page</em>, so really, the only problem is that it's in JSON except of being an HTML page. If I refresh <code>http://localhost/error</code> after, it shows me the normal <em>Whitelabel Error Page</em>.</p>
  
  <p>Now this is where it gets weird, if I try to navigate to <code>http://localhost/</code> after having been redirected to the error page, I am authenticated (the user data is there, so the authorization was successful). Basically, the issue is that I am redirected to <code>http://localhost/error</code> instead of <code>http://localhost/</code>.</p>
  
  <p>Because it is fully functional (aside from the redirection), I will not post the whole security configuration, but I will instead limit it to relevant code:</p>
  
  <p><strong>SecurityConfiguration</strong></p>
  
  <pre><code>@Override
  protected void configure(HttpSecurity httpSecurity) throws Exception {
      httpSecurity
              .csrf().disable()
              .authorizeRequests().anyRequest()
                  .authenticated()
              .and()
                  .oauth2Login()
              .loginPage("/oauth_login")
                  .defaultSuccessUrl("/")
                  .failureUrl("/oauth_login")
                  .permitAll()
              .and()
                  .logout()
                      .logoutUrl("/logout")
                      .logoutSuccessUrl("/oauth_login").permitAll()
      ;
  }
  </code></pre>
  
  <p><strong>Relevant properties</strong></p>
  
  <pre><code>spring.security.oauth2.client.registration.$PROVIDER.redirectUriTemplate=http://localhost/login/oauth2/code/$PROVIDER
  </code></pre>
  
  <p><strong>Relevant information</strong></p>
  
  <ul>
  <li>Once I am authenticated on the website, I can navigate, refresh the page, do whatever I wish (until I log out).</li>
  </ul>
  
  <p><strong>TL;DR</strong> Authorization works but redirected to <code>/error</code> instead of <code>/</code>.</p>