Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to invoke RestClient with OAuth2 in Scheduled Task #1

Closed
iopar opened this issue Feb 16, 2024 · 3 comments
Closed

How to invoke RestClient with OAuth2 in Scheduled Task #1

iopar opened this issue Feb 16, 2024 · 3 comments
Assignees

Comments

@iopar
Copy link

iopar commented Feb 16, 2024

The examples you provided work perfectly when I use the RestClient from a Controller.

I am encountering an issue when attempting to make HTTP requests using the RestClient within a CronService that is annotated with @Scheduled. The goal is to periodically call an external service that requires OAuth2 authentication. However, the execution of the scheduled task results in an IllegalArgumentException, specifically stating that the servletRequest cannot be null.

java.lang.IllegalArgumentException: servletRequest cannot be null
	at org.springframework.util.Assert.notNull(Assert.java:172)
	at org.springframework.security.oauth2.client.web.DefaultOAuth2AuthorizedClientManager.authorize(DefaultOAuth2AuthorizedClientManager.java:144)
	at xx.xxx.xxxx.xxxxxxx.xxxxxxx.service.CronService.performMyTask(CronService.java:95)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.runInternal(ScheduledMethodRunnable.java:130)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.lambda$run$2(ScheduledMethodRunnable.java:124)
	at io.micrometer.observation.Observation.observe(Observation.java:499)
	at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:124)
	at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
	at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:96)
	at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
	at java.base/java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)

Could you include an example when a web request context is not available?

@mjeffrey
Copy link
Owner

merged

@mjeffrey
Copy link
Owner

BTW this POC was related to this. maybe you saw it?

spring-projects/spring-security#13588

@iopar
Copy link
Author

iopar commented Feb 17, 2024

Yes, I came across the issue first, and then found your PoC, which was really helpful. It looks like we are facing similar issues with the RestClient and OAuth configuration. Thank you for sharing your work.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants