作者 疯狂的狮子li

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

1 package com.ruoyi.framework.aspectj; 1 package com.ruoyi.framework.aspectj;
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.stereotype.Component; 6 import org.springframework.stereotype.Component;
11 import com.ruoyi.common.annotation.DataScope; 7 import com.ruoyi.common.annotation.DataScope;
12 import com.ruoyi.common.core.domain.BaseEntity; 8 import com.ruoyi.common.core.domain.BaseEntity;
@@ -55,27 +51,15 @@ public class DataScopeAspect @@ -55,27 +51,15 @@ public class DataScopeAspect
55 */ 51 */
56 public static final String DATA_SCOPE = "dataScope"; 52 public static final String DATA_SCOPE = "dataScope";
57 53
58 - // 配置织入点  
59 - @Pointcut("@annotation(com.ruoyi.common.annotation.DataScope)")  
60 - public void dataScopePointCut()  
61 - {  
62 - }  
63 -  
64 - @Before("dataScopePointCut()")  
65 - public void doBefore(JoinPoint point) throws Throwable 54 + @Before("@annotation(controllerDataScope)")
  55 + public void doBefore(JoinPoint point, DataScope controllerDataScope) throws Throwable
66 { 56 {
67 clearDataScope(point); 57 clearDataScope(point);
68 - handleDataScope(point); 58 + handleDataScope(point, controllerDataScope);
69 } 59 }
70 60
71 - protected void handleDataScope(final JoinPoint joinPoint) 61 + protected void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope)
72 { 62 {
73 - // 获得注解  
74 - DataScope controllerDataScope = getAnnotationLog(joinPoint);  
75 - if (controllerDataScope == null)  
76 - {  
77 - return;  
78 - }  
79 // 获取当前的用户 63 // 获取当前的用户
80 LoginUser loginUser = SecurityUtils.getLoginUser(); 64 LoginUser loginUser = SecurityUtils.getLoginUser();
81 if (StringUtils.isNotNull(loginUser)) 65 if (StringUtils.isNotNull(loginUser))
@@ -151,22 +135,6 @@ public class DataScopeAspect @@ -151,22 +135,6 @@ public class DataScopeAspect
151 } 135 }
152 136
153 /** 137 /**
154 - * 是否存在注解,如果存在就获取  
155 - */  
156 - private DataScope getAnnotationLog(JoinPoint joinPoint)  
157 - {  
158 - Signature signature = joinPoint.getSignature();  
159 - MethodSignature methodSignature = (MethodSignature) signature;  
160 - Method method = methodSignature.getMethod();  
161 -  
162 - if (method != null)  
163 - {  
164 - return method.getAnnotation(DataScope.class);  
165 - }  
166 - return null;  
167 - }  
168 -  
169 - /**  
170 * 拼接权限sql前先清空params.dataScope参数防止注入 138 * 拼接权限sql前先清空params.dataScope参数防止注入
171 */ 139 */
172 private void clearDataScope(final JoinPoint joinPoint) 140 private void clearDataScope(final JoinPoint joinPoint)
1 package com.ruoyi.framework.aspectj; 1 package com.ruoyi.framework.aspectj;
2 2
3 -import java.util.Objects;  
4 import org.aspectj.lang.ProceedingJoinPoint; 3 import org.aspectj.lang.ProceedingJoinPoint;
5 import org.aspectj.lang.annotation.Around; 4 import org.aspectj.lang.annotation.Around;
6 import org.aspectj.lang.annotation.Aspect; 5 import org.aspectj.lang.annotation.Aspect;
7 -import org.aspectj.lang.annotation.Pointcut;  
8 -import org.aspectj.lang.reflect.MethodSignature;  
9 import org.slf4j.Logger; 6 import org.slf4j.Logger;
10 import org.slf4j.LoggerFactory; 7 import org.slf4j.LoggerFactory;
11 -import org.springframework.core.annotation.AnnotationUtils;  
12 import org.springframework.core.annotation.Order; 8 import org.springframework.core.annotation.Order;
13 import org.springframework.stereotype.Component; 9 import org.springframework.stereotype.Component;
14 import com.ruoyi.common.annotation.DataSource; 10 import com.ruoyi.common.annotation.DataSource;
15 -import com.ruoyi.common.utils.StringUtils;  
16 import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder; 11 import com.ruoyi.framework.datasource.DynamicDataSourceContextHolder;
17 12
18 /** 13 /**
@@ -27,22 +22,11 @@ public class DataSourceAspect @@ -27,22 +22,11 @@ public class DataSourceAspect
27 { 22 {
28 protected Logger logger = LoggerFactory.getLogger(getClass()); 23 protected Logger logger = LoggerFactory.getLogger(getClass());
29 24
30 - @Pointcut("@annotation(com.ruoyi.common.annotation.DataSource)"  
31 - + "|| @within(com.ruoyi.common.annotation.DataSource)")  
32 - public void dsPointCut() 25 + @Around("@annotation(dataSource) || @within(dataSource)")
  26 + public Object around(ProceedingJoinPoint point, DataSource dataSource) throws Throwable
33 { 27 {
34 28
35 - }  
36 -  
37 - @Around("dsPointCut()")  
38 - public Object around(ProceedingJoinPoint point) throws Throwable  
39 - {  
40 - DataSource dataSource = getDataSource(point);  
41 -  
42 - if (StringUtils.isNotNull(dataSource))  
43 - {  
44 - DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());  
45 - } 29 + DynamicDataSourceContextHolder.setDataSourceType(dataSource.value().name());
46 30
47 try 31 try
48 { 32 {
@@ -55,18 +39,4 @@ public class DataSourceAspect @@ -55,18 +39,4 @@ public class DataSourceAspect
55 } 39 }
56 } 40 }
57 41
58 - /**  
59 - * 获取需要切换的数据源  
60 - */  
61 - public DataSource getDataSource(ProceedingJoinPoint point)  
62 - {  
63 - MethodSignature signature = (MethodSignature) point.getSignature();  
64 - DataSource dataSource = AnnotationUtils.findAnnotation(signature.getMethod(), DataSource.class);  
65 - if (Objects.nonNull(dataSource))  
66 - {  
67 - return dataSource;  
68 - }  
69 -  
70 - return AnnotationUtils.findAnnotation(signature.getDeclaringType(), DataSource.class);  
71 - }  
72 } 42 }
1 package com.ruoyi.framework.aspectj; 1 package com.ruoyi.framework.aspectj;
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.stereotype.Component; 13 import org.springframework.stereotype.Component;
@@ -43,21 +38,15 @@ public class LogAspect @@ -43,21 +38,15 @@ public class LogAspect
43 { 38 {
44 private static final Logger log = LoggerFactory.getLogger(LogAspect.class); 39 private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
45 40
46 - // 配置织入点  
47 - @Pointcut("@annotation(com.ruoyi.common.annotation.Log)")  
48 - public void logPointCut()  
49 - {  
50 - }  
51 -  
52 /** 41 /**
53 * 处理完请求后执行 42 * 处理完请求后执行
54 * 43 *
55 * @param joinPoint 切点 44 * @param joinPoint 切点
56 */ 45 */
57 - @AfterReturning(pointcut = "logPointCut()", returning = "jsonResult")  
58 - public void doAfterReturning(JoinPoint joinPoint, Object jsonResult) 46 + @AfterReturning(pointcut = "@annotation(controllerLog)", returning = "jsonResult")
  47 + public void doAfterReturning(JoinPoint joinPoint, Log controllerLog, Object jsonResult)
59 { 48 {
60 - handleLog(joinPoint, null, jsonResult); 49 + handleLog(joinPoint, controllerLog, null, jsonResult);
61 } 50 }
62 51
63 /** 52 /**
@@ -66,22 +55,16 @@ public class LogAspect @@ -66,22 +55,16 @@ public class LogAspect
66 * @param joinPoint 切点 55 * @param joinPoint 切点
67 * @param e 异常 56 * @param e 异常
68 */ 57 */
69 - @AfterThrowing(value = "logPointCut()", throwing = "e")  
70 - public void doAfterThrowing(JoinPoint joinPoint, Exception e) 58 + @AfterThrowing(value = "@annotation(controllerLog)", throwing = "e")
  59 + public void doAfterThrowing(JoinPoint joinPoint, Log controllerLog, Exception e)
71 { 60 {
72 - handleLog(joinPoint, e, null); 61 + handleLog(joinPoint, controllerLog, e, null);
73 } 62 }
74 63
75 - protected void handleLog(final JoinPoint joinPoint, final Exception e, Object jsonResult) 64 + protected void handleLog(final JoinPoint joinPoint, Log controllerLog, final Exception e, Object jsonResult)
76 { 65 {
77 try 66 try
78 { 67 {
79 - // 获得注解  
80 - Log controllerLog = getAnnotationLog(joinPoint);  
81 - if (controllerLog == null)  
82 - {  
83 - return;  
84 - }  
85 68
86 // 获取当前的用户 69 // 获取当前的用户
87 LoginUser loginUser = SecurityUtils.getLoginUser(); 70 LoginUser loginUser = SecurityUtils.getLoginUser();
@@ -173,22 +156,6 @@ public class LogAspect @@ -173,22 +156,6 @@ public class LogAspect
173 } 156 }
174 157
175 /** 158 /**
176 - * 是否存在注解,如果存在就获取  
177 - */  
178 - private Log getAnnotationLog(JoinPoint joinPoint) throws Exception  
179 - {  
180 - Signature signature = joinPoint.getSignature();  
181 - MethodSignature methodSignature = (MethodSignature) signature;  
182 - Method method = methodSignature.getMethod();  
183 -  
184 - if (method != null)  
185 - {  
186 - return method.getAnnotation(Log.class);  
187 - }  
188 - return null;  
189 - }  
190 -  
191 - /**  
192 * 参数拼装 159 * 参数拼装
193 */ 160 */
194 private String argsArrayToString(Object[] paramsArray) 161 private String argsArrayToString(Object[] paramsArray)
@@ -196,11 +163,11 @@ public class LogAspect @@ -196,11 +163,11 @@ public class LogAspect
196 String params = ""; 163 String params = "";
197 if (paramsArray != null && paramsArray.length > 0) 164 if (paramsArray != null && paramsArray.length > 0)
198 { 165 {
199 - for (int i = 0; i < paramsArray.length; i++) 166 + for (Object o : paramsArray)
200 { 167 {
201 - if (StringUtils.isNotNull(paramsArray[i]) && !isFilterObject(paramsArray[i])) 168 + if (StringUtils.isNotNull(o) && !isFilterObject(o))
202 { 169 {
203 - Object jsonObj = JSON.toJSON(paramsArray[i]); 170 + Object jsonObj = JSON.toJSON(o);
204 params += jsonObj.toString() + " "; 171 params += jsonObj.toString() + " ";
205 } 172 }
206 } 173 }
@@ -225,17 +192,17 @@ public class LogAspect @@ -225,17 +192,17 @@ public class LogAspect
225 else if (Collection.class.isAssignableFrom(clazz)) 192 else if (Collection.class.isAssignableFrom(clazz))
226 { 193 {
227 Collection collection = (Collection) o; 194 Collection collection = (Collection) o;
228 - for (Iterator iter = collection.iterator(); iter.hasNext();) 195 + for (Object value : collection)
229 { 196 {
230 - return iter.next() instanceof MultipartFile; 197 + return value instanceof MultipartFile;
231 } 198 }
232 } 199 }
233 else if (Map.class.isAssignableFrom(clazz)) 200 else if (Map.class.isAssignableFrom(clazz))
234 { 201 {
235 Map map = (Map) o; 202 Map map = (Map) o;
236 - for (Iterator iter = map.entrySet().iterator(); iter.hasNext();) 203 + for (Object value : map.entrySet())
237 { 204 {
238 - Map.Entry entry = (Map.Entry) iter.next(); 205 + Map.Entry entry = (Map.Entry) value;
239 return entry.getValue() instanceof MultipartFile; 206 return entry.getValue() instanceof MultipartFile;
240 } 207 }
241 } 208 }
@@ -49,16 +49,9 @@ public class RateLimiterAspect @@ -49,16 +49,9 @@ public class RateLimiterAspect
49 this.limitScript = limitScript; 49 this.limitScript = limitScript;
50 } 50 }
51 51
52 - // 配置织入点  
53 - @Pointcut("@annotation(com.ruoyi.common.annotation.RateLimiter)")  
54 - public void rateLimiterPointCut() 52 + @Before("@annotation(rateLimiter)")
  53 + public void doBefore(JoinPoint point, RateLimiter rateLimiter) throws Throwable
55 { 54 {
56 - }  
57 -  
58 - @Before("rateLimiterPointCut()")  
59 - public void doBefore(JoinPoint point) throws Throwable  
60 - {  
61 - RateLimiter rateLimiter = getAnnotationRateLimiter(point);  
62 String key = rateLimiter.key(); 55 String key = rateLimiter.key();
63 int time = rateLimiter.time(); 56 int time = rateLimiter.time();
64 int count = rateLimiter.count(); 57 int count = rateLimiter.count();
@@ -84,22 +77,6 @@ public class RateLimiterAspect @@ -84,22 +77,6 @@ public class RateLimiterAspect
84 } 77 }
85 } 78 }
86 79
87 - /**  
88 - * 是否存在注解,如果存在就获取  
89 - */  
90 - private RateLimiter getAnnotationRateLimiter(JoinPoint joinPoint)  
91 - {  
92 - Signature signature = joinPoint.getSignature();  
93 - MethodSignature methodSignature = (MethodSignature) signature;  
94 - Method method = methodSignature.getMethod();  
95 -  
96 - if (method != null)  
97 - {  
98 - return method.getAnnotation(RateLimiter.class);  
99 - }  
100 - return null;  
101 - }  
102 -  
103 public String getCombineKey(RateLimiter rateLimiter, JoinPoint point) 80 public String getCombineKey(RateLimiter rateLimiter, JoinPoint point)
104 { 81 {
105 StringBuffer stringBuffer = new StringBuffer(rateLimiter.key()); 82 StringBuffer stringBuffer = new StringBuffer(rateLimiter.key());