Browse Source

网关国际化改造

master
caojianbin 2 years ago
parent
commit
78b085d343
  1. 76
      ruoyi-gateway/src/main/java/com/ruoyi/gateway/constant/MessagesConstant.java
  2. 11
      ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java
  3. 4
      ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/BlackListUrlFilter.java
  4. 11
      ruoyi-gateway/src/main/java/com/ruoyi/gateway/handler/GatewayExceptionHandler.java

76
ruoyi-gateway/src/main/java/com/ruoyi/gateway/constant/MessagesConstant.java

@ -0,0 +1,76 @@ @@ -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;
}
}

11
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/AuthFilter.java

@ -1,5 +1,6 @@ @@ -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 @@ -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 @@ -102,6 +104,7 @@ public class AuthFilter implements GlobalFilter, Ordered
private Mono<Void> unauthorizedResponse(ServerWebExchange exchange, String msg)
{
log.error("[鉴权异常处理]请求路径:{}", exchange.getRequest().getPath());
return ServletUtils.webFluxResponseWriter(exchange.getResponse(), msg, HttpStatus.UNAUTHORIZED);
}

4
ruoyi-gateway/src/main/java/com/ruoyi/gateway/filter/BlackListUrlFilter.java

@ -3,6 +3,8 @@ package com.ruoyi.gateway.filter; @@ -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<BlackListUr @@ -24,7 +26,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(), MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_REQUEST_ADDRESS_ZH,MessagesConstant.MESSAGES_REQUEST_ADDRESS_EN));
}
return chain.filter(exchange);

11
ruoyi-gateway/src/main/java/com/ruoyi/gateway/handler/GatewayExceptionHandler.java

@ -1,5 +1,6 @@ @@ -1,5 +1,6 @@
package com.ruoyi.gateway.handler;
import com.ruoyi.gateway.constant.MessagesConstant;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -37,7 +38,9 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler @@ -37,7 +38,9 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler
if (ex instanceof NotFoundException)
{
msg = "服务未找到";
/**服务未找到*/
msg =MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_SERVICE_NOT_FOUND_ZH,MessagesConstant.MESSAGES_SERVICE_NOT_FOUND_EN);
}
else if (ex instanceof ResponseStatusException)
{
@ -46,11 +49,15 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler @@ -46,11 +49,15 @@ public class GatewayExceptionHandler implements ErrorWebExceptionHandler
}
else
{
msg = "内部服务器错误";
/**内部服务器错误*/
//msg="内部服务器错误";
msg =MessagesConstant.getMessages(exchange,MessagesConstant.MESSAGES_INTERNAL_SERVER_ERROR_ZH,MessagesConstant.MESSAGES_INTERNAL_SERVER_ERROR_EN);
}
log.error("[网关异常处理]请求路径:{},异常信息:{}", exchange.getRequest().getPath(), ex.getMessage());
return ServletUtils.webFluxResponseWriter(response, msg);
}
}
Loading…
Cancel
Save