springboot项目api防刷

aop+redis实现ip请求方法防刷

  1. 引入依赖
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<!-- aop -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>

<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
  1. 修改配置文件
1
2
3
4
5
6
7
spring:
redis:
host: localhost
port: 6379
database: 1
password:
timeout: 5000ms
  1. 创建一个限流的注解
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)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Limiter {

/**
* 从第一次访问接口的时间到cycle周期时间内,无法超过frequency次
*/
int frequency() default 20;

/**
* 周期时间,单位ms:
* 默认周期时间为一分钟
*/
long cycle() default 60 * 1000;

/**
* 返回的错误信息
*/
String message() default "请求过于频繁";

/**
* 到期时间,单位s:
* 如果在cycle周期时间内超过frequency次,则默认1分钟内无法继续访问
*/
long expireTime() default 1 * 60;
}
  1. 创建aop
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
@Aspect
@Component
public class LimitingAspect {
private static final String LIMITING_KEY = "limiting:%s:%s";
private static final String LIMITING_BEGINTIME = "beginTime";
private static final String LIMITING_EXFREQUENCY = "exFrequency";

@Autowired
private RedisTemplate<String, Object> redisTemplate;

@Pointcut("@annotation(limiter)")
public void pointcut(Limiter limiter) {
}

@Around("pointcut(limiter)")
public Object around(ProceedingJoinPoint pjp, Limiter limiter) throws Throwable {
//获取请求的ip和方法
String ipAddress = WebUtil.getIpAddress();
String methodName = pjp.getSignature().toLongString();

//获取方法的访问周期和频率
long cycle = limiter.cycle();
int frequency = limiter.frequency();
long currentTime = System.currentTimeMillis();

//获取redis中周期内第一次访问方法的时间和执行的次数
Long beginTimeLong = (Long) redisTemplate.opsForHash().get(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_BEGINTIME);
Integer exFrequencyLong = (Integer) redisTemplate.opsForHash().get(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_EXFREQUENCY);

long beginTime = beginTimeLong == null ? 0L : beginTimeLong;
int exFrequency = exFrequencyLong == null ? 0 : exFrequencyLong;

//如果当前时间减去周期内第一次访问方法的时间大于周期时间,则正常访问
//并将周期内第一次访问方法的时间和执行次数初始化
if (currentTime - beginTime > cycle) {
redisTemplate.opsForHash().put(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_BEGINTIME, currentTime);
redisTemplate.opsForHash().put(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_EXFREQUENCY, 1);
redisTemplate.expire(String.format(LIMITING_KEY, ipAddress, methodName), limiter.expireTime(), TimeUnit.SECONDS);
return pjp.proceed();
} else {
//如果在周期时间内,执行次数小于频率,则正常访问
//并将执行次数加一
if (exFrequency < frequency) {
redisTemplate.opsForHash().put(String.format(LIMITING_KEY, ipAddress, methodName), LIMITING_EXFREQUENCY, exFrequency + 1);
redisTemplate.expire(String.format(LIMITING_KEY, ipAddress, methodName), limiter.expireTime(), TimeUnit.SECONDS);
return pjp.proceed();
} else {
//否则抛出访问频繁异常
throw new FrequentRequestsException(limiter.message());
}
}
}
}
  1. 创建测试类
1
2
3
4
5
6
7
8
9
10
@RestController
public class TestController {

//限制在周期内只能访问3次
@Limiter(frequency = 3)
@GetMapping("getString")
public String getString(){
return "hello";
}
}
  1. 测试

浏览器中访问前三次时都能正常获取返回值,当访问第四次时抛出异常

1
demo.springboot.limiter.exception.FrequentRequestsException: 请求过于频繁

需要限制访问次数的接口上加上@Limiter注解即可