From 7e213deefec494f0f124b75f2c9be966ec38c0d4 Mon Sep 17 00:00:00 2001 From: quyixiao <2621048238@qq.com> Date: Sat, 12 Apr 2025 23:21:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../heyu/api/data/constants/ApiConstants.java | 10 +++++++ .../heyu/api/data/utils/AnnotationUtils.java | 20 +++++++++++-- .../main/java/com/heyu/api/aop/LogAop.java | 29 +++++++++++++------ 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/api-mapper/src/main/java/com/heyu/api/data/constants/ApiConstants.java b/api-mapper/src/main/java/com/heyu/api/data/constants/ApiConstants.java index 60475a8..4be20db 100644 --- a/api-mapper/src/main/java/com/heyu/api/data/constants/ApiConstants.java +++ b/api-mapper/src/main/java/com/heyu/api/data/constants/ApiConstants.java @@ -296,4 +296,14 @@ public class ApiConstants { public static final String TENCENT_AUTH = "20C7941B2CCAD689A90C796A4A9DF04A"; + + /** + * 不需要 注解名称 + */ + public static final String EB_AUTHENTICATION = "EbAuthentication"; + + + public static final String t_auth = "t_auth"; + + } diff --git a/api-mapper/src/main/java/com/heyu/api/data/utils/AnnotationUtils.java b/api-mapper/src/main/java/com/heyu/api/data/utils/AnnotationUtils.java index 6bd94fa..fa4ddb1 100644 --- a/api-mapper/src/main/java/com/heyu/api/data/utils/AnnotationUtils.java +++ b/api-mapper/src/main/java/com/heyu/api/data/utils/AnnotationUtils.java @@ -23,7 +23,7 @@ public class AnnotationUtils { if (annotations != null && annotations.length > 0) { for (Annotation annotation : annotations) { if (name.equals(getAnnotationName(annotation))) { - return getAnnotationValue(annotation); + return getAnnotationValue(annotation,"value"); } } } @@ -43,6 +43,20 @@ public class AnnotationUtils { return false; } + public static String getAnnotation(Method method, String name) { + Annotation[] annotations = method.getAnnotations(); + if (annotations != null && annotations.length > 0) { + for (Annotation annotation : annotations) { + if (name.equals(getAnnotationName(annotation))) { + return getAnnotationValue(annotation,"tencent"); + } + } + } + return null; + } + + + public static String getAnnotationName(Annotation annotation) { String annotionStr = annotation.toString(); int a = annotionStr.indexOf("(", 0); @@ -58,9 +72,9 @@ public class AnnotationUtils { - public static T getAnnotationValue(Annotation annotation) { + public static T getAnnotationValue(Annotation annotation,String key) { try { - Method method = annotation.getClass().getMethod("value"); + Method method = annotation.getClass().getMethod(key); if (method != null) { T paramName = (T) method.invoke(annotation); return paramName; diff --git a/api-web/api-interface/src/main/java/com/heyu/api/aop/LogAop.java b/api-web/api-interface/src/main/java/com/heyu/api/aop/LogAop.java index 31ba5fc..6189abe 100644 --- a/api-web/api-interface/src/main/java/com/heyu/api/aop/LogAop.java +++ b/api-web/api-interface/src/main/java/com/heyu/api/aop/LogAop.java @@ -1,6 +1,7 @@ package com.heyu.api.aop; import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.JSONObject; import com.heyu.api.common.LogAspect; import com.heyu.api.data.constants.ApiConstants; import com.heyu.api.data.service.bussiness.RedisSettingService; @@ -48,6 +49,8 @@ public class LogAop { public static Map faceAuthMap = new HashMap<>(); + public static Map faceAuthTentMap = new HashMap<>(); + @Value("${eb.config.rabbitQueue.accountAmountQueue}") private String accountAmountQueue; @@ -64,7 +67,6 @@ public class LogAop { Object result = null; StringBuilder sb = new StringBuilder(); String methodName = ""; - String token = ""; String ip = ""; String args = ""; String className = ""; @@ -91,6 +93,7 @@ public class LogAop { className = clazz.getName(); // 类名加方法名 String classNameAndMethodName = className + "#" + methodName; + String classNameAndMethodNametencent = className + "#" + methodName + "#tencent"; sb.append("LoggerAop args:").append(args).append(BLANK_SPACE) .append("class:").append(className).append("#").append(methodName).append(BLANK_SPACE) @@ -100,26 +103,31 @@ public class LogAop { Boolean intercept = faceAuthMap.get(className); Boolean classMethodConfigNotIntercept = faceAuthMap.get(classNameAndMethodName); - String authConfig = request.getHeader("X-TCloudMarket-Custom-AuthConfig"); - log.info("authConfig:{}",authConfig); - if (intercept == null) { boolean exitClassAnnotation = AnnotationUtils.hasAnnotation(clazz, ApiConstants.NOT_INTERCEPT_ANNOTATION_NAME); boolean exitMethodAnnotation = AnnotationUtils.hasAnnotation(method, ApiConstants.NOT_INTERCEPT_ANNOTATION_NAME); + String tencent = AnnotationUtils.getAnnotation(method, ApiConstants.EB_AUTHENTICATION); // 如果存在NotIntercept则不进行拦截,否则进行拦截 intercept = exitClassAnnotation ? false : true; classMethodConfigNotIntercept = exitMethodAnnotation ? false : true; faceAuthMap.put(className, intercept); - faceAuthMap.put(classNameAndMethodName, intercept); + faceAuthMap.put(classNameAndMethodName, classMethodConfigNotIntercept); + faceAuthTentMap.put(classNameAndMethodNametencent , tencent); + log.info("LogAop one cache className:{},classNameAndMethodName:{},classNameAndMethodNametencent:{}", intercept, classMethodConfigNotIntercept, tencent); } // 如果需要进行拦截 - if (!intercept && !classMethodConfigNotIntercept ) { - - - return R.error("auth error"); + if (!intercept && !classMethodConfigNotIntercept) { + String authConfig = request.getHeader("X-TCloudMarket-Custom-AuthConfig"); + Map authConfigMap = JSONObject.parseObject(authConfig, Map.class); + String auth = faceAuthTentMap.get(classNameAndMethodNametencent); + if(StringUtils.isBlank(auth) || !auth.equals(authConfigMap.get(ApiConstants.t_auth))){ + return R.error("auth error"); + } } + + result = point.proceed(); } catch (Exception e) { log.error("LogAop set error " + sb.toString(), e); @@ -144,6 +152,9 @@ public class LogAop { } + + + private String recordRequestLog(Object[] argArrs, String uri) { String mediaType = ""; String args = null;