最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

springboot接口服務(wù),防刷、防止請求攻擊,AOP實(shí)現(xiàn)方式

 更新時(shí)間:2024年11月22日 14:45:53   作者:十&年  
本文介紹了如何使用AOP防止Spring?Boot接口服務(wù)被網(wǎng)絡(luò)攻擊,通過在pom.xml中加入AOP依賴,創(chuàng)建自定義注解類和AOP切面,以及在業(yè)務(wù)類中使用這些注解,可以有效地對接口進(jìn)行保護(hù),測試表明,這種方法有效地防止了網(wǎng)絡(luò)攻擊

springboot接口服務(wù),防刷、防止請求攻擊,AOP實(shí)現(xiàn)

本文使用AOP的方式防止spring boot的接口服務(wù)被網(wǎng)絡(luò)攻擊

pom.xml 中加入 AOP 依賴

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

AOP自定義注解類

package org.jeecg.common.aspect.annotation;

import java.lang.annotation.*;

/**
 * 用于防刷限流的注解
 *      默認(rèn)是5秒內(nèi)只能調(diào)用一次
 */
@Target({ ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface RateLimit {

    /** 限流的key */
    String key() default "limit:";

    /** 周期,單位是秒 */
    int cycle() default 5;

    /** 請求次數(shù) */
    int count() default 1;

    /** 默認(rèn)提示信息 */
    String msg() default "請勿重復(fù)點(diǎn)擊";
}

AOP切面業(yè)務(wù)類

package org.jeecg.common.aspect;

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.jeecg.common.aspect.annotation.RateLimit;
import org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint;
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.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;

/**
 * 切面類:實(shí)現(xiàn)限流校驗(yàn)
 */
@Aspect
@Component
public class AccessLimitAspect {

    @Resource
    private RedisTemplate<String, Integer> redisTemplate;

    /**
     * 這里我們使用注解的形式
     * 當(dāng)然,我們也可以通過切點(diǎn)表達(dá)式直接指定需要攔截的package,需要攔截的class 以及 method
     */
    @Pointcut("@annotation(org.jeecg.common.aspect.annotation.RateLimit)")
    public void limitPointCut() {
    }

    /**
     * 環(huán)繞通知
     */
    @Around("limitPointCut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        // 獲取被注解的方法
        MethodInvocationProceedingJoinPoint mjp = (MethodInvocationProceedingJoinPoint) pjp;
        MethodSignature signature = (MethodSignature) mjp.getSignature();
        Method method = signature.getMethod();

        // 獲取方法上的注解
        RateLimit rateLimit = method.getAnnotation(RateLimit.class);
        if (rateLimit == null) {
            // 如果沒有注解,則繼續(xù)調(diào)用,不做任何處理
            return pjp.proceed();
        }
        /**
         * 代碼走到這里,說明有 RateLimit 注解,那么就需要做限流校驗(yàn)了
         *  1、這里可以使用Redis的API做計(jì)數(shù)校驗(yàn)
         *  2、這里也可以使用Lua腳本做計(jì)數(shù)校驗(yàn),都可以
         */
        //獲取request對象
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        // 獲取請求IP地址
        String ip = getIpAddr(request);
        // 請求url路徑
        String uri = request.getRequestURI();
        //存到redis中的key
        String key = "RateLimit:" + ip + ":" + uri;
        // 緩存中存在key,在限定訪問周期內(nèi)已經(jīng)調(diào)用過當(dāng)前接口
        if (redisTemplate.hasKey(key)) {
            // 訪問次數(shù)自增1
            redisTemplate.opsForValue().increment(key, 1);
            // 超出訪問次數(shù)限制
            if (redisTemplate.opsForValue().get(key) > rateLimit.count()) {
                throw new RuntimeException(rateLimit.msg());
            }
            // 未超出訪問次數(shù)限制,不進(jìn)行任何操作,返回true
        } else {
            // 第一次設(shè)置數(shù)據(jù),過期時(shí)間為注解確定的訪問周期
            redisTemplate.opsForValue().set(key, 1, rateLimit.cycle(), TimeUnit.SECONDS);
        }
        return pjp.proceed();
    }

    //獲取請求的歸屬IP地址
    private String getIpAddr(HttpServletRequest request) {
        String ipAddress = null;
        try {
            ipAddress = request.getHeader("x-forwarded-for");
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ipAddress == null || ipAddress.length() == 0 || "unknown".equalsIgnoreCase(ipAddress)) {
                ipAddress = request.getRemoteAddr();
            }
            // 對于通過多個(gè)代理的情況,第一個(gè)IP為客戶端真實(shí)IP,多個(gè)IP按照','分割
            if (ipAddress != null && ipAddress.length() > 15) {
                // = 15
                if (ipAddress.indexOf(",") > 0) {
                    ipAddress = ipAddress.substring(0, ipAddress.indexOf(","));
                }
            }
        } catch (Exception e) {
            ipAddress = "";
        }
        return ipAddress;
    }
}

測試

package org.jeecg.modules.api.controller;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.RateLimit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 測試接口
 * @author wujiangbo
 * @date 2022-08-23 18:50
 */
@RestController
@RequestMapping("/test")
public class TestController {

    //4秒內(nèi)只能訪問2次
    @RateLimit(key= "testLimit", count = 2, cycle = 4, msg = "大哥、慢點(diǎn)刷請求!")
    @GetMapping("/test001")
    public Result<?> rate() {
        System.out.println("請求成功");
        return Result.OK("請求成功!");
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Springboot打成war包并在tomcat中運(yùn)行的部署方法

    Springboot打成war包并在tomcat中運(yùn)行的部署方法

    這篇文章主要介紹了Springboot打成war包并在tomcat中運(yùn)行,在文中還給大家介紹了SpringBoot war包tomcat運(yùn)行啟動(dòng)報(bào)錯(cuò)(Cannot determine embedded database driver class for database type NONE)的解決方法,需要的朋友可以參考下
    2018-01-01
  • SpringBoot利用Redis解決海量重復(fù)提交問題

    SpringBoot利用Redis解決海量重復(fù)提交問題

    本文主要介紹了SpringBoot利用Redis解決海量重復(fù)提交問題,介紹了三種常見的解決方案,包括使用Redis計(jì)數(shù)器,使用Redis分布式鎖和使用Redis發(fā)布/訂閱機(jī)制,感興趣的可以了解一下
    2024-03-03
  • SpringBoot中Get請求和POST請求接收參數(shù)示例詳解

    SpringBoot中Get請求和POST請求接收參數(shù)示例詳解

    文章詳細(xì)介紹了SpringBoot中Get請求和POST請求的參數(shù)接收方式,包括方法形參接收參數(shù)、實(shí)體類接收參數(shù)、HttpServletRequest接收參數(shù)、@PathVariable接收參數(shù)、數(shù)組參數(shù)接收、集合參數(shù)接收、Map接收參數(shù)以及通過@RequestBody接收J(rèn)SON格式的參數(shù),感興趣的朋友一起看看吧
    2024-12-12
  • SpringBoot實(shí)現(xiàn)輕量級動(dòng)態(tài)定時(shí)任務(wù)管控及組件化的操作步驟

    SpringBoot實(shí)現(xiàn)輕量級動(dòng)態(tài)定時(shí)任務(wù)管控及組件化的操作步驟

    文章介紹了一種在SpringBoot中實(shí)現(xiàn)動(dòng)態(tài)定時(shí)任務(wù)的解決方案,基于COLA架構(gòu)理論,封裝到了組件層,該組件支持類級別和方法級別的定時(shí)任務(wù)注冊,并提供了易用性和擴(kuò)展性,組件使用Maven形式引入,并且可以通過YAML配置文件進(jìn)行設(shè)置,感興趣的朋友一起看看吧
    2024-11-11
  • CORS跨域問題常用解決方法代碼實(shí)例

    CORS跨域問題常用解決方法代碼實(shí)例

    這篇文章主要介紹了CORS跨域問題常用解決方法代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-11-11
  • java中List去除重復(fù)數(shù)據(jù)的5種方式總結(jié)

    java中List去除重復(fù)數(shù)據(jù)的5種方式總結(jié)

    這篇文章主要給大家總結(jié)介紹了關(guān)于java中List去除重復(fù)數(shù)據(jù)的5種方式,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • IDEA多線程文件下載插件開發(fā)的步驟詳解

    IDEA多線程文件下載插件開發(fā)的步驟詳解

    這篇文章主要介紹了IDEA多線程文件下載插件開發(fā)的步驟詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-12-12
  • Java中RocketMQ使用方法詳解

    Java中RocketMQ使用方法詳解

    這篇文章主要介紹了RocketMQ和Kafka在SpringBoot中的使用方法,以及如何保證消息隊(duì)列的順序性、可靠性以及冪等性,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2025-02-02
  • 基于SpringBoot和Vue3的博客平臺文章列表與分頁功能實(shí)現(xiàn)

    基于SpringBoot和Vue3的博客平臺文章列表與分頁功能實(shí)現(xiàn)

    在前面的教程中,我們已經(jīng)實(shí)現(xiàn)了基于Spring Boot和Vue3的發(fā)布、編輯、刪除文章功能。本教程將繼續(xù)引導(dǎo)您實(shí)現(xiàn)博客平臺的文章列表與分頁功能,需要的朋友可以參考閱讀
    2023-04-04
  • Springboot+ElementUi實(shí)現(xiàn)評論、回復(fù)、點(diǎn)贊功能

    Springboot+ElementUi實(shí)現(xiàn)評論、回復(fù)、點(diǎn)贊功能

    這篇文章主要介紹了通過Springboot ElementUi實(shí)現(xiàn)評論、回復(fù)、點(diǎn)贊功能。如果是自己評論的還可以刪除,刪除的規(guī)則是如果該評論下還有回復(fù),也一并刪除。需要的可以參考一下
    2022-01-01

最新評論

贡嘎县| 垦利县| 阜新| 贵南县| 新沂市| 交口县| 南平市| 北海市| 长顺县| 孝义市| 桃园市| 枣强县| 凌海市| 扎赉特旗| 兴山县| 通山县| 哈尔滨市| 清丰县| 凉城县| 出国| 阿克陶县| 渭源县| 铜梁县| 星子县| 招远市| 克拉玛依市| 台北市| 平度市| 萝北县| 潞城市| 镇安县| 柳江县| 兴国县| 伊春市| 那曲县| 全州县| 凤山市| 濉溪县| 麦盖提县| 鹿邑县| 聂拉木县|