From 9d7b44d1dd85ba3561cbe602874397e502315e9d Mon Sep 17 00:00:00 2001 From: zhangsz Date: Wed, 22 Jan 2025 14:07:02 +0800 Subject: [PATCH] feat: gateway support i18n message --- .../src/main/resources/i18n/messages.properties | 10 ++++++++++ .../main/resources/i18n/messages_en_US.properties | 10 ++++++++++ .../main/resources/i18n/messages_zh_CN.properties | 10 ++++++++++ .../java/org/wfc/gateway/WfcGatewayApplication.java | 2 +- .../java/org/wfc/gateway/config/GatewayConfig.java | 2 +- .../java/org/wfc/gateway/filter/AuthFilter.java | 13 +++++++------ .../org/wfc/gateway/filter/BlackListUrlFilter.java | 3 ++- .../gateway/handler/GatewayExceptionHandler.java | 7 ++++--- .../gateway/handler/SentinelFallbackHandler.java | 5 ++++- 9 files changed, 49 insertions(+), 13 deletions(-) diff --git a/wfc-common/wfc-common-core/src/main/resources/i18n/messages.properties b/wfc-common/wfc-common-core/src/main/resources/i18n/messages.properties index 8927baf..25c3523 100644 --- a/wfc-common/wfc-common-core/src/main/resources/i18n/messages.properties +++ b/wfc-common/wfc-common-core/src/main/resources/i18n/messages.properties @@ -125,6 +125,16 @@ job.execute.success=The scheduled task was executed successfully, the time taken job.execute.spend.time=the time taken: {0} milliseconds ## wfc-file file.cannot.null=File must not be null or empty +## wfc-gateway +gateway.token.not.blank=Token cannot be blank +gateway.token.expired=Token has expired or verification is incorrect +gateway.status.expired=Login status has expired +gateway.token.error=Token error +gateway.user.portal.forbidden=User portal is forbidden to access +gateway.internal.server.error=Internal server error +gateway.service.not.found=Service not found +gateway.request.address.forbidden=Request address is not allowed to access +gateway.request.limit=Request limit exceeded, please try again later ## wfc-common common.operate.success=Operation successful diff --git a/wfc-common/wfc-common-core/src/main/resources/i18n/messages_en_US.properties b/wfc-common/wfc-common-core/src/main/resources/i18n/messages_en_US.properties index 8927baf..25c3523 100644 --- a/wfc-common/wfc-common-core/src/main/resources/i18n/messages_en_US.properties +++ b/wfc-common/wfc-common-core/src/main/resources/i18n/messages_en_US.properties @@ -125,6 +125,16 @@ job.execute.success=The scheduled task was executed successfully, the time taken job.execute.spend.time=the time taken: {0} milliseconds ## wfc-file file.cannot.null=File must not be null or empty +## wfc-gateway +gateway.token.not.blank=Token cannot be blank +gateway.token.expired=Token has expired or verification is incorrect +gateway.status.expired=Login status has expired +gateway.token.error=Token error +gateway.user.portal.forbidden=User portal is forbidden to access +gateway.internal.server.error=Internal server error +gateway.service.not.found=Service not found +gateway.request.address.forbidden=Request address is not allowed to access +gateway.request.limit=Request limit exceeded, please try again later ## wfc-common common.operate.success=Operation successful diff --git a/wfc-common/wfc-common-core/src/main/resources/i18n/messages_zh_CN.properties b/wfc-common/wfc-common-core/src/main/resources/i18n/messages_zh_CN.properties index 5018f5b..f7779e5 100644 --- a/wfc-common/wfc-common-core/src/main/resources/i18n/messages_zh_CN.properties +++ b/wfc-common/wfc-common-core/src/main/resources/i18n/messages_zh_CN.properties @@ -126,6 +126,16 @@ job.execute.success=定时任务执行成功, 耗时:{0} 毫秒 job.execute.spend.time=总共耗时:{0} 毫秒 ## wfc-file file.cannot.null=文件不能为空 +## wfc-gateway +gateway.token.not.blank=令牌不能为空 +gateway.token.expired=令牌已过期或验证不正确 +gateway.status.expired=登录状态已过期 +gateway.token.error=令牌验证失败 +gateway.user.portal.forbidden=用户平台禁止访问 +gateway.internal.server.error=内部服务器错误 +gateway.service.not.found=服务未找到 +gateway.request.address.forbidden=请求地址不允许访问 +gateway.request.limit=请求超过最大数,请稍候再试 ## wfc-common common.operate.success=操作成功 diff --git a/wfc-gateway/src/main/java/org/wfc/gateway/WfcGatewayApplication.java b/wfc-gateway/src/main/java/org/wfc/gateway/WfcGatewayApplication.java index 122af9a..45efed1 100644 --- a/wfc-gateway/src/main/java/org/wfc/gateway/WfcGatewayApplication.java +++ b/wfc-gateway/src/main/java/org/wfc/gateway/WfcGatewayApplication.java @@ -15,6 +15,6 @@ public class WfcGatewayApplication public static void main(String[] args) { SpringApplication.run(WfcGatewayApplication.class, args); - System.out.println("(♥◠‿◠)ノ゙ 网关启动成功 ლ(´ڡ`ლ)゙ \n"); + System.out.println("(♥◠‿◠)ノ゙ Gateway started successfully ლ(´ڡ`ლ)゙ \n"); } } diff --git a/wfc-gateway/src/main/java/org/wfc/gateway/config/GatewayConfig.java b/wfc-gateway/src/main/java/org/wfc/gateway/config/GatewayConfig.java index fc0f028..e1c9833 100644 --- a/wfc-gateway/src/main/java/org/wfc/gateway/config/GatewayConfig.java +++ b/wfc-gateway/src/main/java/org/wfc/gateway/config/GatewayConfig.java @@ -32,7 +32,7 @@ public class GatewayConfig */ @Bean public RouterFunction healthCheckRoute() { - // TODO: Implement a health check endpoint + // Implement a health check endpoint return RouterFunctions.route(RequestPredicates.GET("/health"), request -> ServerResponse.status(HttpStatus.OK) .body(BodyInserters.fromValue("Gateway is healthy")) diff --git a/wfc-gateway/src/main/java/org/wfc/gateway/filter/AuthFilter.java b/wfc-gateway/src/main/java/org/wfc/gateway/filter/AuthFilter.java index b3a3f7c..43d0aa0 100644 --- a/wfc-gateway/src/main/java/org/wfc/gateway/filter/AuthFilter.java +++ b/wfc-gateway/src/main/java/org/wfc/gateway/filter/AuthFilter.java @@ -14,6 +14,7 @@ import org.wfc.common.core.constant.HttpStatus; import org.wfc.common.core.constant.SecurityConstants; import org.wfc.common.core.constant.TokenConstants; import org.wfc.common.core.utils.JwtUtils; +import org.wfc.common.core.utils.MessageUtils; import org.wfc.common.core.utils.ServletUtils; import org.wfc.common.core.utils.StringUtils; import org.wfc.common.redis.service.RedisService; @@ -58,29 +59,29 @@ public class AuthFilter implements GlobalFilter, Ordered String token = getToken(request); if (StringUtils.isEmpty(token)) { - return unauthorizedResponse(exchange, "令牌不能为空"); + return unauthorizedResponse(exchange, MessageUtils.message("gateway.token.not.blank")); } Claims claims = JwtUtils.parseToken(token); if (claims == null) { - return unauthorizedResponse(exchange, "令牌已过期或验证不正确!"); + return unauthorizedResponse(exchange, MessageUtils.message("gateway.token.expired")); } String userkey = JwtUtils.getUserKey(claims); boolean islogin = redisService.hasKey(getTokenKey(userkey)); if (!islogin) { - return unauthorizedResponse(exchange, "登录状态已过期"); + return unauthorizedResponse(exchange, MessageUtils.message("gateway.status.expired")); } String userid = JwtUtils.getUserId(claims); String username = JwtUtils.getUserName(claims); if (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username)) { - return unauthorizedResponse(exchange, "令牌验证失败"); + return unauthorizedResponse(exchange, MessageUtils.message("gateway.token.error")); } String platform = JwtUtils.getUserPlatform(claims); if ("user".equals(platform) && StringUtils.startsWith(url,"/system")) { - return unauthorizedResponse(exchange, "用户平台禁止访问"); + return unauthorizedResponse(exchange, MessageUtils.message("gateway.user.portal.forbidden")); } // 设置用户信息到请求 addHeader(mutate, SecurityConstants.USER_KEY, userkey); @@ -114,7 +115,7 @@ public class AuthFilter implements GlobalFilter, Ordered private Mono unauthorizedResponse(ServerWebExchange exchange, String msg) { - log.error("[鉴权异常处理]请求路径:{},错误信息:{}", exchange.getRequest().getPath(), msg); + log.error("[Authentication exception handling]Request path:{}, error message:{}", exchange.getRequest().getPath(), msg); return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED); } diff --git a/wfc-gateway/src/main/java/org/wfc/gateway/filter/BlackListUrlFilter.java b/wfc-gateway/src/main/java/org/wfc/gateway/filter/BlackListUrlFilter.java index 20466e3..2a295ff 100644 --- a/wfc-gateway/src/main/java/org/wfc/gateway/filter/BlackListUrlFilter.java +++ b/wfc-gateway/src/main/java/org/wfc/gateway/filter/BlackListUrlFilter.java @@ -6,6 +6,7 @@ import java.util.regex.Pattern; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.stereotype.Component; +import org.wfc.common.core.utils.MessageUtils; import org.wfc.common.core.utils.ServletUtils; /** @@ -24,7 +25,7 @@ public class BlackListUrlFilter extends AbstractGatewayFilterFactory writeResponse(ServerResponse response, ServerWebExchange exchange) { - return ServletUtils.webFluxResponseWriter(exchange.getResponse(), "请求超过最大数,请稍候再试"); + return ServletUtils.webFluxResponseWriter(exchange.getResponse(), + MessageUtils.message("gateway.request.limit")); } @Override