From 78b085d343a9d943299a531cc28ebf61b4bcd2df Mon Sep 17 00:00:00 2001 From: caojianbin <1910336823> Date: Mon, 25 Jul 2022 17:43:53 +0800 Subject: [PATCH] =?UTF-8?q?=E7=BD=91=E5=85=B3=E5=9B=BD=E9=99=85=E5=8C=96?= =?UTF-8?q?=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ruoyi/gateway/constant/MessagesConstant.java | 76 ++++++++++++++++++++++ .../java/com/ruoyi/gateway/filter/AuthFilter.java | 11 ++-- .../ruoyi/gateway/filter/BlackListUrlFilter.java | 4 +- .../gateway/handler/GatewayExceptionHandler.java | 11 +++- 4 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 ruoyi-gateway/src/main/java/com/ruoyi/gateway/constant/MessagesConstant.java diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/constant/MessagesConstant.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/constant/MessagesConstant.java new file mode 100644 index 0000000..16f93ce --- /dev/null +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/constant/MessagesConstant.java @@ -0,0 +1,76 @@ +package com.ruoyi.gateway.constant; + +import com.ruoyi.gateway.filter.AuthFilter; +import org.slf4j.LoggerFactory; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.web.server.ServerWebExchange; + +import java.util.List; +import java.util.Locale; +import java.util.logging.Logger; + +public class MessagesConstant { + private static final org.slf4j.Logger log = LoggerFactory.getLogger(MessagesConstant.class); + /**令牌不能为空*/ + public static final String MESSAGES_TOKE_EMPTY_EN="Token cannot be empty"; + public static final String MESSAGES_TOKE_EMPTY_ZH="令牌不能为"; + /**令牌已过期或验证不正确*/ + public static final String MESSAGES_TOKE_EMPTY_OR_ERROR_EN="Token expired or incorrectly verified"; + public static final String MESSAGES_TOKE_EMPTY_OR_ERROR_ZH="令牌已过期或验证不正确"; + /**登录状态已过期*/ + public static final String MESSAGES_LOGIN_STATUS_EXPIRED_EN="Login status has expired"; + public static final String MESSAGES_LOGIN_STATUS_EXPIRED_ZH="登录状态已过期"; + /**令牌验证失败*/ + public static final String MESSAGES_TOKE_VERIFICATION_FAILED_EN="Token verification failed"; + public static final String MESSAGES_TOKE_VERIFICATION_FAILED_ZH="令牌验证失败"; + /**请求地址不允许访问*/ + public static final String MESSAGES_REQUEST_ADDRESS_EN="The requested address does not allow access"; + public static final String MESSAGES_REQUEST_ADDRESS_ZH="请求地址不允许访问"; + /**服务未找到*/ + public static final String MESSAGES_SERVICE_NOT_FOUND_EN="Service not found"; + public static final String MESSAGES_SERVICE_NOT_FOUND_ZH="服务未找到"; + /**内部服务器错误*/ + public static final String MESSAGES_INTERNAL_SERVER_ERROR_EN="Internal server error"; + public static final String MESSAGES_INTERNAL_SERVER_ERROR_ZH="内部服务器错误"; + + + /**语言类型 中文*/ + public static final String Language_ZH="zh"; + /**语言类型 英文*/ + public static final String Language_EN="en"; + /**语言类型 key*/ + public static final String HEADER_KEY="yisailanguage"; + + + /** + * 国际化处理 + * @param exchange + * @return + */ + public static String getMessages(ServerWebExchange exchange, String zh_msg, String en_msg) { + //给一个默认值 + String msg=zh_msg; + ServerHttpRequest request = exchange.getRequest(); + if (request.getHeaders().containsKey(MessagesConstant.HEADER_KEY)) { + String zh = exchange.getRequest().getHeaders().getFirst(MessagesConstant.Language_ZH); + if (MessagesConstant.Language_ZH.equals(zh)) { + msg = zh_msg; + } else { + msg = en_msg; + } + }else { + String s = request.getHeaders().get("accept-language").get(0).split(",")[1]; + log.info("获取头部的语言:{}",s); + if (MessagesConstant.Language_ZH.equals(s)) { + msg = zh_msg; + } else { + msg = en_msg; + } + + + } + + + return msg; + } +} diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java index 101de63..49a8ce1 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java @@ -1,5 +1,6 @@ package com.ruoyi.gateway.filter; +import com.ruoyi.gateway.constant.MessagesConstant; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -54,24 +55,25 @@ public class AuthFilter implements GlobalFilter, Ordered String token = getToken(request); if (StringUtils.isEmpty(token)) { - return unauthorizedResponse(exchange, "令牌不能为空"); + + return unauthorizedResponse(exchange, MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_TOKE_EMPTY_ZH,MessagesConstant.MESSAGES_TOKE_EMPTY_EN)); } Claims claims = JwtUtils.parseToken(token); if (claims == null) { - return unauthorizedResponse(exchange, "令牌已过期或验证不正确!"); + return unauthorizedResponse(exchange, MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_TOKE_EMPTY_OR_ERROR_ZH,MessagesConstant.MESSAGES_TOKE_EMPTY_OR_ERROR_EN)); } String userkey = JwtUtils.getUserKey(claims); boolean islogin = redisService.hasKey(getTokenKey(userkey)); if (!islogin) { - return unauthorizedResponse(exchange, "登录状态已过期"); + return unauthorizedResponse(exchange, MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_LOGIN_STATUS_EXPIRED_ZH,MessagesConstant.MESSAGES_LOGIN_STATUS_EXPIRED_EN)); } String userid = JwtUtils.getUserId(claims); String username = JwtUtils.getUserName(claims); if (StringUtils.isEmpty(userid) || StringUtils.isEmpty(username)) { - return unauthorizedResponse(exchange, "令牌验证失败"); + return unauthorizedResponse(exchange, MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_TOKE_VERIFICATION_FAILED_ZH,MessagesConstant.MESSAGES_TOKE_VERIFICATION_FAILED_EN)); } // 设置用户信息到请求 @@ -102,6 +104,7 @@ public class AuthFilter implements GlobalFilter, Ordered private Mono unauthorizedResponse(ServerWebExchange exchange, String msg) { log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath()); + return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED); } diff --git a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/BlackListUrlFilter.java b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/BlackListUrlFilter.java index ec7801b..d5ce7cd 100644 --- a/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/BlackListUrlFilter.java +++ b/ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/BlackListUrlFilter.java @@ -3,6 +3,8 @@ package com.ruoyi.gateway.filter; import java.util.ArrayList; import java.util.List; import java.util.regex.Pattern; + +import com.ruoyi.gateway.constant.MessagesConstant; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; import org.springframework.stereotype.Component; @@ -24,7 +26,7 @@ public class BlackListUrlFilter extends AbstractGatewayFilterFactory