Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
C
cms
概览
概览
详情
活动
周期分析
版本库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
统计图
问题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程表
图表
维基
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
iuav
cms
Commits
9d0a885c
提交
9d0a885c
authored
9月 14, 2023
作者:
xiaowang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
修改配置
上级
137f9a44
隐藏空白字符变更
内嵌
并排
正在显示
6 个修改的文件
包含
120 行增加
和
4 行删除
+120
-4
pom.xml
csm-service/cms-service-user/pom.xml
+6
-0
PreventDuplication.java
...ain/java/com/mmc/iuav/user/config/PreventDuplication.java
+15
-0
PreventDuplicationAspect.java
...va/com/mmc/iuav/user/config/PreventDuplicationAspect.java
+94
-0
application-dev.yml
...e/cms-service-user/src/main/resources/application-dev.yml
+1
-1
application-local.yml
...cms-service-user/src/main/resources/application-local.yml
+3
-2
application-prod.yml
.../cms-service-user/src/main/resources/application-prod.yml
+1
-1
没有找到文件。
csm-service/cms-service-user/pom.xml
浏览文件 @
9d0a885c
...
@@ -35,6 +35,12 @@
...
@@ -35,6 +35,12 @@
<artifactId>
spring-boot-starter-actuator
</artifactId>
<artifactId>
spring-boot-starter-actuator
</artifactId>
</dependency>
</dependency>
<dependency>
<groupId>
org.springframework.boot
</groupId>
<artifactId>
spring-boot-starter-aop
</artifactId>
</dependency>
<!-- mybatis驱动&Druid数据源-start -->
<!-- mybatis驱动&Druid数据源-start -->
<dependency>
<dependency>
<groupId>
com.alibaba
</groupId>
<groupId>
com.alibaba
</groupId>
...
...
csm-service/cms-service-user/src/main/java/com/mmc/iuav/user/config/PreventDuplication.java
0 → 100644
浏览文件 @
9d0a885c
package
com
.
mmc
.
iuav
.
user
.
config
;
import
java.lang.annotation.*
;
/**
* @author 23214
*/
@Target
(
ElementType
.
METHOD
)
@Retention
(
RetentionPolicy
.
RUNTIME
)
@Documented
public
@interface
PreventDuplication
{
String
value
()
default
"value"
;
long
expireSeconds
()
default
10
;
}
csm-service/cms-service-user/src/main/java/com/mmc/iuav/user/config/PreventDuplicationAspect.java
0 → 100644
浏览文件 @
9d0a885c
package
com
.
mmc
.
iuav
.
user
.
config
;
import
cn.hutool.core.lang.Assert
;
import
com.alibaba.fastjson2.JSONObject
;
import
com.mmc.iuav.response.ResultBody
;
import
lombok.extern.slf4j.Slf4j
;
import
org.aspectj.lang.ProceedingJoinPoint
;
import
org.aspectj.lang.annotation.Around
;
import
org.aspectj.lang.annotation.Aspect
;
import
org.aspectj.lang.annotation.Pointcut
;
import
org.aspectj.lang.reflect.MethodSignature
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.data.redis.core.RedisTemplate
;
import
org.springframework.data.redis.core.script.DigestUtils
;
import
org.springframework.stereotype.Component
;
import
org.springframework.web.context.request.RequestContextHolder
;
import
org.springframework.web.context.request.ServletRequestAttributes
;
import
javax.servlet.http.HttpServletRequest
;
import
java.lang.reflect.Method
;
import
java.util.Objects
;
import
java.util.concurrent.TimeUnit
;
@Aspect
@Component
@Slf4j
public
class
PreventDuplicationAspect
{
@Autowired
private
RedisTemplate
redisTemplate
;
@Pointcut
(
"@annotation(com.mmc.iuav.user.config.PreventDuplication)"
)
public
void
preventDuplication
()
{
}
// 使用@Around注解定义一个环绕通知,拦截带有@PreventDuplication注解的方法
@Around
(
"preventDuplication()"
)
public
Object
before
(
ProceedingJoinPoint
joinPoint
)
throws
Exception
{
// 获取当前请求的属性
ServletRequestAttributes
attributes
=
(
ServletRequestAttributes
)
RequestContextHolder
.
getRequestAttributes
();
// 获取当前请求
HttpServletRequest
request
=
attributes
.
getRequest
();
// 确保请求不为空
Assert
.
notNull
(
request
,
"request cannot be null."
);
// 获取被拦截方法的签名,并转换为Method对象
Method
method
=
((
MethodSignature
)
joinPoint
.
getSignature
()).
getMethod
();
// 获取被拦截方法上的@PreventDuplication注解
PreventDuplication
annotation
=
method
.
getAnnotation
(
PreventDuplication
.
class
);
// 从请求头中获取token
String
token
=
request
.
getHeader
(
"token"
);
// 构造Redis的键,由前缀、token和方法签名组成
String
redisKey
=
"prevent_duplication_prefix"
.
concat
(
token
).
concat
(
getMethodSign
(
method
,
joinPoint
.
getArgs
()));
// 构造Redis的值,由键、注解的值和一个固定字符串组成
String
redisValue
=
redisKey
.
concat
(
annotation
.
value
()).
concat
(
"submit duplication"
);
// 检查Redis中是否已经存在该键
if
(!
redisTemplate
.
hasKey
(
redisKey
))
{
// 如果不存在,则将键值对存入Redis,并设置过期时间
redisTemplate
.
opsForValue
().
set
(
redisKey
,
redisValue
,
annotation
.
expireSeconds
(),
TimeUnit
.
SECONDS
);
try
{
// 执行被拦截的方法
return
joinPoint
.
proceed
();
}
catch
(
Throwable
throwable
)
{
// 如果执行过程中发生异常,则从Redis中删除该键值对,并抛出异常
redisTemplate
.
delete
(
redisKey
);
throw
new
RuntimeException
(
throwable
);
}
}
else
{
// 如果已经存在,则返回错误信息,防止重复提交
return
ResultBody
.
error
(
"请勿重复提交"
);
}
}
// 定义一个私有方法,用于获取方法的签名,包括方法名和参数列表
private
String
getMethodSign
(
Method
method
,
Object
...
args
)
{
StringBuilder
sb
=
new
StringBuilder
(
method
.
toString
());
for
(
Object
arg
:
args
)
{
sb
.
append
(
toString
(
arg
));
}
return
DigestUtils
.
sha1DigestAsHex
(
sb
.
toString
());
}
// 定义一个私有方法,用于将参数转换为字符串
private
String
toString
(
Object
arg
)
{
if
(
Objects
.
isNull
(
arg
))
{
return
"null"
;
}
if
(
arg
instanceof
Number
)
{
return
arg
.
toString
();
}
return
JSONObject
.
toJSONString
(
arg
);
}
}
csm-service/cms-service-user/src/main/resources/application-dev.yml
浏览文件 @
9d0a885c
...
@@ -89,7 +89,7 @@ fdd:
...
@@ -89,7 +89,7 @@ fdd:
appid
:
407664
appid
:
407664
appkey
:
q146q3Cjmi7Y6BVl8jtZK9pH
appkey
:
q146q3Cjmi7Y6BVl8jtZK9pH
version
:
2
version
:
2
host
:
https://testapi
.fadada.com:8443
/api/
host
:
https://testapi
06.fadada.com
/api/
mmc-url
:
https://test.iuav.shop/userapp/
mmc-url
:
https://test.iuav.shop/userapp/
iuav
:
iuav
:
...
...
csm-service/cms-service-user/src/main/resources/application-local.yml
浏览文件 @
9d0a885c
...
@@ -10,8 +10,9 @@ spring:
...
@@ -10,8 +10,9 @@ spring:
type
:
com.alibaba.druid.pool.DruidDataSource
type
:
com.alibaba.druid.pool.DruidDataSource
redis
:
redis
:
database
:
1
database
:
1
host
:
127.0.0.1
host
:
r-wz9ke310fs684hacn1pd.redis.rds.aliyuncs.com
port
:
6379
port
:
6379
password
:
MMC@2022&REDIS
#rabbitMQ
#rabbitMQ
rabbitmq
:
rabbitmq
:
host
:
amqp-cn-zvp2ozhnj001.cn-shenzhen.amqp-0.net.mq.amqp.aliyuncs.com
host
:
amqp-cn-zvp2ozhnj001.cn-shenzhen.amqp-0.net.mq.amqp.aliyuncs.com
...
@@ -52,7 +53,7 @@ fdd:
...
@@ -52,7 +53,7 @@ fdd:
appid
:
407664
appid
:
407664
appkey
:
q146q3Cjmi7Y6BVl8jtZK9pH
appkey
:
q146q3Cjmi7Y6BVl8jtZK9pH
version
:
2
version
:
2
host
:
https://testapi
.fadada.com:8443
/api/
host
:
https://testapi
06.fadada.com
/api/
mmc-url
:
https://test.iuav.shop/userapp/
mmc-url
:
https://test.iuav.shop/userapp/
iuav
:
iuav
:
...
...
csm-service/cms-service-user/src/main/resources/application-prod.yml
浏览文件 @
9d0a885c
...
@@ -81,7 +81,7 @@ fdd:
...
@@ -81,7 +81,7 @@ fdd:
appid
:
407664
appid
:
407664
appkey
:
q146q3Cjmi7Y6BVl8jtZK9pH
appkey
:
q146q3Cjmi7Y6BVl8jtZK9pH
version
:
2
version
:
2
host
:
https://testapi
.fadada.com:8443
/api/
host
:
https://testapi
06.fadada.com
/api/
mmc-url
:
https://test.iuav.shop/userapp/
mmc-url
:
https://test.iuav.shop/userapp/
iuav
:
iuav
:
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论