Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
C
cloud-backend
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
framework
cloud-backend
Commits
cbadd4c5
Commit
cbadd4c5
authored
Jul 10, 2021
by
袁伟铭
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
1.0.0
parent
1442e125
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
46 additions
and
13 deletions
+46
-13
common-utils/src/main/java/com/zq/common/annotation/Limit.java
+30
-6
common-utils/src/main/java/com/zq/common/config/limit/LimitAspect.java
+13
-6
user-server/src/main/java/com/zq/user/controller/app/UserController.java
+3
-1
No files found.
common-utils/src/main/java/com/zq/common/annotation/Limit.java
View file @
cbadd4c5
...
...
@@ -30,22 +30,46 @@ import java.lang.annotation.Target;
@Retention
(
RetentionPolicy
.
RUNTIME
)
public
@interface
Limit
{
// 限制类型
/**
* 限流类型
*
* @return
*/
LimitType
limitType
()
default
LimitType
.
IP
;
// 资源名称,用于描述接口功能
/**
* 资源名称,用于描述接口功能, 不设置将默认为调用方法名
*
* @return
*/
String
name
()
default
""
;
// 资源 key
/**
* 缓存key, 不设置将默认为调用方法名
*
* @return
*/
String
key
()
default
""
;
// key prefix
/**
* 前缀前面还会有一个{@code BaseCacheKeys.PREFIX}的项目前缀, 默认为"rate-limit"
*
* @return
*/
String
prefix
()
default
""
;
// 时间的,单位秒
/**
* 时间范围,单位秒, 在这个时间范围内可以调用多少次
*
* @return
*/
int
period
()
default
1
;
// 限制访问次数
/**
* 限制访问次数
*
* @return
*/
int
count
()
default
3
;
/**
...
...
common-utils/src/main/java/com/zq/common/config/limit/LimitAspect.java
View file @
cbadd4c5
...
...
@@ -67,10 +67,13 @@ public class LimitAspect {
Method
signatureMethod
=
signature
.
getMethod
();
Limit
limit
=
signatureMethod
.
getAnnotation
(
Limit
.
class
);
String
key
=
buildLimitKey
(
limit
,
joinPoint
,
signatureMethod
);
log
.
debug
(
"限流缓存KEY: {}"
,
key
);
String
limitKey
=
buildLimitKey
(
limit
,
joinPoint
,
signatureMethod
);
log
.
debug
(
">> 限流缓存KEY: {}"
,
limitKey
);
if
(
limitKey
==
null
||
limitKey
.
trim
().
length
()
==
0
)
{
return
;
}
List
<
String
>
keys
=
Collections
.
singletonList
(
k
ey
);
List
<
String
>
keys
=
Collections
.
singletonList
(
limitK
ey
);
String
luaScript
=
buildLuaScript
();
RedisScript
<
Long
>
redisScript
=
new
DefaultRedisScript
<>(
luaScript
,
Long
.
class
);
Long
count
=
stringRedisTemplate
.
execute
(
redisScript
,
keys
,
String
.
valueOf
(
limit
.
count
()),
String
.
valueOf
(
limit
.
period
()));
...
...
@@ -79,7 +82,7 @@ public class LimitAspect {
String
name
=
limit
.
name
();
name
=
StringUtils
.
isNotBlank
(
name
)
?
name
:
signatureMethod
.
getName
();
logger
.
info
(
"第{}次访问key
为 {},描述为 [{}] 的接口"
,
count
,
keys
,
name
);
logger
.
debug
(
"第{}次访问,KEY
为 {},描述为 [{}] 的接口"
,
count
,
keys
,
name
);
}
/**
...
...
@@ -146,7 +149,11 @@ public class LimitAspect {
log
.
warn
(
">> 未找到对象,限流失败: {}"
,
joinPoint
);
break
;
}
String
fieldValue
=
getPojoField
(
field
,
args
[
0
]);
String
fieldValue
=
getPojoField
(
args
[
0
],
field
);
if
(
StringUtils
.
isBlank
(
fieldValue
))
{
log
.
warn
(
">> 对象字段值为空,请检查限流字段是否准确,限流失败: {}"
,
joinPoint
);
break
;
}
limitKey
=
BaseCacheKeys
.
rateLimitKey
(
LimitType
.
POJO_FIELD
,
prefix
,
key
,
fieldValue
);
break
;
case
PARAM:
...
...
@@ -174,7 +181,7 @@ public class LimitAspect {
||
(
param
instanceof
Byte
);
}
private
String
getPojoField
(
String
field
,
Object
pojo
)
{
private
String
getPojoField
(
Object
pojo
,
String
field
)
{
try
{
JSONObject
object
=
JSON
.
parseObject
(
JSON
.
toJSONString
(
pojo
));
return
object
.
getString
(
field
);
...
...
user-server/src/main/java/com/zq/user/controller/app/UserController.java
View file @
cbadd4c5
...
...
@@ -2,6 +2,7 @@ package com.zq.user.controller.app;
import
com.zq.common.annotation.Limit
;
import
com.zq.common.config.limit.LimitType
;
import
com.zq.common.utils.AssertUtils
;
import
com.zq.common.utils.ValidateUtil
;
import
com.zq.common.vo.ApiTokenVo
;
...
...
@@ -25,6 +26,8 @@ public class UserController {
private
final
UserService
userService
;
// 30秒只能访问一次
@Limit
(
limitType
=
LimitType
.
PARAM
,
keyParamIndex
=
0
,
period
=
30
,
count
=
1
,
name
=
"发送验证码"
,
errMsg
=
"请稍后再试!"
)
@ApiOperation
(
"发送验证码"
)
@GetMapping
(
value
=
"/sendCode"
)
public
ResultVo
sendCode
(
String
phone
)
{
...
...
@@ -71,7 +74,6 @@ public class UserController {
return
ResultVo
.
success
(
userService
.
passwdLogin
(
vo
));
}
@Limit
(
count
=
1
)
@ApiOperation
(
"获取用户信息"
)
@GetMapping
(
value
=
"/getUserInfo"
)
public
ResultVo
getUserInfo
(
@RequestParam
String
userId
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment