![](/img/trans.png)
[英]How to use Spring Boot feign client for Oauth2 authentication?
[英]OAuth + Feign with Scheduled in Spring Boot
我有兩個不時相互通信的 spring 啟動應用程序。 其中一種方法是 Feign 客戶端使用 @Scheduled 發送請求。
我正在使用 RequestInterceptor 從 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());
}
}
問題是,當通過Scheduled執行時, appContext 總是與 null, event 不同。 如果通過請求執行,它工作正常。
如何配置以檢查此攔截器是按計划執行還是按用戶請求執行?
錯誤是:
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.
這是保存有關用戶會話的一些信息的 AppContext,當通過 Scheduled 執行時,我不需要查看它。
@Component
@Scope(proxyMode=ScopedProxyMode.TARGET_CLASS, value="session")
public class AppContext implements Serializable {
...
}
檢查我在Feign 和 Spring Security 5 - Client Credentials 上的回復。 我認為 AppContext 和 OAuth2RestOperations 的使用弄亂了你的代碼。 你應該避免它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.