title: Spring oauth2 dont redirect to original url
body: <p>I tried to configure the authorization code flow as a client. As far the flow is working. I get a redirect to the login page. The oauth2 server gives me an auth code and I can exchange the code for an access token.</p>
  
  <p>But I can't get the last step right: get back to the original resource.
  This is my SecurityConfig:</p>
  
  <pre><code>@Configuration
  @EnableWebSecurity
  @EnableOAuth2Client
  public class SecureConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      OAuth2ClientContext oauth2ClientContext;
  
      @Value("${openId.userinfo}")
      private String userInfoUri;
  
      @Value("${openId.clientId}")
      private String clientId;
  
      @Value("${openId.clientSecret}")
      private String clientSecret;
  
      @Value("${openId.accessTokenUri}")
      private String accessTokenUri;
  
      @Value("${openId.userAuthorizationUri}")
      private String userAuthorizationUri;
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
                  http.csrf().disable()
                  .addFilterAfter(ssoFilter(), BasicAuthenticationFilter.class);
      }
  
      private OAuth2ClientAuthenticationProcessingFilter ssoFilter() {
          OAuth2ClientAuthenticationProcessingFilter openIDFilter = new OAuth2ClientAuthenticationProcessingFilter("/resource/**");
          openIDFilter.setRestTemplate(restTemplate());
          UserInfoTokenServices tokenServices = new UserInfoTokenServices(userInfoUri, clientId);
          tokenServices.setRestTemplate(restTemplate());
          openIDFilter.setTokenServices(tokenServices);
          return openIDFilter;
      }
  
      @Bean
      @Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
      public OAuth2RestTemplate restTemplate() {
          return new OAuth2RestTemplate(protectedResourceDetails(), oauth2ClientContext);
      }
  
      @Bean
      public FilterRegistrationBean oauth2ClientFilterRegistration(
              OAuth2ClientContextFilter filter) {
          FilterRegistrationBean registration = new FilterRegistrationBean();
          registration.setFilter(filter);
          registration.setOrder(-100);
          return registration;
      }
  
      @Bean
      public OAuth2ProtectedResourceDetails protectedResourceDetails() {
          AuthorizationCodeResourceDetails details = new AuthorizationCodeResourceDetails();
          details.setClientId(clientId);
          details.setClientSecret(clientSecret);
          details.setAccessTokenUri(accessTokenUri);
          details.setUserAuthorizationUri(userAuthorizationUri);
          details.setScope(Arrays.asList("read"));
          details.setUseCurrentUri(true);
          return details;
      }
  }
  </code></pre>
  
  <p>And this is my controller:</p>
  
  <pre><code>@Controller
  @RequestMapping("/resource")
  public class TestController {
  
      @RequestMapping(value = "/test",  method = {RequestMethod.GET, RequestMethod.POST})
      @ResponseStatus(code = HttpStatus.OK)
      public void test(){
          System.out.println("hello world");
      }
  }
  </code></pre>
  
  <p>In the last step spring redirect me to my base url:
  <a href="https://i.stack.imgur.com/Ubvpq.png" rel="noreferrer"><img src="https://i.stack.imgur.com/Ubvpq.png" alt="enter image description here"></a></p>
  
  <p>I found <a href="http://forum.spring.io/forum/spring-projects/security/119246-redirecting-to-original-unauthenticated-page-after-spring-security-openid-authentic" rel="noreferrer">this forum post</a></p>
  
  <p>It suggests saving the request in the <code>RequestCache</code>. But this post is about 6 years old, maybe spring offers a more elegant solution in the meantime?</p>
  
  <p>EDIT:
  This are my dependencies:</p>
  
  <pre><code>&lt;dependencies&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-dependencies&lt;/artifactId&gt;
          &lt;version&gt;1.5.2.RELEASE&lt;/version&gt;
          &lt;type&gt;pom&lt;/type&gt;
          &lt;scope&gt;import&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-security&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-test&lt;/artifactId&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.springframework.security.oauth&lt;/groupId&gt;
          &lt;artifactId&gt;spring-security-oauth2&lt;/artifactId&gt;
      &lt;/dependency&gt;
  &lt;/dependencies&gt;
  </code></pre>