2

I am trying to request an object via oauth2 authentification with spring security and then try to use those credentials in my service, which is supposed to request data in fixed intervals.

The SleepController's purpose is to initially get data and as a side effect cache the important data for the service below.

@RestController
public class SleepController {
  @Autowired
  OAuth2RestTemplate oAuth2RestTemplate;

  public static OAuth2RestTemplate currentOAuth2RestTemplate;

  @RequestMapping("/sleep")
  public OverallSleep getSleep(String date) {

      if(date != null) {
          OverallSleep overallSleep = null;
          try {
              overallSleep = oAuthRestTemplate.getForObject("https://api.fitbit.com/1.2/user/-/sleep/date/" + date + ".json", OverallSleep.class);
              currentOAuth2RestTemplate = oAuth2RestTemplate;

          } catch (Exception e) {
              e.printStackTrace();
          }
          OverallSleep tmp = overallSleepDao.findOverallSleepByToday();
          return overallSleep;
      }
      return null;
  }
}

The BeatService has a scheduled function that tries to request data from the server without user interaction.

@Service
public class BeatService {

    private static final long INTERVAL = 60_000L;
    private static final long INITIAL_DELAY = 3_000L;

    public BeatService() { }

    @Scheduled(fixedRate = INTERVAL, initialDelay = INITIAL_DELAY)
    public void beat() {
        if(SleepController.currentOAuth2RestTemplate != null) {
            OverallSleep overallSleep = null;
            try {
                overallSleep = SleepController.currentOAuth2RestTemplate.getForObject("https://api.fitbit.com/1.2/user/-/sleep/date/" + "2018-02-11" + ".json", OverallSleep.class);
                overallSleepDao.save(overallSleep);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

Sadly, my approach of statically storing the oauthRestTemplate and trying to use that in my scheduled service does not work. This Exception is thrown

org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'scopedTarget.oauth2ClientContext': 
Scope 'session' is not active for the current thread; consider defining a 
scoped proxy for this bean if you intend to refer to it from a singleton; 
nested exception is java.lang.IllegalStateException: No thread-bound request 
found: Are you referring to request attributes outside of an actual web 
request, or processing a request outside of the originally receiving thread? 
If you are actually operating within a web request and still receive this 
message, your code is probably running outside of 
DispatcherServlet/DispatcherPortlet: In this case, use 
RequestContextListener or RequestContextFilter to expose the current 
request.
at [...]
Caused by: java.lang.IllegalStateException: 
No thread-bound request found: Are you referring to request attributes 
outside of an actual web request, or processing a request outside of the 
originally receiving thread? If you are actually operating within a web 
request and still receive this message, your code is probably running     
outside of DispatcherServlet/DispatcherPortlet: In this case, use 
RequestContextListener or RequestContextFilter to expose the current 
request.

Is there an appriopriate or right way to reuse the credentials from a prior authentification in oauth2 in Spring? I found a post sadly it doesn't work and actually make the controller crash so I can't test the service at all. If needed, I can add other classes or what @Configuration I use.

ByteBiter
  • 116
  • 1
  • 7

0 Answers0