diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/config/handler/GlobalErrorWebExceptionHandler.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/config/handler/GlobalErrorWebExceptionHandler.java index 708af930d..fb8afeb61 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/config/handler/GlobalErrorWebExceptionHandler.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/config/handler/GlobalErrorWebExceptionHandler.java @@ -58,8 +58,12 @@ public class GlobalErrorWebExceptionHandler implements ErrorWebExceptionHandler { if(ex instanceof ResponseStatusException) { String reason = ((ResponseStatusException) ex).getReason(); - log.info(reason); - return reason; + if(log.isDebugEnabled()){ + log.debug(ex.getMessage()); + } + if(StrUtil.isNotBlank(reason)) { + return reason; + } } String message = ex.getMessage(); log.error(message, ex); diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/constant/ErrorMessageConstant.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/constant/ErrorMessageConstant.java index b7811d093..917c53a5e 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/constant/ErrorMessageConstant.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/constant/ErrorMessageConstant.java @@ -6,10 +6,10 @@ package com.ruoyi.gateway.constant; * @date 2021-10-18 */ public interface ErrorMessageConstant { - String AK_REQUIRE = "没有找到授权码"; - String CONTENT_TYPE_NOT_SUPPORTED = "请求ContentType不支持"; - String METHOD_NOT_SUPPORTED = "请求Method不支持"; - String RATE_LIMIT = "调用次数过多"; - String RULE_EXPIRED = "规则已过期"; - String RULE_NOT_EXIST = "规则不存在"; + String AK_REQUIRE = "没有找到授权码[%s]"; + String CONTENT_TYPE_NOT_SUPPORTED = "请求ContentType[%s]不支持"; + String METHOD_NOT_SUPPORTED = "请求Method[%s]不支持"; + String RATE_LIMIT = "调用次数[>%d/%s]过多"; + String RULE_EXPIRED = "规则已过期[%s]"; + String RULE_NOT_EXIST = "授权码[%s]对应服务[%s]规则不存在"; } diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/AkRequireException.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/AkRequireException.java index 1e4f47a7c..a51c555db 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/AkRequireException.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/AkRequireException.java @@ -10,8 +10,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant; */ public class AkRequireException extends GatewayException { - public AkRequireException() + public AkRequireException(String ak) { - super(ErrorMessageConstant.AK_REQUIRE); + super(String.format(ErrorMessageConstant.AK_REQUIRE, ak)); } } diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/ContentTypeNotSupportedException.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/ContentTypeNotSupportedException.java index 892343534..4d133af0d 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/ContentTypeNotSupportedException.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/ContentTypeNotSupportedException.java @@ -1,6 +1,7 @@ package com.ruoyi.gateway.exception; import com.ruoyi.gateway.constant.ErrorMessageConstant; +import org.springframework.http.MediaType; /** * 网关异常-请求方法不支持 @@ -10,8 +11,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant; */ public class ContentTypeNotSupportedException extends GatewayException { - public ContentTypeNotSupportedException() + public ContentTypeNotSupportedException(MediaType contentType) { - super(ErrorMessageConstant.CONTENT_TYPE_NOT_SUPPORTED); + super(String.format(ErrorMessageConstant.CONTENT_TYPE_NOT_SUPPORTED, contentType)); } } diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/MethodNotSupportedException.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/MethodNotSupportedException.java index 8f465a784..15fe606ff 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/MethodNotSupportedException.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/MethodNotSupportedException.java @@ -1,6 +1,7 @@ package com.ruoyi.gateway.exception; import com.ruoyi.gateway.constant.ErrorMessageConstant; +import org.springframework.http.HttpMethod; /** * 网关异常-请求方法不支持 @@ -10,8 +11,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant; */ public class MethodNotSupportedException extends GatewayException { - public MethodNotSupportedException() + public MethodNotSupportedException(HttpMethod httpMethod) { - super(ErrorMessageConstant.METHOD_NOT_SUPPORTED); + super(String.format(ErrorMessageConstant.METHOD_NOT_SUPPORTED, httpMethod)); } } diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RateLimitException.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RateLimitException.java index 7b6b3b107..e29728513 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RateLimitException.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RateLimitException.java @@ -3,6 +3,8 @@ package com.ruoyi.gateway.exception; import com.ruoyi.gateway.constant.ErrorMessageConstant; import org.springframework.http.HttpStatus; +import java.util.concurrent.TimeUnit; + /** * 网关异常-限流异常 * @@ -11,8 +13,8 @@ import org.springframework.http.HttpStatus; */ public class RateLimitException extends GatewayException { - public RateLimitException() + public RateLimitException(long limit, TimeUnit timeUnit) { - super(HttpStatus.TOO_MANY_REQUESTS, ErrorMessageConstant.RATE_LIMIT); + super(HttpStatus.TOO_MANY_REQUESTS, String.format(ErrorMessageConstant.RATE_LIMIT, limit, timeUnit.name())); } } diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleExpiredException.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleExpiredException.java index 7b678f85b..d2e1e918e 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleExpiredException.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleExpiredException.java @@ -1,7 +1,10 @@ package com.ruoyi.gateway.exception; +import cn.hutool.core.date.DateUtil; import com.ruoyi.gateway.constant.ErrorMessageConstant; +import java.util.Date; + /** * 网关异常-规则已过期 * @@ -10,8 +13,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant; */ public class RuleExpiredException extends GatewayException { - public RuleExpiredException() + public RuleExpiredException(Date endTime) { - super(ErrorMessageConstant.RULE_EXPIRED); + super(String.format(ErrorMessageConstant.RULE_EXPIRED, DateUtil.formatDateTime(endTime))); } } diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleNotExistException.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleNotExistException.java index 62519a323..15cca6e2f 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleNotExistException.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/exception/RuleNotExistException.java @@ -10,8 +10,8 @@ import com.ruoyi.gateway.constant.ErrorMessageConstant; */ public class RuleNotExistException extends GatewayException { - public RuleNotExistException() + public RuleNotExistException(String ak, String path) { - super(ErrorMessageConstant.RULE_NOT_EXIST); + super(String.format(ErrorMessageConstant.RULE_NOT_EXIST, ak, path)); } } diff --git a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/filter/CustomerGlobalFilter.java b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/filter/CustomerGlobalFilter.java index f4b9a6f0b..1430d6585 100644 --- a/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/filter/CustomerGlobalFilter.java +++ b/ruoyi-extend/ruoyi-isc-gateway/src/main/java/com/ruoyi/gateway/filter/CustomerGlobalFilter.java @@ -69,7 +69,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered { } else if (HttpMethod.POST.equals(httpMethod)) { return handlePostRequest(exchange, chain, route, request, accessKeyName, headerAk); } - throw new MethodNotSupportedException(); + throw new MethodNotSupportedException(httpMethod); } /** @@ -95,7 +95,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered { } else if (MediaType.APPLICATION_FORM_URLENCODED.equals(mediaType)) { return handlePostRequestFormUrlencoded(exchange, route, request, accessKeyName, headerAk, body); } - return Mono.error(ContentTypeNotSupportedException::new); + return Mono.error(() -> new ContentTypeNotSupportedException(mediaType)); }); return GatewayUtils.modifyBody(exchange, chain, modifiedBody); } @@ -116,13 +116,13 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered { String headerAk, String body) { if (StrUtil.isBlank(body)) { - Assert.notBlank(headerAk, AkRequireException::new); + Assert.notBlank(headerAk, () -> new AkRequireException(accessKeyName)); } final List srcParams = Arrays.stream(body.split("&")).map(param -> param.split("=")) .collect(Collectors.toList()); final IscRule rule = handleRule(headerAk, () -> srcParams.stream() .filter(param -> param.length > 0 && accessKeyName.equals(param[0])) - .map(param -> param[1]).collect(Collectors.toList()), route); + .map(param -> param[1]).collect(Collectors.toList()), route, accessKeyName); Supplier> rateLimiterAfterSupplier = () -> { removeParam(headerAk, request, accessKeyName, null); final List params = srcParams.stream().filter(param -> !accessKeyName.equals(param[0])) @@ -160,7 +160,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered { String headerAk, String body) { JSONObject jsonObj = StrUtil.isBlank(body) ? new JSONObject() : JSONUtil.parseObj(body); final IscRule rule = handleRule(headerAk, () -> Collections.singletonList(jsonObj.get(accessKeyName, - String.class, true)), route); + String.class, true)), route, accessKeyName); Supplier> rateLimiterAfterSupplier = () -> { removeParam(headerAk, request, accessKeyName, () -> jsonObj.remove(accessKeyName)); handleHiddenParams(route, jsonObj, (next, map) -> map.set(next.getKey(), next.getValue())); @@ -184,7 +184,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered { private Mono handleGetRequest(ServerWebExchange exchange, GatewayFilterChain chain, Route route, ServerHttpRequest request, String accessKeyName, String headerAk) { final MultiValueMap queryParams = new LinkedMultiValueMap<>(request.getQueryParams()); - final IscRule rule = handleRule(headerAk, () -> queryParams.get(accessKeyName), route); + final IscRule rule = handleRule(headerAk, () -> queryParams.get(accessKeyName), route, accessKeyName); Supplier> rateLimiterAfterSupplier = () -> { removeParam(headerAk, request, accessKeyName, () -> queryParams.remove(accessKeyName)); handleHiddenParams(route, queryParams, (next, map) -> { @@ -216,16 +216,17 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered { * @param headerAk 头部AK * @param valueSupplier valueList 生产者 * @param route 路由信息 + * @param accessKeyName AK键名 * @return AK对应服务规则信息 */ - private IscRule handleRule(String headerAk, Supplier> valueSupplier, Route route) { + private IscRule handleRule(String headerAk, Supplier> valueSupplier, Route route, String accessKeyName) { //获取AK - final String ak = GatewayUtils.getValue(headerAk, valueSupplier, AkRequireException::new); + final String ak = GatewayUtils.getValue(headerAk, valueSupplier, () -> new AkRequireException(accessKeyName)); //获取规则 final IscRule rule = GatewayUtils.getRequiredValue(() -> GatewayUtils.getRule(ak, route.getId()), - RuleNotExistException::new); + () -> new RuleNotExistException(ak, route.getId())); //是否到期 - GatewayUtils.isBefore(rule, RuleExpiredException::new); + GatewayUtils.isBefore(rule, () -> new RuleExpiredException(rule.getExpire())); //设置AK 到ID 为了传参方便 rule.setId(ak); return rule; @@ -307,7 +308,7 @@ public class CustomerGlobalFilter implements GlobalFilter, Ordered { } return rateLimiter(exchange, rule, route, index + 1, rateLimiterAfterSupplier); } - return Mono.error(RateLimitException::new); + return Mono.error(() -> new RateLimitException(limit, timeUnit)); }); } }