Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
cocktail-party-server
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
cocktail-party
cocktail-party-server
Commits
26f0737c
提交
26f0737c
authored
9月 20, 2021
作者:
RuoYi
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
防重提交注解支持配置间隔时间/提示消息
上级
ac942428
隐藏空白字符变更
内嵌
并排
正在显示
3 个修改的文件
包含
17 行增加
和
20 行删除
+17
-20
RepeatSubmit.java
...c/main/java/com/ruoyi/common/annotation/RepeatSubmit.java
+8
-0
RepeatSubmitInterceptor.java
.../ruoyi/framework/interceptor/RepeatSubmitInterceptor.java
+3
-3
SameUrlDataInterceptor.java
...yi/framework/interceptor/impl/SameUrlDataInterceptor.java
+6
-17
没有找到文件。
ruoyi-common/src/main/java/com/ruoyi/common/annotation/RepeatSubmit.java
浏览文件 @
26f0737c
...
@@ -19,5 +19,13 @@ import java.lang.annotation.Target;
...
@@ -19,5 +19,13 @@ import java.lang.annotation.Target;
@Documented
@Documented
public
@interface
RepeatSubmit
public
@interface
RepeatSubmit
{
{
/**
* 间隔时间(ms),小于此时间视为重复提交
*/
public
int
interval
()
default
5000
;
/**
* 提示消息
*/
public
String
message
()
default
"不允许重复提交,请稍后再试"
;
}
}
ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/RepeatSubmitInterceptor.java
浏览文件 @
26f0737c
...
@@ -29,9 +29,9 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
...
@@ -29,9 +29,9 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
RepeatSubmit
annotation
=
method
.
getAnnotation
(
RepeatSubmit
.
class
);
RepeatSubmit
annotation
=
method
.
getAnnotation
(
RepeatSubmit
.
class
);
if
(
annotation
!=
null
)
if
(
annotation
!=
null
)
{
{
if
(
this
.
isRepeatSubmit
(
request
))
if
(
this
.
isRepeatSubmit
(
request
,
annotation
))
{
{
AjaxResult
ajaxResult
=
AjaxResult
.
error
(
"不允许重复提交,请稍后再试"
);
AjaxResult
ajaxResult
=
AjaxResult
.
error
(
annotation
.
message
()
);
ServletUtils
.
renderString
(
response
,
JSONObject
.
toJSONString
(
ajaxResult
));
ServletUtils
.
renderString
(
response
,
JSONObject
.
toJSONString
(
ajaxResult
));
return
false
;
return
false
;
}
}
...
@@ -51,5 +51,5 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
...
@@ -51,5 +51,5 @@ public abstract class RepeatSubmitInterceptor extends HandlerInterceptorAdapter
* @return
* @return
* @throws Exception
* @throws Exception
*/
*/
public
abstract
boolean
isRepeatSubmit
(
HttpServletRequest
request
);
public
abstract
boolean
isRepeatSubmit
(
HttpServletRequest
request
,
RepeatSubmit
annotation
);
}
}
ruoyi-framework/src/main/java/com/ruoyi/framework/interceptor/impl/SameUrlDataInterceptor.java
浏览文件 @
26f0737c
...
@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
...
@@ -8,6 +8,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.beans.factory.annotation.Value
;
import
org.springframework.stereotype.Component
;
import
org.springframework.stereotype.Component
;
import
com.alibaba.fastjson.JSONObject
;
import
com.alibaba.fastjson.JSONObject
;
import
com.ruoyi.common.annotation.RepeatSubmit
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.constant.Constants
;
import
com.ruoyi.common.core.redis.RedisCache
;
import
com.ruoyi.common.core.redis.RedisCache
;
import
com.ruoyi.common.filter.RepeatedlyRequestWrapper
;
import
com.ruoyi.common.filter.RepeatedlyRequestWrapper
;
...
@@ -35,21 +36,9 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
...
@@ -35,21 +36,9 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
@Autowired
@Autowired
private
RedisCache
redisCache
;
private
RedisCache
redisCache
;
/**
* 间隔时间,单位:秒 默认10秒
*
* 两次相同参数的请求,如果间隔时间大于该参数,系统不会认定为重复提交的数据
*/
private
int
intervalTime
=
10
;
public
void
setIntervalTime
(
int
intervalTime
)
{
this
.
intervalTime
=
intervalTime
;
}
@SuppressWarnings
(
"unchecked"
)
@SuppressWarnings
(
"unchecked"
)
@Override
@Override
public
boolean
isRepeatSubmit
(
HttpServletRequest
request
)
public
boolean
isRepeatSubmit
(
HttpServletRequest
request
,
RepeatSubmit
annotation
)
{
{
String
nowParams
=
""
;
String
nowParams
=
""
;
if
(
request
instanceof
RepeatedlyRequestWrapper
)
if
(
request
instanceof
RepeatedlyRequestWrapper
)
...
@@ -87,7 +76,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
...
@@ -87,7 +76,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
if
(
sessionMap
.
containsKey
(
url
))
if
(
sessionMap
.
containsKey
(
url
))
{
{
Map
<
String
,
Object
>
preDataMap
=
(
Map
<
String
,
Object
>)
sessionMap
.
get
(
url
);
Map
<
String
,
Object
>
preDataMap
=
(
Map
<
String
,
Object
>)
sessionMap
.
get
(
url
);
if
(
compareParams
(
nowDataMap
,
preDataMap
)
&&
compareTime
(
nowDataMap
,
preDataMap
))
if
(
compareParams
(
nowDataMap
,
preDataMap
)
&&
compareTime
(
nowDataMap
,
preDataMap
,
annotation
.
interval
()
))
{
{
return
true
;
return
true
;
}
}
...
@@ -95,7 +84,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
...
@@ -95,7 +84,7 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
}
}
Map
<
String
,
Object
>
cacheMap
=
new
HashMap
<
String
,
Object
>();
Map
<
String
,
Object
>
cacheMap
=
new
HashMap
<
String
,
Object
>();
cacheMap
.
put
(
url
,
nowDataMap
);
cacheMap
.
put
(
url
,
nowDataMap
);
redisCache
.
setCacheObject
(
cacheRepeatKey
,
cacheMap
,
intervalTime
,
TimeUnit
.
SECONDS
);
redisCache
.
setCacheObject
(
cacheRepeatKey
,
cacheMap
,
annotation
.
interval
(),
TimeUnit
.
MILLI
SECONDS
);
return
false
;
return
false
;
}
}
...
@@ -112,11 +101,11 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
...
@@ -112,11 +101,11 @@ public class SameUrlDataInterceptor extends RepeatSubmitInterceptor
/**
/**
* 判断两次间隔时间
* 判断两次间隔时间
*/
*/
private
boolean
compareTime
(
Map
<
String
,
Object
>
nowMap
,
Map
<
String
,
Object
>
preMap
)
private
boolean
compareTime
(
Map
<
String
,
Object
>
nowMap
,
Map
<
String
,
Object
>
preMap
,
int
interval
)
{
{
long
time1
=
(
Long
)
nowMap
.
get
(
REPEAT_TIME
);
long
time1
=
(
Long
)
nowMap
.
get
(
REPEAT_TIME
);
long
time2
=
(
Long
)
preMap
.
get
(
REPEAT_TIME
);
long
time2
=
(
Long
)
preMap
.
get
(
REPEAT_TIME
);
if
((
time1
-
time2
)
<
(
this
.
intervalTime
*
1000
)
)
if
((
time1
-
time2
)
<
interval
)
{
{
return
true
;
return
true
;
}
}
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论