具有OAuth2授权的Spring WebClient

Luk*_*szS 5 java spring spring-security oauth-2.0 spring-webflux

当使用旧的Spring的RestTemplate与OAuth授权后面的某些API集成时,我正在做某种事情:

ClientCredentialsResourceDetails resourceDetails = new ClientCredentialsResourceDetails();resourceDetails.setClientId(oauthClientId);
resourceDetails.setClientSecret(oauthClientSecret);
resourceDetails.setAccessTokenUri(accessTokenUri);
// and we got the restTemplate:
OAuth2RestTemplate client = new OAuth2RestTemplate(resourceDetails);
Run Code Online (Sandbox Code Playgroud)

然后GET就像:

client.getForEntity(restApiUri, MyEntity.class);
Run Code Online (Sandbox Code Playgroud)

但它阻止了:(

作为一种解决方法,我使用Mono.fromCallable(...)包装旧的RestTempate.我还尝试使用WebClient做一些自定义客户端,它接受令牌并授权用户,然后再次调用RestApi.但由于令牌可能会过期而且我不得不实施续订流程,因此不方便.OAuth中还可能存在更多我不知道的陷阱.

我如何使用Spring web-flux和WebClient以及Mono/Flux反应对象?

zer*_*ref 1

也许会对你有所帮助。有关配置 Web 客户端以按照您提到的方式使用它的中型文章。