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