Spring boot 对外接口API通过AOP防刷

spring boot 项目对外提供接口防刷功能,通过自定义注解,拦截接口,设置接口在规定的时间内请求的次数,该方式定义的自定义注解只适用在方法上,不能使用在类上。

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.xasj</groupId>
    <artifactId>cps-client</artifactId>
    <version>1.0.0</version>
    <name>cps-client</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.15</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.5.6</version>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.2</version>
                <configuration>
                    <skipTests>true</skipTests>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

AOP自定义注解类

package com.xasj.cpsclient.common;

import java.lang.annotation.*;
/**
 * @version V1.0
 * @description: 接口限流注解类,请求限制的自定义注解
 * 在 second 秒内,最大只能请求 maxCount 次
 * @author: HHJ
 * @create: 2021-11-16 15:36
 **/
@Documented
@Inherited
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestLimit {
    int second() default 1;
    int maxCount() default 1;
}

AOP切面业务类

package com.xasj.cpsclient.common;

import cn.hutool.json.JSONObject;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
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 javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @version V1.0
 * @description: aop 接口防刷工具类
 * @author: HHJ
 * @create: 2021-11-16 15:35
 **/
@Aspect
@Component
public class RequestLimitAspect {

    private  static final Logger loggerInfo = LoggerFactory.getLogger(RequestLimitAspect.class);
    private RedisTemplate redisTemplate;

    @Value("${redis.key.expireTime}")
    private Long expireTime;

    public RequestLimitAspect(RedisTemplate redisTemplate){
        this.redisTemplate = redisTemplate;
    }

    @Pointcut("@annotation(com.xasj.cpsclient.common.RequestLimit)")
    public void pointcut() {
    }

    @Before("pointcut()&&@annotation(requestLimit)")
    public void before( RequestLimit requestLimit){
    }

    @Around("pointcut()&&@annotation(requestLimit)")
    public Object around(ProceedingJoinPoint point, RequestLimit requestLimit) throws Throwable {
        // 1, 根据请求路径,判断该接口在内存中被请求的次数。
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();

        if(isLimited(request,requestLimit)){
            HttpServletResponse response = attributes.getResponse();
            JSONObject jsonObject = new JSONObject();
            jsonObject.set("提示","请求次数过于频繁,请稍后再试");
            responseOut(response,jsonObject);
            return null;
        }
        return point.proceed();
    }

    public boolean isLimited(HttpServletRequest request, RequestLimit requestLimit){
        String requestURI = request.getRequestURI();
        String remoteHost = request.getRemoteHost();
        String limitKey = requestURI+";"+remoteHost;
        Integer  o = (Integer) redisTemplate.opsForValue().get(limitKey);
        if(Objects.isNull(o)){
            loggerInfo.debug("aop isLimited 进行拦截了,写入内存:{}",limitKey);
            redisTemplate.opsForValue().set(limitKey,1,expireTime, TimeUnit.MINUTES);
        }else {
            if(o.intValue() >= requestLimit.maxCount()){
                loggerInfo.debug("aop isLimited 进行拦截了,请求次数:{},超过了限制最大请求次数:{}",o.intValue(),requestLimit.maxCount());
                return true;
            }else {
                loggerInfo.debug("aop isLimited 进行拦截了,key:{},请求次数在内存中加1",limitKey);
                redisTemplate.opsForValue().increment(limitKey);
                redisTemplate.expire(limitKey,expireTime,TimeUnit.MINUTES);
            }
        }
        return false;
    }
    /**
     * 回写给客户端
     * @param response
     * @param result
     * @throws IOException
     */
    private void responseOut(HttpServletResponse response, JSONObject result) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json; charset=utf-8");
        PrintWriter out = null ;
        String json = result.toString();
        out = response.getWriter();
        out.append(json);
    }

    @After("pointcut()&&@annotation(requestLimit)")
    public void after( RequestLimit requestLimit){
        loggerInfo.debug("aop After 进行拦截了");
    }

    @AfterReturning("pointcut()&&@annotation(requestLimit)")
    public void afterReturning(RequestLimit requestLimit){
        loggerInfo.debug("aop afterReturning 进行拦截了");
    }

    @AfterThrowing("pointcut()&&@annotation(requestLimit)")
    public void afterThrowing(RequestLimit requestLimit){
        loggerInfo.debug("aop afterThrowing 进行拦截了");
    }
}

redis序列化配置RedisSerializerConfig

package com.xasj.cpsclient.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @version V1.0
 * @description: redis 序列化配置
 * @author: HHJ
 * @create: 2021-11-03 16:28
 **/
@Configuration
public class RedisSerializerConfig {

    @Bean
    public RedisSerializer<String> redisKeySerializer() {
        return new StringRedisSerializer();
    }

    @Bean
    public RedisSerializer<Object> redisValueSerializer() {
        return new Jackson2JsonRedisSerializer(Object.class);
    }
    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory, RedisSerializer<String> redisKeySerializer, RedisSerializer<Object> redisValueSerializer) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(factory);
        //设置Key的序列化采用StringRedisSerializer
        redisTemplate.setKeySerializer(redisKeySerializer);
        redisTemplate.setHashKeySerializer(redisKeySerializer);
        //设置值的序列化
        redisTemplate.setValueSerializer(redisValueSerializer);
        redisTemplate.setHashValueSerializer(redisValueSerializer);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

限流测试类

package com.xasj.cpsclient.scheduler;

import cn.hutool.core.map.MapUtil;
import com.xasj.cpsclient.common.RequestLimit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @version V1.0
 * @description: 描述
 * @author: HHJ
 * @create: 2021-11-16 16:07
 **/
@RestController
@RequestMapping("/aop")
public class AopTest {

    @GetMapping("/test")
    @RequestLimit(maxCount = 6,second = 2)
    public Map<String,String> testMap(){
        Map<String, String> map = MapUtil.newHashMap(1);
        map.put("a","b");
        return map;
    }
}

启动类

package com.xasj.cpsclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

/**
 * 程序启动入口
 * @version V1.0
 * @author: HHJ
 * @create: 2021-11-04 15:11
 **/
@SpringBootApplication
public class CpsClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(CpsClientApplication.class, args);
    }

}

配置文件application.yml

spring:
  redis:
    database: 0
    host: 127.0.0.1
    port: 6379
    password:
    timeout: 600000
redis:
  key:
    expireTime: 30
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值