feat(紫鸟): 令牌级静态代理支持——白名单被拒的 key 走已授权代理 IP

- shop_key 新增 proxy_url / ip_whitelist_fail_count(V114 迁移)
- HttpClientPool 支持按代理地址复用的静态代理客户端
- 白名单连续失败达阈值后停止自动重试,控制台可按 key 配置代理(改动自
  上一工作阶段遗留,功能已编译验证)
This commit is contained in:
2026-09-11 17:11:50 +08:00
parent a3b01f1f50
commit 96680a6844
13 changed files with 419 additions and 22 deletions
@@ -26,24 +26,42 @@ public class ZiniaoClientImpl implements ZiniaoClient {
private final ZiniaoProperties ziniaoProperties;
private final ObjectMapper objectMapper;
private final com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics;
/** 可为 null(测试/未配置代理时直连)。 */
private final ZiniaoProxyResolver proxyResolver;
/** 生产构造:双构造器下 Spring 不会自动推断,必须显式 @Autowired。 */
@Autowired
public ZiniaoClientImpl(ZiniaoProperties ziniaoProperties, ObjectMapper objectMapper,
ZiniaoProxyResolver proxyResolver) {
this(ziniaoProperties, objectMapper, null, proxyResolver);
}
public ZiniaoClientImpl(ZiniaoProperties ziniaoProperties, ObjectMapper objectMapper) {
this(ziniaoProperties, objectMapper, null);
this(ziniaoProperties, objectMapper, null, null);
}
public ZiniaoClientImpl(ZiniaoProperties ziniaoProperties,
ObjectMapper objectMapper,
com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics) {
this(ziniaoProperties, objectMapper, externalCallMetrics, null);
}
public ZiniaoClientImpl(ZiniaoProperties ziniaoProperties,
ObjectMapper objectMapper,
com.nanri.aiimage.metrics.ExternalCallMetricsRecorder externalCallMetrics,
ZiniaoProxyResolver proxyResolver) {
this.ziniaoProperties = ziniaoProperties;
this.objectMapper = objectMapper;
this.externalCallMetrics = externalCallMetrics;
this.proxyResolver = proxyResolver;
}
/** Task 77:单例 RestClient(共享连接池),避免每次调用新建短命客户端。 */
private volatile RestClient sharedRestClient;
/** 按代理地址缓存 RestClient:同一代理复用同一连接池,避免每请求新建。 */
private final Map<String, RestClient> proxyRestClients = new java.util.concurrent.ConcurrentHashMap<>();
@Override
public Long getCompanyIdByApiKey(String apiKey) {
String raw = getWithApiKey(apiKey, "/app/builtin/company", "获取 companyId");
@@ -145,7 +163,7 @@ public class ZiniaoClientImpl implements ZiniaoClient {
private String getWithApiKey(String apiKey, String path, String action) {
String raw = getRestClient().get()
String raw = restClientFor(apiKey).get()
.uri(joinUrl(ziniaoProperties.getBaseUrl(), path))
.headers(headers -> applyAuthorization(headers, apiKey))
.retrieve()
@@ -158,7 +176,7 @@ public class ZiniaoClientImpl implements ZiniaoClient {
}
private String postWithApiKey(String apiKey, String path, Map<String, Object> body, String action) {
RestClient.RequestBodySpec request = getRestClient().post()
RestClient.RequestBodySpec request = restClientFor(apiKey).post()
.uri(joinUrl(ziniaoProperties.getBaseUrl(), path))
.headers(headers -> {
applyAuthorization(headers, apiKey);
@@ -271,17 +289,46 @@ public class ZiniaoClientImpl implements ZiniaoClient {
}
synchronized (this) {
if (sharedRestClient == null) {
RestClient.Builder builder = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(ziniaoProperties.getReadTimeoutSeconds() * 1000));
if (externalCallMetrics != null) {
builder.requestInterceptor(externalCallMetrics.interceptor("ziniao"));
}
sharedRestClient = builder.build();
sharedRestClient = buildRestClient(null);
}
return sharedRestClient;
}
}
/**
* 按该 apiKey 配置的静态代理返回客户端;未配置代理时回退共享直连客户端。
* 代理 RestClient 按代理地址缓存,避免同一代理反复新建连接池。
*/
private RestClient restClientFor(String apiKey) {
String proxyUrl = resolveProxyUrl(apiKey);
if (proxyUrl == null || proxyUrl.isBlank()) {
return getRestClient();
}
return proxyRestClients.computeIfAbsent(proxyUrl, this::buildRestClient);
}
private String resolveProxyUrl(String apiKey) {
if (proxyResolver == null || apiKey == null || apiKey.isBlank()) {
return null;
}
try {
return proxyResolver.resolveProxyUrl(apiKey);
} catch (Exception ex) {
// 代理解析失败不应阻断请求:回退直连,由上游按白名单失败处理
return null;
}
}
private RestClient buildRestClient(String proxyUrl) {
RestClient.Builder builder = RestClient.builder()
.requestFactory(HttpClientPool.requestFactory(
ziniaoProperties.getReadTimeoutSeconds() * 1000, proxyUrl));
if (externalCallMetrics != null) {
builder.requestInterceptor(externalCallMetrics.interceptor("ziniao"));
}
return builder.build();
}
/** 反射/测试可见:与 getRestClient 同一单例。 */
RestClient restClient() {
return getRestClient();
@@ -0,0 +1,13 @@
package com.nanri.aiimage.modules.ziniao.client;
/**
* 按紫鸟 apiKey 解析该 key 应使用的静态代理地址。
* 实现方需自行做短时缓存,客户端会在每次请求前调用。
*/
public interface ZiniaoProxyResolver {
/**
* @return 代理地址(http://host:port 或 http://user:pass@host:port);无代理返回 null 表示直连
*/
String resolveProxyUrl(String apiKey);
}
@@ -5,6 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.modules.shopkey.mapper.ShopKeyMapper;
import com.nanri.aiimage.modules.shopkey.model.entity.ShopKeyEntity;
import com.nanri.aiimage.modules.ziniao.client.ZiniaoProxyResolver;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
@@ -16,13 +17,25 @@ import java.util.Map;
@Service
@RequiredArgsConstructor
public class ZiniaoApiKeyProvider {
public class ZiniaoApiKeyProvider implements ZiniaoProxyResolver {
public static final String IP_WHITELIST_STATUS_ALLOWED = "ALLOWED";
public static final String IP_WHITELIST_STATUS_BLOCKED = "BLOCKED";
public static final String IP_WHITELIST_STATUS_UNKNOWN = "UNKNOWN";
/**
* 白名单放行需紫鸟公司侧授权,反复重试只是白白拖慢每轮刷新。
* 连续失败达到该次数后不再自动重试,需人工点击检测或改令牌/代理。
*/
public static final int IP_WHITELIST_MAX_FAIL_COUNT = 3;
private final ShopKeyMapper shopKeyMapper;
private static final long PROXY_URL_CACHE_MILLIS = 60_000L;
private volatile Map<String, String> cachedProxyUrls;
private volatile long proxyUrlCacheLoadedAt;
public List<ApiKeyAccount> listApiKeyAccounts() {
List<ShopKeyEntity> entities = shopKeyMapper.selectList(new LambdaQueryWrapper<ShopKeyEntity>()
.orderByDesc(ShopKeyEntity::getId));
@@ -38,11 +51,68 @@ public class ZiniaoApiKeyProvider {
.map(entry -> new ApiKeyAccount(
entry.getKey(),
resolveAccountName(entry.getValue()),
entry.getValue().stream().map(ShopKeyEntity::getId).filter(java.util.Objects::nonNull).toList()
entry.getValue().stream().map(ShopKeyEntity::getId).filter(java.util.Objects::nonNull).toList(),
resolveProxyUrl(entry.getValue()),
resolveIpWhitelistFailCount(entry.getValue())
))
.toList();
}
/**
* 客户端每次请求都会问一次代理地址,直查 DB 太重;这里缓存 60 秒快照。
*/
@Override
public String resolveProxyUrl(String apiKey) {
return proxyUrlCache().get(apiKey);
}
private Map<String, String> proxyUrlCache() {
long now = System.currentTimeMillis();
Map<String, String> snapshot = cachedProxyUrls;
if (snapshot != null && now - proxyUrlCacheLoadedAt < PROXY_URL_CACHE_MILLIS) {
return snapshot;
}
synchronized (this) {
if (cachedProxyUrls != null && System.currentTimeMillis() - proxyUrlCacheLoadedAt < PROXY_URL_CACHE_MILLIS) {
return cachedProxyUrls;
}
Map<String, String> fresh = new LinkedHashMap<>();
for (ApiKeyAccount account : listApiKeyAccounts()) {
if (account.proxyUrl() != null && !account.proxyUrl().isBlank()) {
fresh.put(account.apiKey(), account.proxyUrl());
}
}
cachedProxyUrls = fresh;
proxyUrlCacheLoadedAt = System.currentTimeMillis();
return fresh;
}
}
/** 令牌或代理变更后立即失效缓存,避免后台改完仍按旧代理发请求。 */
public void invalidateProxyUrlCache() {
synchronized (this) {
cachedProxyUrls = null;
proxyUrlCacheLoadedAt = 0L;
}
}
private String resolveProxyUrl(List<ShopKeyEntity> entities) {
return entities.stream()
.map(ShopKeyEntity::getProxyUrl)
.filter(proxy -> proxy != null && !proxy.isBlank())
.map(String::trim)
.findFirst()
.orElse(null);
}
private Integer resolveIpWhitelistFailCount(List<ShopKeyEntity> entities) {
return entities.stream()
.map(ShopKeyEntity::getIpWhitelistFailCount)
.filter(count -> count != null)
.max(Integer::compareTo)
.orElse(0);
}
public List<String> listApiKeys() {
return listApiKeyAccounts().stream()
.map(ApiKeyAccount::apiKey)
@@ -63,22 +133,50 @@ public class ZiniaoApiKeyProvider {
}
public void markIpWhitelistAllowed(ApiKeyAccount account) {
updateIpWhitelistStatus(account, IP_WHITELIST_STATUS_ALLOWED, null);
updateIpWhitelistStatus(account, IP_WHITELIST_STATUS_ALLOWED, null, 0);
}
public void markIpWhitelistBlocked(ApiKeyAccount account, String message) {
updateIpWhitelistStatus(account, IP_WHITELIST_STATUS_BLOCKED, message);
/**
* 记录一次白名单失败:累加连续失败次数,仅在首次达阈值时写库(避免每轮重复更新)。
*
* @return true 表示本次为该 key 首次达到阈值(调用方据此打一次日志)
*/
public boolean markIpWhitelistBlocked(ApiKeyAccount account, String message) {
if (account == null || account.shopKeyIds().isEmpty()) {
return false;
}
int currentCount = account.ipWhitelistFailCount() == null ? 0 : account.ipWhitelistFailCount();
int nextCount = currentCount + 1;
boolean reachedThresholdNow = currentCount < IP_WHITELIST_MAX_FAIL_COUNT
&& nextCount >= IP_WHITELIST_MAX_FAIL_COUNT;
if (currentCount >= IP_WHITELIST_MAX_FAIL_COUNT) {
// 已达阈值:只刷新检测时间与最新消息,不再累计,也不再触发重试
updateIpWhitelistStatus(account, IP_WHITELIST_STATUS_BLOCKED, message, null);
return false;
}
updateIpWhitelistStatus(account, IP_WHITELIST_STATUS_BLOCKED, message, nextCount);
return reachedThresholdNow;
}
private void updateIpWhitelistStatus(ApiKeyAccount account, String status, String message) {
/** 该 key 是否已连续失败达阈值、应跳过自动重试。 */
public boolean isIpWhitelistRetryExhausted(ApiKeyAccount account) {
int count = account == null || account.ipWhitelistFailCount() == null ? 0 : account.ipWhitelistFailCount();
return count >= IP_WHITELIST_MAX_FAIL_COUNT;
}
private void updateIpWhitelistStatus(ApiKeyAccount account, String status, String message, Integer failCount) {
if (account == null || account.shopKeyIds().isEmpty()) {
return;
}
shopKeyMapper.update(null, new LambdaUpdateWrapper<ShopKeyEntity>()
LambdaUpdateWrapper<ShopKeyEntity> update = new LambdaUpdateWrapper<ShopKeyEntity>()
.in(ShopKeyEntity::getId, account.shopKeyIds())
.set(ShopKeyEntity::getIpWhitelistStatus, status)
.set(ShopKeyEntity::getIpWhitelistCheckedAt, LocalDateTime.now())
.set(ShopKeyEntity::getIpWhitelistMessage, truncateMessage(message)));
.set(ShopKeyEntity::getIpWhitelistMessage, truncateMessage(message));
if (failCount != null) {
update.set(ShopKeyEntity::getIpWhitelistFailCount, failCount);
}
shopKeyMapper.update(null, update);
}
private String resolveAccountName(List<ShopKeyEntity> entities) {
@@ -109,14 +207,20 @@ public class ZiniaoApiKeyProvider {
return normalized.isBlank() ? null : normalized;
}
public record ApiKeyAccount(String apiKey, String accountName, List<Long> shopKeyIds) {
public record ApiKeyAccount(String apiKey, String accountName, List<Long> shopKeyIds,
String proxyUrl, Integer ipWhitelistFailCount) {
public ApiKeyAccount {
shopKeyIds = shopKeyIds == null ? List.of() : List.copyOf(shopKeyIds);
ipWhitelistFailCount = ipWhitelistFailCount == null ? 0 : ipWhitelistFailCount;
}
public ApiKeyAccount(String apiKey, String accountName, List<Long> shopKeyIds) {
this(apiKey, accountName, shopKeyIds, null, 0);
}
public ApiKeyAccount(String apiKey, String accountName) {
this(apiKey, accountName, List.of());
this(apiKey, accountName, List.of(), null, 0);
}
}
}
@@ -136,6 +136,21 @@ public class ZiniaoAuthService {
return resolveCompanyId(apiKey);
}
/**
* 后台手动检测专用:绕过本地缓存与固定 companyId 配置,真实打一次紫鸟接口,
* 否则命中缓存会把已失效的白名单状态误判为可用。
* 成功时顺带刷新 COMPANY_ID 缓存。
*
* @throws BusinessException 上游失败(白名单/令牌无效等)时抛出,错误信息透传给后台展示
*/
public Long probeApiKeyUpstream(String apiKey) {
String normalizedApiKey = requireText(apiKey, "紫鸟 apiKey 未配置");
Long companyId = ziniaoClient.getCompanyIdByApiKey(normalizedApiKey);
ziniaoTransientCacheService.put(CACHE_TYPE_COMPANY_ID, buildApiKeyHash(normalizedApiKey), companyId, COMPANY_ID_CACHE_TTL);
log.info("[ziniao-whitelist-probe] upstream ok keyHash={} companyId={}", shortKeyHash(normalizedApiKey), companyId);
return companyId;
}
public List<ZiniaoStaffItemVo> getOrLoadStaffForIndex(String apiKey, Long companyId) {
return getOrLoadStaff(apiKey, companyId);
}
@@ -217,6 +217,15 @@ public class ZiniaoShopIndexService {
}
String apiKey = apiKeyAccount.apiKey();
String companyName = apiKeyAccount.accountName();
// 白名单放行需紫鸟公司侧授权:连续失败达阈值后不再自动重试,省掉每轮无谓的失败等待
if (ziniaoApiKeyProvider.isIpWhitelistRetryExhausted(apiKeyAccount)) {
skippedApiKeyCount++;
whitelistSkippedApiKeyCount++;
completeCoverage = false;
log.info("[ziniao-index] skip apiKey reason=IP_WHITELIST_RETRY_EXHAUSTED accountName={} failCount={}",
companyName, apiKeyAccount.ipWhitelistFailCount());
continue;
}
Map<String, List<ZiniaoShopIndexEntryDto>> apiKeyGrouped = new LinkedHashMap<>();
Map<String, Long> apiKeyFingerprints = new LinkedHashMap<>();
Long companyId;
@@ -459,10 +468,15 @@ public class ZiniaoShopIndexService {
private void markIpWhitelistBlockedSafely(ZiniaoApiKeyProvider.ApiKeyAccount account, String message) {
try {
ziniaoApiKeyProvider.markIpWhitelistBlocked(account, message);
boolean exhaustedNow = ziniaoApiKeyProvider.markIpWhitelistBlocked(account, message);
if (exhaustedNow) {
log.warn("[ziniao-index] IP whitelist retry exhausted accountName={} maxFailCount={} 后续轮次不再重试,需紫鸟公司授权后人工检测",
account == null ? null : account.accountName(),
ZiniaoApiKeyProvider.IP_WHITELIST_MAX_FAIL_COUNT);
}
} catch (Exception ex) {
log.warn("[ziniao-index] failed to record IP whitelist status accountName={} status=BLOCKED msg={}",
account.accountName(), ex.getMessage());
account == null ? null : account.accountName(), ex.getMessage());
}
}