Browse Source

修改:gateway中增加请求url的日志,和报错的日志

dev
wxz 4 years ago
parent
commit
d73a975c16
  1. 6
      epmet-gateway/src/main/java/com/epmet/auth/ExtAppTakeTokenAuthProcessor.java
  2. 56
      epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java

6
epmet-gateway/src/main/java/com/epmet/auth/ExtAppTakeTokenAuthProcessor.java

@ -1,7 +1,6 @@
package com.epmet.auth;
import com.epmet.commons.security.jwt.JwtUtils;
import com.epmet.commons.security.sign.openapi.OpenApiSignUtils;
import com.epmet.commons.tools.exception.EpmetErrorCode;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.redis.RedisKeys;
@ -12,7 +11,6 @@ import com.epmet.feign.EpmetCommonServiceOpenFeignClient;
import com.epmet.openapi.constant.InClusterHeaderKeys;
import com.epmet.openapi.constant.RequestParamKeys;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.server.reactive.ServerHttpRequest;
@ -79,11 +77,11 @@ public class ExtAppTakeTokenAuthProcessor extends ExtAppAuthProcessor {
EpmetCommonServiceOpenFeignClient commonService = SpringContextUtils.getBean(EpmetCommonServiceOpenFeignClient.class);
Result<String> result = commonService.getSecret(appId);
if (result == null || !result.success()) {
throw new RenException("fetchToken方式的外部应用认证,获取secret失败");
throw new RenException("TakeToken方式的外部应用认证,获取secret失败");
}
String secret = result.getData();
if (StringUtils.isBlank(secret)) {
throw new RenException("fetchToken方式的外部应用认证,获取secret失败");
throw new RenException("TakeToken方式的外部应用认证,获取secret失败");
}
return secret;

56
epmet-gateway/src/main/java/com/epmet/filter/CpAuthGatewayFilterFactory.java

@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSON;
import com.epmet.auth.ExternalAuthProcessor;
import com.epmet.auth.InternalAuthProcessor;
import com.epmet.commons.tools.constant.AppClientConstant;
import com.epmet.commons.tools.exception.ExceptionUtils;
import com.epmet.commons.tools.exception.RenException;
import com.epmet.commons.tools.utils.IpUtils;
import com.epmet.commons.tools.utils.Result;
@ -26,13 +27,13 @@ import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.util.MultiValueMap;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.List;
import java.util.*;
/**
* app接口权限过滤器
@ -67,17 +68,22 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
return chain.filter(exchange);
}
//添加流水号
//1.添加流水号
ServerHttpRequest request = exchange.getRequest();
List<String> tranSerials = request.getHeaders().get(AppClientConstant.TRANSACTION_SERIAL_KEY);
if (CollectionUtils.isEmpty(tranSerials) || StringUtils.isBlank(tranSerials.get(0))) {
request.mutate().header(AppClientConstant.TRANSACTION_SERIAL_KEY, new String[]{getTransactionSerial()});
}
String authType = getAuthType(request);
//2.获取请求路径,参数
String requestUri = request.getPath().pathWithinApplication().value();
MultiValueMap<String, String> queryParams = request.getQueryParams();
String queryParamsStr = convertQueryParams2String(queryParams);
logger.info("CpAuthGatewayFilterFactory当前requestUri=[" + requestUri.concat(queryParamsStr) + "],CpAuthGatewayFilterFactory拦截成功,客户端Id:{}", IpUtils.getClientIp(request));
logger.info("CpAuthGatewayFilterFactory当前requestUri=[" + requestUri + "],CpAuthGatewayFilterFactory拦截成功,客户端Id:{}", IpUtils.getClientIp(request));
//3.认证
String authType = getAuthType(request);
logger.info("认证类型为:{}", authType);
try {
switch (authType) {
case AuthTypeConstant.AUTH_TYPE_ALL:
@ -92,8 +98,10 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
break;
}
} catch (RenException e) {
logger.error("CpAuthGatewayFilterFactory认证出错,错误信息:{}", ExceptionUtils.getErrorStackTrace(e));
return response(exchange, new Result<>().error(e.getCode(), e.getMessage()));
} catch (Exception e) {
logger.error("CpAuthGatewayFilterFactory认证出错,错误信息:{}", ExceptionUtils.getErrorStackTrace(e));
return response(exchange, new Result<>().error(e.getMessage()));
}
@ -101,8 +109,44 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
};
}
/**
* @return
* @Description 将url参数转化为String
* @author wxz
* @date 2021.08.11 23:12
*/
private String convertQueryParams2String(MultiValueMap<String, String> queryParams) {
try {
if (queryParams == null || queryParams.size() == 0) {
return "";
}
StringBuilder sb = new StringBuilder("");
queryParams.entrySet().forEach(entry -> {
String key = entry.getKey();
List<String> values = entry.getValue();
String value = "";
if (values != null && values.size() > 0) {
value = values.get(0);
}
sb.append("&").append(key).append("=").append(value);
});
String result = sb.toString();
if (result.startsWith("&")) {
result = result.substring(1);
return "?".concat(result);
}
return "";
} catch (Exception e) {
logger.warn("gateway中将url参数转化为String失败,程序继续执行,错误信息:".concat(ExceptionUtils.getErrorStackTrace(e)));
return "";
}
}
/**
* 判断需要执行的认证方式(内部应用认证还是外部应用认证)
*
* @return
*/
private String getAuthType(ServerHttpRequest request) {
@ -139,6 +183,7 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
/**
* 获取请求头
*
* @param request
* @return
*/
@ -149,6 +194,7 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA
/**
* 获取事务流水号
*
* @return
*/
public static String getTransactionSerial() {

Loading…
Cancel
Save