前言
作為一個寫java的使用最多的輕量級框架莫過于spring,不管是老項目用到的springmvc,還是現在流行的springboot,都離不開spring的一些操作,我在面試的時候問的最多的spring的問題就是我們在平常的項目中使用spring最多的有哪幾個點
在我看來無非就兩個
- spring的bean管理,說的高大上一點就是spring的ioc,di
- spring的AOP
spring是一個很強大的輕量級框架,功能遠不止這兩點,但是我們用的最多的就是這兩點。
spring bean 管理
想我們常用的 @Controller @Service @Component 等等都是將我們的bean交給spring管理,我們在獲取bean的時候就直接通過 @Resource 就可以獲取,當然@Resource 不是spring的,@Autowired 才是spring的,這樣我們可以很方便的管理我們的各種bean,使用起來也很方便,不用到處new
springAOP
這個應該是spring面試最常問道的問題了,我面試的時候一般不直接問,我會說一個場景,如果面試者使用過aop立馬就可以回答出來。
有這么一個場景,一個系統已經開發完成了,而且已經上線運行了一段時間,很穩定了,現在要加一個功能,就是想收集用戶的操作日志,操作日志要比較細致,比如 某某人,在哪個時間點,操作了哪個模塊,請求的參數是什么樣子的,操作結果如何,等一些比較細致的操作。很多面試者第一時間想到的就是使用過濾器,試想一下,過濾器真的能做到記錄這么細致的內容嗎?有的可能會想到,我們定義一個公共的方法,所有需要記錄日志的地方都去調用這個方法,等等。其實這些都不好,最好的當然是使用aop,使用aop侵入性最小,系統已經穩定運行了,不能去動之前的代碼,我們做個aop就可以了,對原來的代碼幾乎0侵入,對系統影響最小。
那aop實現的方式有哪幾種呢?aop實現的步驟又是怎么樣的呢?aop還能做些什么呢?這些問題如果在實際項目中使用過,一定能回答出來,如果沒有使用過,估計有點難回答,這些都是項目框架的東西,很多公司的項目這一塊已經封裝好了,很多人直接一直在用,但是沒有去查看源碼,就不知道具體的實現,實現起來其實也很簡單。
接下來我們就以一個小例子來說明下如何使用aop
aop總結起來最常用的就兩種方式
1、采用聲明的方式來實現(基于XML) 胡子眉毛一把抓,哈哈
2、是采用注解的方式來實現(基于AspectJ)精準定位
我習慣使用注解的方式,更加靈活,使用起來也方便,接下來就以注解的方式來講下如何使用aop來做權限校驗,數據校驗,操作日志記錄
申明切面
我們使用springaop,首先要將定義的類交給spring管理,然后使用aspectj 定義切面,我們要額外引入
aspectj
1
2
3
4
5
|
<dependency> <groupId>org.aspectj</groupId> <artifactId>aspectjweaver</artifactId> <version> 1.8 . 9 </version> </dependency> |
1
2
3
4
5
|
@Component @Aspect public class OperationInterceptor { } |
定義切點
我們采用注解的方式,那么我們首先要申明一個注解
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
@Target ({ElementType.METHOD, ElementType.TYPE}) @Retention (RetentionPolicy.RUNTIME) @Documented @Mapping public @interface GlobalInterceptor { /** * 權限編碼 */ PermissionCodeEnum permissionCode(); /** * 操作模塊 * * @return */ OperModuleEnum opObject() default OperModuleEnum.NO_OBJECT; /** * 操作描述 * * @return */ String opDescription() default "" ; } |
然后在我們的切面中定義切點
1
2
3
4
5
6
7
8
9
|
@Component @Aspect public class OperationInterceptor { @Pointcut ( "@annotation(com.xx.xx.GlobalInterceptor)" ) private void opMethods() { } } |
定義通知類型
我們要輸入還想要輸出,那么我們就要將目標方法包圍,所以使用around
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
@Component @Aspect public class OperationInterceptor { @Pointcut ( "@annotation(com.xx.xx.GlobalInterceptor)" ) private void opMethods() { } @Around ( "opMethods()" ) public Object doMethod(ProceedingJoinPoint point) throws BusinessException { //TODO 業務代碼 return null ; } } |
這樣一個完整的切面就定義好了
使用切面
在我們的controller中直接使用,我們拿一個登錄來講
1
2
3
4
5
|
@RequestMapping ( "/login" ) @GlobalInterceptor (permissionCode = PermissionCodeEnum.NO_PERMISSION, opObject = OperModuleEnum.OBJECT_LOGIN, opDescription = "登錄賬號:#{#param1}" ) public AjaxResponseVO login(HttpSession session, @VerifyParam (required = true ) String account, @VerifyParam (required = true ) String password, @VerifyParam (required = true ) String checkCode){ } |
我們要傳入 切面注解需要的參數
permissionCode 權限編碼,我這里定義的是一個枚舉,類型自己根據實際情況定義
opObject 操作模塊
opDescription 操作描述,這里簡單的組織下描述文字,參數的地方使用占位符,到時候根據占位符index獲取具體的參數
這樣我們在切面中就可以拿到這些參數
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
@Component @Aspect public class OperationInterceptor { @Pointcut ( "@annotation(com.xx.xx.GlobalInterceptor)" ) private void opMethods() { } @Around ( "opMethods()" ) public Object doMethod(ProceedingJoinPoint point) throws BusinessException { Object obj = null ; try { /** * 獲取登錄信息 */ SessionUserDto sessionUserDto = getSessionUser(); /** * 獲取目標切點 */ Object target = point.getTarget(); /** * 獲取參數 */ Object[] arguments = point.getArgs(); /** * 獲取方法名 */ String method = point.getSignature().getName(); /** * 獲取參數類型 */ Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes(); /** * 獲取具體的方法 */ Method m = target.getClass().getMethod(method, parameterTypes); GlobalInterceptor interceptor = m.getAnnotation(GlobalInterceptor. class ); if ( null == interceptor) { return obj; } /** * 校驗權限 */ if (sessionUserDto != null ) { validatePermission(interceptor, sessionUserDto); } /** * 校驗參數 */ validateParams(interceptor, m, arguments); /** * 獲取描述信息,這里在執行方法之前組織好描述信息,當參數是傳值引用時 執行具體方法后,會改變原始參數對象值 */ String description = getDescription(arguments, interceptor.opDescription()); /** * 執行操作 */ obj = point.proceed(); /** * 記錄日志 */ wirteLog(obj, description, interceptor, sessionUserDto); } catch (BusinessException e) { logger.error( "全局攔截器異常" , e); throw e; } catch (Exception e) { logger.error( "全局攔截器異常" , e); } catch (Throwable e) { logger.error( "全局攔截器異常" , e); } return obj; } } |
這里只貼出了部分代碼,完整的代碼可以到這里獲取
總結
回到最開始的問題,這樣我們實現了一個對原有系統侵入極小,然后又實現了操作日志的解決方案。 我們使用spring的aop非常簡單,我們使用aop結合反射,可以做很多事情。aop對代碼的侵入非常小,不需要動原來的代碼,只需要在原有的方法上加一個注解就可以完成對系統的改造,加權限,加日志 等等一系列操作。
到此這篇關于springAop實現權限管理數據校驗操作日志的場景分析的文章就介紹到這了,更多相關springAop權限管理數據校驗內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!
原文鏈接:https://blog.csdn.net/weixin_45574655/article/details/114776505