Sfoglia il codice sorgente

优化aop语法 使用spring自动注入注解 基于注解拦截的aop注解不可能为空

RuoYi 4 anni fa
parent
commit
39e7d8a84b

+ 4 - 36
ruoyi-common/ruoyi-common-datascope/src/main/java/com/ruoyi/common/datascope/aspect/DataScopeAspect.java

@@ -1,12 +1,8 @@
1
 package com.ruoyi.common.datascope.aspect;
1
 package com.ruoyi.common.datascope.aspect;
2
 
2
 
3
-import java.lang.reflect.Method;
4
 import org.aspectj.lang.JoinPoint;
3
 import org.aspectj.lang.JoinPoint;
5
-import org.aspectj.lang.Signature;
6
 import org.aspectj.lang.annotation.Aspect;
4
 import org.aspectj.lang.annotation.Aspect;
7
 import org.aspectj.lang.annotation.Before;
5
 import org.aspectj.lang.annotation.Before;
8
-import org.aspectj.lang.annotation.Pointcut;
9
-import org.aspectj.lang.reflect.MethodSignature;
10
 import org.springframework.beans.factory.annotation.Autowired;
6
 import org.springframework.beans.factory.annotation.Autowired;
11
 import org.springframework.stereotype.Component;
7
 import org.springframework.stereotype.Component;
12
 import com.ruoyi.common.core.utils.StringUtils;
8
 import com.ruoyi.common.core.utils.StringUtils;
@@ -59,27 +55,15 @@ public class DataScopeAspect
59
     @Autowired
55
     @Autowired
60
     private TokenService tokenService;
56
     private TokenService tokenService;
61
 
57
 
62
-    // 配置织入点
63
-    @Pointcut("@annotation(com.ruoyi.common.datascope.annotation.DataScope)")
64
-    public void dataScopePointCut()
65
-    {
66
-    }
67
-
68
-    @Before("dataScopePointCut()")
69
-    public void doBefore(JoinPoint point) throws Throwable
58
+    @Before("@annotation(controllerDataScope)")
59
+    public void doBefore(JoinPoint point, DataScope controllerDataScope) throws Throwable
70
     {
60
     {
71
         clearDataScope(point);
61
         clearDataScope(point);
72
-        handleDataScope(point);
62
+        handleDataScope(point, controllerDataScope);
73
     }
63
     }
74
 
64
 
75
-    protected void handleDataScope(final JoinPoint joinPoint)
65
+    protected void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope)
76
     {
66
     {
77
-        // 获得注解
78
-        DataScope controllerDataScope = getAnnotationLog(joinPoint);
79
-        if (controllerDataScope == null)
80
-        {
81
-            return;
82
-        }
83
         // 获取当前的用户
67
         // 获取当前的用户
84
         LoginUser loginUser = tokenService.getLoginUser();
68
         LoginUser loginUser = tokenService.getLoginUser();
85
         if (StringUtils.isNotNull(loginUser))
69
         if (StringUtils.isNotNull(loginUser))
@@ -156,22 +140,6 @@ public class DataScopeAspect
156
     }
140
     }
157
 
141
 
158
     /**
142
     /**
159
-     * 是否存在注解,如果存在就获取
160
-     */
161
-    private DataScope getAnnotationLog(JoinPoint joinPoint)
162
-    {
163
-        Signature signature = joinPoint.getSignature();
164
-        MethodSignature methodSignature = (MethodSignature) signature;
165
-        Method method = methodSignature.getMethod();
166
-
167
-        if (method != null)
168
-        {
169
-            return method.getAnnotation(DataScope.class);
170
-        }
171
-        return null;
172
-    }
173
-
174
-    /**
175
      * 拼接权限sql前先清空params.dataScope参数防止注入
143
      * 拼接权限sql前先清空params.dataScope参数防止注入
176
      */
144
      */
177
     private void clearDataScope(final JoinPoint joinPoint)
145
     private void clearDataScope(final JoinPoint joinPoint)

+ 17 - 46
ruoyi-common/ruoyi-common-log/src/main/java/com/ruoyi/common/log/aspect/LogAspect.java

@@ -1,18 +1,13 @@
1
 package com.ruoyi.common.log.aspect;
1
 package com.ruoyi.common.log.aspect;
2
 
2
 
3
-import java.lang.reflect.Method;
4
 import java.util.Collection;
3
 import java.util.Collection;
5
-import java.util.Iterator;
6
 import java.util.Map;
4
 import java.util.Map;
7
 import javax.servlet.http.HttpServletRequest;
5
 import javax.servlet.http.HttpServletRequest;
8
 import javax.servlet.http.HttpServletResponse;
6
 import javax.servlet.http.HttpServletResponse;
9
 import org.aspectj.lang.JoinPoint;
7
 import org.aspectj.lang.JoinPoint;
10
-import org.aspectj.lang.Signature;
11
 import org.aspectj.lang.annotation.AfterReturning;
8
 import org.aspectj.lang.annotation.AfterReturning;
12
 import org.aspectj.lang.annotation.AfterThrowing;
9
 import org.aspectj.lang.annotation.AfterThrowing;
13
 import org.aspectj.lang.annotation.Aspect;
10
 import org.aspectj.lang.annotation.Aspect;
14
-import org.aspectj.lang.annotation.Pointcut;
15
-import org.aspectj.lang.reflect.MethodSignature;
16
 import org.slf4j.Logger;
11
 import org.slf4j.Logger;
17
 import org.slf4j.LoggerFactory;
12
 import org.slf4j.LoggerFactory;
18
 import org.springframework.beans.factory.annotation.Autowired;
13
 import org.springframework.beans.factory.annotation.Autowired;
@@ -44,21 +39,15 @@ public class LogAspect
44
     @Autowired
39
     @Autowired
45
     private AsyncLogService asyncLogService;
40
     private AsyncLogService asyncLogService;
46
 
41
 
47
-    // 配置织入点
48
-    @Pointcut("@annotation(com.ruoyi.common.log.annotation.Log)")
49
-    public void logPointCut()
50
-    {
51
-    }
52
-
53
     /**
42
     /**
54
      * 处理完请求后执行
43
      * 处理完请求后执行
55
      *
44
      *
56
      * @param joinPoint 切点
45
      * @param joinPoint 切点
57
      */
46
      */
58
-    @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")
59
-    public void doAfterReturning(JoinPoint joinPoint, Object jsonResult)
47
+    @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
48
+    public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult)
60
     {
49
     {
61
-        handleLog(joinPoint, null, jsonResult);
50
+        handleLog(joinPoint, controllerLog, null, jsonResult);
62
     }
51
     }
63
 
52
 
64
     /**
53
     /**
@@ -67,23 +56,16 @@ public class LogAspect
67
      * @param joinPoint 切点
56
      * @param joinPoint 切点
68
      * @param e 异常
57
      * @param e 异常
69
      */
58
      */
70
-    @AfterThrowing(value = "logPointCut()", throwing = "e")
71
-    public void doAfterThrowing(JoinPoint joinPoint, Exception e)
59
+    @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
60
+    public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e)
72
     {
61
     {
73
-        handleLog(joinPoint, e, null);
62
+        handleLog(joinPoint, controllerLog, e, null);
74
     }
63
     }
75
 
64
 
76
-    protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult)
65
+    protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult)
77
     {
66
     {
78
         try
67
         try
79
         {
68
         {
80
-            // 获得注解
81
-            Log controllerLog = getAnnotationLog(joinPoint);
82
-            if (controllerLog == null)
83
-            {
84
-                return;
85
-            }
86
-
87
             // *========数据库日志=========*//
69
             // *========数据库日志=========*//
88
             SysOperLog operLog = new SysOperLog();
70
             SysOperLog operLog = new SysOperLog();
89
             operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
71
             operLog.setStatus(BusinessStatus.SUCCESS.ordinal());
@@ -164,22 +146,11 @@ public class LogAspect
164
             String params = argsArrayToString(joinPoint.getArgs());
146
             String params = argsArrayToString(joinPoint.getArgs());
165
             operLog.setOperParam(StringUtils.substring(params, 0, 2000));
147
             operLog.setOperParam(StringUtils.substring(params, 0, 2000));
166
         }
148
         }
167
-    }
168
-
169
-    /**
170
-     * 是否存在注解,如果存在就获取
171
-     */
172
-    private Log getAnnotationLog(JoinPoint joinPoint) throws Exception
173
-    {
174
-        Signature signature = joinPoint.getSignature();
175
-        MethodSignature methodSignature = (MethodSignature) signature;
176
-        Method method = methodSignature.getMethod();
177
-
178
-        if (method != null)
149
+        else
179
         {
150
         {
180
-            return method.getAnnotation(Log.class);
151
+            Map<?, ?> paramsMap = (Map<?, ?>) ServletUtils.getRequest().getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
152
+            operLog.setOperParam(StringUtils.substring(paramsMap.toString(), 0, 2000));
181
         }
153
         }
182
-        return null;
183
     }
154
     }
184
 
155
 
185
     /**
156
     /**
@@ -190,13 +161,13 @@ public class LogAspect
190
         String params = "";
161
         String params = "";
191
         if (paramsArray != null && paramsArray.length > 0)
162
         if (paramsArray != null && paramsArray.length > 0)
192
         {
163
         {
193
-            for (int i = 0; i < paramsArray.length; i++)
164
+            for (Object o : paramsArray)
194
             {
165
             {
195
-                if (StringUtils.isNotNull(paramsArray[i]) && !isFilterObject(paramsArray[i]))
166
+                if (StringUtils.isNotNull(o) && !isFilterObject(o))
196
                 {
167
                 {
197
                     try
168
                     try
198
                     {
169
                     {
199
-                        Object jsonObj = JSON.toJSON(paramsArray[i]);
170
+                        Object jsonObj = JSON.toJSON(o);
200
                         params += jsonObj.toString() + " ";
171
                         params += jsonObj.toString() + " ";
201
                     }
172
                     }
202
                     catch (Exception e)
173
                     catch (Exception e)
@@ -225,17 +196,17 @@ public class LogAspect
225
         else if (Collection.class.isAssignableFrom(clazz))
196
         else if (Collection.class.isAssignableFrom(clazz))
226
         {
197
         {
227
             Collection collection = (Collection) o;
198
             Collection collection = (Collection) o;
228
-            for (Iterator iter = collection.iterator(); iter.hasNext();)
199
+            for (Object value : collection)
229
             {
200
             {
230
-                return iter.next() instanceof MultipartFile;
201
+                return value instanceof MultipartFile;
231
             }
202
             }
232
         }
203
         }
233
         else if (Map.class.isAssignableFrom(clazz))
204
         else if (Map.class.isAssignableFrom(clazz))
234
         {
205
         {
235
             Map map = (Map) o;
206
             Map map = (Map) o;
236
-            for (Iterator iter = map.entrySet().iterator(); iter.hasNext();)
207
+            for (Object value : map.entrySet())
237
             {
208
             {
238
-                Map.Entry entry = (Map.Entry) iter.next();
209
+                Map.Entry entry = (Map.Entry) value;
239
                 return entry.getValue() instanceof MultipartFile;
210
                 return entry.getValue() instanceof MultipartFile;
240
             }
211
             }
241
         }
212
         }