问题描述
我有使用OAUTH令牌的Api调用,此auth令牌特定于不同的用户,有效期为24小时。但是有时即使进行api调用时到期时间还没有到,它也会返回无效的令牌。是他们通过上述方式通过获取新的用户访问令牌来重试api调用的一种方式。用户访问令牌也可以通过api调用获得。我在春季启动时使用Java反应式网络客户端。
public Mono<Abc> create(Long customerId,Abc abc) {
return profileRepo.findByCustomerId(customerId)
.map(profile -> refreshTokens(customerId)
.flatMap(tokens ->
client.create(token,getProfile(customerId))))
.orElseThrow(ResourceNotFoundException::new);
}
public Mono<Token> refreshTokens(final Long customerId) {
Token token = service.findByCustomerId(customerId);
if (LocalDateTime.Now().isAfter(token.getExpiryTime())) {
newToken = client.refresh(token);
}
return newToken;
}
Api调用以刷新和创建令牌
public Mono<Token> refresh(final Token token) {
return client.post()
.uri(OAUTH_TOKEN_PATH)
.header(AUTHORIZATION,basicAuth()) // The master token of the service provider
.body(forRefreshToken(new RefreshToken(token.getRefreshToken())))
.retrieve()
.onStatus(HttpStatus::is4xxClientError,response -> response.bodyToMono(String.class)
.flatMap(error -> Mono.error(new ClientResponseException(response.statusCode().value(),response.statusCode(),error))))
.onStatus(HttpStatus::is5xxServerError,error))))
.bodyToMono(Token.class);
}
public Mono<Abc> create(final Token token,Profile pro) {
return client.post()
.uri(PATH_V2)
.header(AUTHORIZATION,token.bearer())
.contentType(APPLICATION_JSON)
.body(fromObject(pro))
.retrieve()
.onStatus(HttpStatus::is4xxClientError,error))))
.bodyToMono(Abc.class);
}
预先感谢, Sameekshya
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)