|
@ -5,6 +5,7 @@ import com.alibaba.fastjson.JSON; |
|
|
import com.epmet.auth.ExternalAuthProcessor; |
|
|
import com.epmet.auth.ExternalAuthProcessor; |
|
|
import com.epmet.auth.InternalAuthProcessor; |
|
|
import com.epmet.auth.InternalAuthProcessor; |
|
|
import com.epmet.commons.tools.constant.AppClientConstant; |
|
|
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.exception.RenException; |
|
|
import com.epmet.commons.tools.utils.IpUtils; |
|
|
import com.epmet.commons.tools.utils.IpUtils; |
|
|
import com.epmet.commons.tools.utils.Result; |
|
|
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.MediaType; |
|
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
|
|
import org.springframework.http.server.reactive.ServerHttpRequest; |
|
|
import org.springframework.stereotype.Component; |
|
|
import org.springframework.stereotype.Component; |
|
|
|
|
|
import org.springframework.util.MultiValueMap; |
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
import org.springframework.web.server.ServerWebExchange; |
|
|
import reactor.core.publisher.Flux; |
|
|
import reactor.core.publisher.Flux; |
|
|
import reactor.core.publisher.Mono; |
|
|
import reactor.core.publisher.Mono; |
|
|
|
|
|
|
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.nio.charset.StandardCharsets; |
|
|
import java.util.Arrays; |
|
|
import java.util.*; |
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* app接口权限过滤器 |
|
|
* app接口权限过滤器 |
|
@ -67,17 +68,22 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA |
|
|
return chain.filter(exchange); |
|
|
return chain.filter(exchange); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
//添加流水号
|
|
|
//1.添加流水号
|
|
|
ServerHttpRequest request = exchange.getRequest(); |
|
|
ServerHttpRequest request = exchange.getRequest(); |
|
|
List<String> tranSerials = request.getHeaders().get(AppClientConstant.TRANSACTION_SERIAL_KEY); |
|
|
List<String> tranSerials = request.getHeaders().get(AppClientConstant.TRANSACTION_SERIAL_KEY); |
|
|
if (CollectionUtils.isEmpty(tranSerials) || StringUtils.isBlank(tranSerials.get(0))) { |
|
|
if (CollectionUtils.isEmpty(tranSerials) || StringUtils.isBlank(tranSerials.get(0))) { |
|
|
request.mutate().header(AppClientConstant.TRANSACTION_SERIAL_KEY, new String[]{getTransactionSerial()}); |
|
|
request.mutate().header(AppClientConstant.TRANSACTION_SERIAL_KEY, new String[]{getTransactionSerial()}); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
String authType = getAuthType(request); |
|
|
//2.获取请求路径,参数
|
|
|
String requestUri = request.getPath().pathWithinApplication().value(); |
|
|
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 { |
|
|
try { |
|
|
switch (authType) { |
|
|
switch (authType) { |
|
|
case AuthTypeConstant.AUTH_TYPE_ALL: |
|
|
case AuthTypeConstant.AUTH_TYPE_ALL: |
|
@ -92,8 +98,10 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
} catch (RenException e) { |
|
|
} catch (RenException e) { |
|
|
|
|
|
logger.error("CpAuthGatewayFilterFactory认证出错,错误信息:{}", ExceptionUtils.getErrorStackTrace(e)); |
|
|
return response(exchange, new Result<>().error(e.getCode(), e.getMessage())); |
|
|
return response(exchange, new Result<>().error(e.getCode(), e.getMessage())); |
|
|
} catch (Exception e) { |
|
|
} catch (Exception e) { |
|
|
|
|
|
logger.error("CpAuthGatewayFilterFactory认证出错,错误信息:{}", ExceptionUtils.getErrorStackTrace(e)); |
|
|
return response(exchange, new Result<>().error(e.getMessage())); |
|
|
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 |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
private String getAuthType(ServerHttpRequest request) { |
|
|
private String getAuthType(ServerHttpRequest request) { |
|
@ -139,6 +183,7 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 获取请求头 |
|
|
* 获取请求头 |
|
|
|
|
|
* |
|
|
* @param request |
|
|
* @param request |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
@ -149,6 +194,7 @@ public class CpAuthGatewayFilterFactory extends AbstractGatewayFilterFactory<CpA |
|
|
|
|
|
|
|
|
/** |
|
|
/** |
|
|
* 获取事务流水号 |
|
|
* 获取事务流水号 |
|
|
|
|
|
* |
|
|
* @return |
|
|
* @return |
|
|
*/ |
|
|
*/ |
|
|
public static String getTransactionSerial() { |
|
|
public static String getTransactionSerial() { |
|
|