2
0

feat: gateway support i18n message

This commit is contained in:
zhangsz
2025-01-22 14:07:02 +08:00
parent f7f8642401
commit 9d7b44d1dd
9 changed files with 49 additions and 13 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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=操作成功

View File

@@ -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");
}
}

View File

@@ -32,7 +32,7 @@ public class GatewayConfig
*/
@Bean
public RouterFunction<ServerResponse> 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"))

View File

@@ -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<Void> 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);
}

View File

@@ -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<BlackListUr
String url = exchange.getRequest().getURI().getPath();
if (config.matchBlacklist(url))
{
return ServletUtils.webFluxResponseWriter(exchange.getResponse(), "请求地址不允许访问");
return ServletUtils.webFluxResponseWriter(exchange.getResponse(), MessageUtils.message("gateway.request.address.forbidden"));
}
return chain.filter(exchange);

View File

@@ -9,6 +9,7 @@ import org.springframework.core.annotation.Order;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.web.server.ResponseStatusException;
import org.springframework.web.server.ServerWebExchange;
import org.wfc.common.core.utils.MessageUtils;
import org.wfc.common.core.utils.ServletUtils;
import reactor.core.publisher.Mono;
@@ -37,7 +38,7 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler
if (ex instanceof NotFoundException)
{
msg = "服务未找到";
msg = MessageUtils.message("gateway.service.not.found");
}
else if (ex instanceof ResponseStatusException)
{
@@ -46,10 +47,10 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler
}
else
{
msg = "内部服务器错误";
msg = MessageUtils.message("gateway.internal.server.error");
}
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
log.error("[Gateway Exception Handling] Request path: {}, exception: {}", exchange.getRequest().getPath(), ex.getMessage());
return ServletUtils.webFluxResponseWriter(response, msg);
}

View File

@@ -2,6 +2,8 @@ package org.wfc.gateway.handler;
import com.alibaba.csp.sentinel.adapter.gateway.sc.callback.GatewayCallbackManager;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import org.wfc.common.core.utils.MessageUtils;
import org.wfc.common.core.utils.ServletUtils;
import org.springframework.web.reactive.function.server.ServerResponse;
import org.springframework.web.server.ServerWebExchange;
@@ -17,7 +19,8 @@ public class SentinelFallbackHandler implements WebExceptionHandler
{
private Mono<Void> writeResponse(ServerResponse response, ServerWebExchange exchange)
{
return ServletUtils.webFluxResponseWriter(exchange.getResponse(), "请求超过最大数,请稍候再试");
return ServletUtils.webFluxResponseWriter(exchange.getResponse(),
MessageUtils.message("gateway.request.limit"));
}
@Override