Webclient - 未经授权刷新 API 密钥
我正在寻找如何正确实现令牌刷新机制的解决方案。 当我们从外部 API 获取 401 或 403 http 状态代码后,应该更改令牌。我想调用登录端点来获取有效令牌,将令牌保存到数据库中,然后使用新令牌重试。
目前,我在反应器和流方面遇到问题。以下是包含核心功能的两个方法。
register方法(外部调用)
public Mono<Void> register(UserConfiguration configuration) {
return externalClient.post()
.uri("http://example.com")
.body(Mono.just(new SomeRequest()), SomeRequest.class)
.header(AUTHORIZATION_TOKEN_HEADER, token)
.exchangeToMono(clientResponse -> handleResponse(clientResponse, Void.class, configuration))
.retryWhen(Retry.max(5)
.doBeforeRetry(retrySignal -> log.info("Unauthorized request"))
.filter(UnauthorizedException.class::isInstance))
.then();
}
handleResponse方法
private <R> Mono<R> handleResponse(ClientResponse clientResponse, Class<R> clazz, Configuration configuration, ) {
if (clientResponse.statusCode().equals(HttpStatus.OK)) {
return clientResponse.bodyToMono(clazz);
} else if (clientResponse.statusCode().equals(HttpStatus.FORBIDDEN) || clientResponse.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
return authenticationService.refreshToken(configuration).flatMap(conf -> Mono.error(new UnauthorizedException()));
} else {
return clientResponse.createException()
.flatMap(Mono::error);
}
}
register
负责调用外部API。 handleResponse
负责处理响应状态,当存在未经授权
响应时,该方法会调用 authenticationService
以获取新令牌并保存将令牌写入数据库(UserConfiguration
)。重试检测异常实例,当令牌出现问题时,它会强制流再次运行。不幸的是,register
方法使用相同的 UserConfiguration
实例,因此其中的值已过时,并且令牌不再有效。
更好的方法是什么?现在我可以看到通过获取 UserConfiguration 来启动流的唯一解决方法,因此重试时我们将获得数据库的最新状态。它似乎有效,但从性能角度来看并不完美。
I'm looking for solution how to properly implement token refreshing mechanism.
A token should be changed after we got 401, or 403 http status code from external API. I'd like to call login endpoint to obtain a valid token, save the token into database and try again with the new token.
Currently, I'm having problem with reactor and streams. Below are two methods that contains core functionality.
register method (external call)
public Mono<Void> register(UserConfiguration configuration) {
return externalClient.post()
.uri("http://example.com")
.body(Mono.just(new SomeRequest()), SomeRequest.class)
.header(AUTHORIZATION_TOKEN_HEADER, token)
.exchangeToMono(clientResponse -> handleResponse(clientResponse, Void.class, configuration))
.retryWhen(Retry.max(5)
.doBeforeRetry(retrySignal -> log.info("Unauthorized request"))
.filter(UnauthorizedException.class::isInstance))
.then();
}
handleResponse method
private <R> Mono<R> handleResponse(ClientResponse clientResponse, Class<R> clazz, Configuration configuration, ) {
if (clientResponse.statusCode().equals(HttpStatus.OK)) {
return clientResponse.bodyToMono(clazz);
} else if (clientResponse.statusCode().equals(HttpStatus.FORBIDDEN) || clientResponse.statusCode().equals(HttpStatus.UNAUTHORIZED)) {
return authenticationService.refreshToken(configuration).flatMap(conf -> Mono.error(new UnauthorizedException()));
} else {
return clientResponse.createException()
.flatMap(Mono::error);
}
}
register
is responsible for calling external API. handleResponse
take care of handling response status, and when there is unauthorized
response, the method invokes authenticationService
in order to obtain a new token and save the token into db (UserConfiguration
). Retry detects an exception instance and when there is problem with a token, it enforces the stream going again. Unfortunately, register
method uses the same instance of UserConfiguration
so values there are outdated as well as token is not valid anymore.
What would be the better approach? Now I can see the only workaround to start a stream from getting UserConfiguration
, so on retry we will have the latest state of db. It seems to work, but it's not perfect from a performance view.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能会迟到,但万一它对其他人有帮助:您可以使用
onErrorResume
方法来过滤 401 和 403 HTTP 响应,在这些情况下,更新令牌,将其保存在数据库中并调用使用新用户配置注册方法。Might be late, but in case it helps someone else: you could use the
onErrorResume
method to filter for 401 and 403 HTTP responses and in those cases, renew the token, save it in the database and call the register method with the new user configuration.