title: Spring 5 Security OAuth2 Login Redirect Loop
body: <p>I want to work with Spotify Web API, but I'm having trouble with Spring Security Configuration. Here are my security dependencies:</p>
  
  <pre class="lang-groovy prettyprint-override"><code> /* springBootVersion = '2.1.2.RELEASE' */
  implementation "org.springframework.security:spring-security-oauth2-client"
  implementation 'org.springframework.security:spring-security-oauth2-jose:5.1.6.RELEASE'
  implementation "org.springframework.security.oauth.boot:spring-security-oauth2-autoconfigure:2.1.7.RELEASE"
  </code></pre>
  
  <p>And here's my security in my <code>application.yml</code> file:</p>
  
  <pre class="lang-yaml prettyprint-override"><code>spring:
    security:
      oauth2:
        client:
          registration:
            spotify:
              provider: spotify-provider
              client-id: &lt;client-id&gt;
              client-secret: &lt;client-secret&gt;
              client-authentication-method: basic
              authorization-grant-type: authorization_code
              redirect-uri: http://localhost:8080/
              scope: &lt;comma delimited scopes&gt;
          provider:
            spotify-provider:
              authorization-uri: https://accounts.spotify.com/authorize
              token-uri: https://accounts.spotify.com/api/token
              user-info-uri: https://api.spotify.com/v1/me
  </code></pre>
  
  <p>My issue is that after I login and get redirected back to my application, it gets stuck on the URL <code>http://localhost:8080/oauth2/authorization/spotify</code> with the error</p>
  
  <blockquote>
    <p>localhost redirected you too many times.</p>
  </blockquote>
  
  <p>Here's what my java security configuration looks like:</p>
  
  <pre><code>@Configuration
  public class SpotifySecurityConfig extends WebSecurityConfigurerAdapter {
  
    @Override
    protected void configure(HttpSecurity http) throws Exception {
      http.authorizeRequests()
              .anyRequest()
              .authenticated()
              .and()
              .oauth2Login();
    }
  }
  </code></pre>