title: Spring Security OAuth2 dance and get parameters
body: <p>In my Java Spring application I have implemented OAuth2 user authorization via external OAuth2 provider.</p>
  
  <p>At my localhost in order to authenticate user via this external OAuth2 provider I need to go by the following url: <a href="https://127.0.0.1:8443/login/ok" rel="nofollow noreferrer">https://127.0.0.1:8443/login/ok</a> and right after OAuth2 dance  I can get this user authenticated. So far everything is ok.</p>
  
  <p>But when I have some request parameters in my login url, for example uid and level:</p>
  
  <p><a href="https://127.0.0.1:8443/login/ok?uid=45134132&amp;level=3" rel="nofollow noreferrer">https://127.0.0.1:8443/login/ok?uid=45134132&amp;level=3</a></p>
  
  <p>after OAuth2 dance I'm redirected to <a href="https://127.0.0.1:8443/" rel="nofollow noreferrer">https://127.0.0.1:8443/</a> and lose those parameters.</p>
  
  <p>In my Chrome network panel I can see following set of calls:</p>
  
  <ol>
  <li><a href="https://127.0.0.1:8443/login/ok?uid=45134132&amp;level=3" rel="nofollow noreferrer">https://127.0.0.1:8443/login/ok?uid=45134132&amp;level=3</a></li>
  <li><a href="https://connect.ok.ru/oauth/authorize?redirect_uri=https://127.0.0.1:8443/login/ok?uid%3D45134132%26level%3D3&amp;response_type=code&amp;state=AKakq" rel="nofollow noreferrer">https://connect.ok.ru/oauth/authorize?redirect_uri=https://127.0.0.1:8443/login/ok?uid%3D45134132%26level%3D3&amp;response_type=code&amp;state=AKakq</a>....</li>
  <li><a href="https://127.0.0.1:8443/login/ok?uid=45134132&amp;level=3&amp;code=" rel="nofollow noreferrer">https://127.0.0.1:8443/login/ok?uid=45134132&amp;level=3&amp;code=</a>....</li>
  <li><a href="https://127.0.0.1:8443/" rel="nofollow noreferrer">https://127.0.0.1:8443/</a></li>
  </ol>
  
  <p>So I'm losing these parameters after step #3.</p>
  
  <p>Is it possible to configure Spring Security + OAuth2 to pass these parameters to step #4 also ?</p>
  
  <p>This is my config(this a solution based on this answer <a href="https://stackoverflow.com/questions/14282569/spring-security-retaining-url-parameters-on-redirect-to-login/27559666#27559666">Spring Security - Retaining URL parameters on redirect to login</a>) but it doesn't work(<code>AuthenticationProcessingFilterEntryPoint .commence method</code> is not invoked):</p>
  
  <pre><code>    @Override
      public void configure(HttpSecurity http) throws Exception {
          // @formatter:off   
          http
          .headers().frameOptions().disable()
          .and().logout()
          .and().antMatcher("/**").authorizeRequests()
              .antMatchers("/", "/login**", "/index.html", "/home.html").permitAll()
              .anyRequest().authenticated()
          .and().exceptionHandling().authenticationEntryPoint(new AuthenticationProcessingFilterEntryPoint("/"))
          .and().logout().logoutSuccessUrl("/").permitAll()
          .and().csrf().csrfTokenRepository(csrfTokenRepository())
          .and().addFilterAfter(csrfHeaderFilter(), CsrfFilter.class)
          .addFilterBefore(ssoFilter(), BasicAuthenticationFilter.class);
          // @formatter:on
      }
  
      public class AuthenticationProcessingFilterEntryPoint extends LoginUrlAuthenticationEntryPoint {
          public AuthenticationProcessingFilterEntryPoint(String loginFormUrl) {
              super(loginFormUrl);
          }
  
          @Override
          public void commence(HttpServletRequest request, HttpServletResponse response,
                  AuthenticationException authException) throws IOException, ServletException {
              RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
              redirectStrategy.sendRedirect(request, response, getLoginFormUrl() + "?" + request.getQueryString());
          }
      }
  </code></pre>
  
  <p>What can be wrong ?</p>