【安眼】拦截器
This commit is contained in:
+2
-15
@@ -2,7 +2,6 @@ package com.renkang.restaurant.config;
|
||||
|
||||
import com.renkang.anyan.client.AnYanClient;
|
||||
import com.renkang.anyan.config.AnYanProperties;
|
||||
import com.renkang.anyan.enums.AnYanApiEnum;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.jeecg.common.exception.ExceptionAssertsUtil;
|
||||
@@ -22,7 +21,6 @@ import java.nio.charset.StandardCharsets;
|
||||
import java.time.Duration;
|
||||
import java.time.temporal.ChronoUnit;
|
||||
import java.util.Arrays;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* @Description: AnYanClient xj安眼系统接口调用客户端
|
||||
@@ -55,27 +53,16 @@ public class AnYanRestTemplateConfig {
|
||||
* 自定义拦截器处理接口鉴权失败, 配合spring retry使用从而避免因token失效导致的接口调用失败
|
||||
*/
|
||||
public ClientHttpRequestInterceptor authInterceptor() {
|
||||
// 添加重试次数限制
|
||||
AtomicInteger retryCount = new AtomicInteger(0);
|
||||
int maxRetries = 5;
|
||||
return (httpRequest, bytes, clientHttpRequestExecution) -> {
|
||||
ClientHttpResponse execute;
|
||||
try {
|
||||
// 获取请求URL
|
||||
String url = httpRequest.getURI().toString();
|
||||
// 排除特定接口不执行拦截逻辑
|
||||
boolean shouldSkip = url.contains(AnYanApiEnum.QUERY_AUTH_TOKEN.getMethodPath());
|
||||
execute = clientHttpRequestExecution.execute(httpRequest, bytes);
|
||||
if (shouldSkip) {
|
||||
return execute;
|
||||
}
|
||||
HttpStatus httpStatus = execute.getStatusCode();
|
||||
if (Arrays.asList(HttpStatus.UNAUTHORIZED, HttpStatus.FORBIDDEN).contains(httpStatus)
|
||||
&& retryCount.getAndIncrement() < maxRetries) {
|
||||
if (Arrays.asList(HttpStatus.UNAUTHORIZED, HttpStatus.FORBIDDEN).contains(httpStatus)) {
|
||||
SpringContextUtils.getBean("anYanClientExecutor", AnYanClient.class).refreshToken();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.error("---------安眼平台API调用异常-------e{}", e.getMessage(), e);
|
||||
log.error("---------安眼平台API调用异常-------e{},接口:{}", e.getMessage(), httpRequest.getURI(), e);
|
||||
ExceptionAssertsUtil.fail("安眼平台API调用失败");
|
||||
throw e;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user