2

I have two spring boot application that communicate with each other from time to time. One of the methods is a Feign client sending a request with @Scheduled.

I'm using a RequestInterceptor to add the Authorization token from restTemplate.

@Component
@Scope("prototype")
public class OAuthInterceptor implements RequestInterceptor {
@Autowired
private OAuth2RestOperations restTemplate;

@Autowired
private AppContext appContext;

@Override
public void apply(RequestTemplate requestTemplate) {
    if (appContext != null) {
        ...
    }

    requestTemplate.header("Authorization", "bearer " + restTemplate.getAccessToken());
}

}

The issue is, appContext is always different from null, event when executed via Scheduled. If executed via a request, it works fine.

How I can configure to check if this interceptor was executed by scheduled or by a user request ?

The error is:

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.

Here is the AppContext that holds some info about an user session, and when is executed via Scheduled I don't need to look into it.

@Component
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="session")
public class AppContext implements Serializable {
...
}
David Magalhães
  • 750
  • 4
  • 10
  • 27

1 Answers1

0

Check my response at Feign and Spring Security 5 - Client Credentials. I think the use of AppContext and OAuth2RestOperations are messing up your code. You should avoid it.

Javi Vazquez
  • 517
  • 6
  • 21