srping注解方式防止重復提交原理分析,供大家參考,具體內容如下
方法一: springmvc使用token
使用token的邏輯是,給所有的url加一個攔截器,在攔截器里面用java的uuid生成一個隨機的uuid并把這個uuid放到session里面,然后在瀏覽器做數據提交的時候將此uuid提交到服務器。服務器在接收到此uuid后,檢查一下該uuid是否已經被提交,如果已經被提交,則不讓邏輯繼續執行下去…**
1 首先要定義一個annotation: 用@retention 和 @target 標注接口
1
2
3
4
5
6
|
@target (elementtype.method) @retention (retentionpolicy.runtime) public @interface token { boolean save() default false ; boolean remove() default false ; } |
2 定義攔截器tokeninterceptor:
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
|
public class tokeninterceptor extends handlerinterceptoradapter { @override public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception { if (handler instanceof handlermethod) { handlermethod handlermethod = (handlermethod) handler; method method = handlermethod.getmethod(); token annotation = method.getannotation(token. class ); if (annotation != null ) { boolean needsavesession = annotation.save(); if (needsavesession) { request.getsession( false ).setattribute( "token" , uuid.randomuuid().tostring()); } boolean needremovesession = annotation.remove(); if (needremovesession) { if (isrepeatsubmit(request)) { return false ; } request.getsession( false ).removeattribute( "token" ); } } return true ; } else { return super .prehandle(request, response, handler); } } private boolean isrepeatsubmit(httpservletrequest request) { string servertoken = (string) request.getsession( false ).getattribute( "token" ); if (servertoken == null ) { return true ; } string clinettoken = request.getparameter( "token" ); if (clinettoken == null ) { return true ; } if (!servertoken.equals(clinettoken)) { return true ; } return false ; } } |
spring mvc的配置文件里加入:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
<mvc:interceptors> <!-- 使用bean定義一個interceptor,直接定義在mvc:interceptors根下面的interceptor將攔截所有的請求 --> <mvc:interceptor> <mvc:mapping path= "/**" /> <!-- 定義在mvc:interceptor下面的表示是對特定的請求才進行攔截的 --> <bean class = "****包名****.tokeninterceptor" /> </mvc:interceptor> </mvc:interceptors> @requestmapping ( "/add.jspf" ) @token (save= true ) public string add() { //省略 return tpl_base + "index" ; } @requestmapping ( "/save.jspf" ) @token (remove= true ) public void save() { //省略 } |
用法:
在controller類的用于定向到添加/修改操作的方法上增加自定義的注解類 @token(save=true)
在controller類的用于表單提交保存的的方法上增加@token(remove=true)
在表單中增加 用于存儲token,每次需要報token值傳入到后臺類,用于從緩存對比是否是重復提交操作
方法二:springboot中用注解方式
每次操作,生成的key存放于緩存中,比如用google的gruava或者redis做緩存
定義annotation類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
@target (elementtype.method) @retention (retentionpolicy.runtime) @documented @inherited public @interface locallock { /** * @author fly */ string key() default "" ; /** * 過期時間 todo 由于用的 guava 暫時就忽略這屬性吧 集成 redis 需要用到 * * @author fly */ int expire() default 5 ; } |
設置攔截類
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
|
@aspect @configuration public class lockmethodinterceptor { private static final cache<string, object> caches = cachebuilder.newbuilder() // 最大緩存 100 個 .maximumsize( 1000 ) // 設置寫緩存后 5 秒鐘過期 .expireafterwrite( 5 , timeunit.seconds) .build(); @around ( "execution(public * *(..)) && @annotation(com.demo.testduplicate.test1.locallock)" ) public object interceptor(proceedingjoinpoint pjp) { methodsignature signature = (methodsignature) pjp.getsignature(); method method = signature.getmethod(); locallock locallock = method.getannotation(locallock. class ); string key = getkey(locallock.key(), pjp.getargs()); if (!stringutils.isempty(key)) { if (caches.getifpresent(key) != null ) { throw new runtimeexception( "請勿重復請求" ); } // 如果是第一次請求,就將 key 當前對象壓入緩存中 caches.put(key, key); } try { return pjp.proceed(); } catch (throwable throwable) { throw new runtimeexception( "服務器異常" ); } finally { // todo 為了演示效果,這里就不調用 caches.invalidate(key); 代碼了 } } /** * key 的生成策略,如果想靈活可以寫成接口與實現類的方式(todo 后續講解) * * @param keyexpress 表達式 * @param args 參數 * @return 生成的key */ private string getkey(string keyexpress, object[] args) { for ( int i = 0 ; i < args.length; i++) { keyexpress = keyexpress.replace( "arg[" + i + "]" , args[i].tostring()); } return keyexpress; } } |
controller類引用
1
2
3
4
5
6
7
8
9
10
|
@restcontroller @requestmapping ( "/books" ) public class bookcontroller { @locallock (key = "book:arg[0]" ) @getmapping public string save( @requestparam string token) { return "success - " + token; } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/xdy3008/article/details/84452587