title: Redirect to a specific URL after successful anonymous auth via Spring Security OAuth2 client
body: <p>I use Spring Security's OAuth2 client auth with code grant type to do anonymous authentication.</p>
  
  <p>The built-in <code>OAuth2AuthorizationCodeGrantFilter</code> does a redirect after a successful auth of an anonymous principal.
  It redirects to a URL obtained either via a request stored in a <code>RequestCache</code>, or uses the default url (<code>oauth2/code/{registrationId}</code>) stripped from query params.</p>
  
  <p>The <code>OAuth2AuthorizationCodeGrantFilter</code>'s code in question:</p>
  
  <pre class="lang-java prettyprint-override"><code>String redirectUrl = authorizationResponse.getRedirectUri();
  SavedRequest savedRequest = this.requestCache.getRequest(request, response);
  if (savedRequest != null) {
      redirectUrl = savedRequest.getRedirectUrl();
      this.requestCache.removeRequest(request, response);
  }
  this.redirectStrategy.sendRedirect(request, response, redirectUrl);
  </code></pre>
  
  <p>I can leverage the request cache to store a request for the redirection. But the <code>RequestCache</code>'s interface doesn't let me specify arbitrary URL for the redirection, only use an existing (immutable) HttpServletRequest.</p>
  
  <p>I need to do a redirect to a specific URL based on some business logic. How can I force an arbitrary redirect URL?</p>