springboot通过servlet拦截器对请求防重处理

springboot通过servlet HandlerInterceptor 处理请求防重

一、前言

在传统web开发过程中,通常因为服务端并发请求,非但前端要做防重处理,后端同时也要做防重处理,这样一方面可以保证系统的安全,同时可以保证系统高性能输出,本编为大家带来springboot处理http请求接口的防重实现方案。

二、总体思路

技术实现的总体思路:

1.实现servlet HandlerInterceptor接口,实现preHandle方法

2.将拦截器添加至servlet  WebMvcConfigurer拦截链路

3.添加http请求头 requestId

4.配置对应的防止请求的业务实现类

5.编写controller层对应的注解

三、重难点讲解

1.实现servlet HandlerInterceptor接口,实现preHandle方法:

package com.jiuzhou.intercepter;

import com.jiuzhou.common.annotation.Repeat;
import com.jiuzhou.common.properties.RepeatProperties;
import com.jiuzhou.service.RepeatService;
import com.jiuzhou.utils.WebUtils;
import org.apache.commons.collections.CollectionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 【问题】群组表
 * github地址 http://www.github.com/wanyushu
 * gitee地址 http://www.gitee.com/wanyushu
 * @author yushu
 * @email 921784721@qq.com
 * @date 2023/12/29 13:43
 */
@Component
public class RepeatInterceptor implements HandlerInterceptor {


    private Logger logger = LoggerFactory.getLogger(RepeatInterceptor.class);

    @Autowired
    private RepeatProperties repeatProperties;

    @Autowired
    private ApplicationContext applicationContext;

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        Repeat annotation = null;
        if(handler instanceof HandlerMethod) {
            annotation = ((HandlerMethod) handler).getMethodAnnotation(Repeat.class);
        }
        if(null==annotation){
            return true;
        }

        //判断是否设置了白名单
        if(CollectionUtils.isNotEmpty(repeatProperties.getWhiteIp())
                &&!repeatProperties.getWhiteIp().contains(WebUtils.getIP())){
            return false;
        }
        //走拦截链路
        Object bean = applicationContext.getBean(repeatProperties.getType());

        if(null==bean || !(bean instanceof RepeatService)){
            logger.error("请求repeat拦截方式没有配置!");
            return false;
        }

        String reqeustId = request.getHeader(repeatProperties.getRequestId());
        if(null==reqeustId){
            logger.warn("请求链路id为设置");
            return false;
        }

        return !((RepeatService) bean).process(reqeustId);
    }
}


2.将拦截器添加至servlet WebMvcConfigurer拦截链路

/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 *
 * https://www.softworld.vip
 *
 * 版权所有,侵权必究!
 */
package com.jiuzhou.common.configure;

import com.jiuzhou.intercepter.RepeatInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * MVC配置
 *
 * @author Mark sunlightcs@gmail.com
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    private RepeatInterceptor repeatInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(repeatInterceptor)
                .addPathPatterns("/questionGroup/**");
    }
}

3.添加http请求头 requestId

package com.jiuzhou.common.constants;

/**
 * 【问题】群组表
 * github地址 http://www.github.com/wanyushu
 * gitee地址 http://www.gitee.com/wanyushu
 *
 * @author yushu
 * @email 921784721@qq.com
 * @date 2023/12/29 13:55
 */
public interface CommonConstants {


    /**
     * 消息防重key
     */
    String REPEAT_REQ = "REPEAT_REQ:";

    String MEMORY = "memory";

    String REDIS = "redis";

}

4.配置对应的防止请求的业务实现类

package com.jiuzhou.service.impl;

import com.jiuzhou.common.properties.RepeatProperties;
import com.jiuzhou.common.constants.CommonConstants;
import com.jiuzhou.service.RepeatService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;


/**
 * redis防重实现类 -->省平台集群情况下可配置为redis
 */
@Service(CommonConstants.REDIS)
public class RedisRepeatServiceImpl implements RepeatService {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private RepeatProperties repeatProperties;

    @Override
    public boolean process(String requestId) {
        Object o = redisTemplate.opsForValue().get(CommonConstants.REPEAT_REQ + requestId);
        if (null==o){
            redisTemplate.opsForValue().set(CommonConstants.REPEAT_REQ+requestId,repeatProperties.getCacheTime());
            return false;
        }
        return true;
    }
}


package com.jiuzhou.service.impl;

import com.jiuzhou.common.properties.RepeatProperties;
import com.jiuzhou.common.constants.CommonConstants;
import com.jiuzhou.service.RepeatService;
import com.jiuzhou.utils.LocalCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 内存防重实现类 -->该类不适用集群 集群情况下请配置为其它实现类 特点单点性能好
 */
@Service(CommonConstants.MEMORY)
public class MemoryRepeatServiceImpl implements RepeatService {


    @Autowired
    private RepeatProperties repeatProperties;

    @Override
    public boolean process(String requestId) {

        String seq =  (String) LocalCache.CACHE.get(requestId);
        if(null==seq){
            //将请求id塞入缓存
            LocalCache.CACHE.put(requestId,requestId,repeatProperties.getCacheTime());
            return false;
        }

        return true;
    }
}

5.编写controller层对应的注解

package com.jiuzhou.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jiuzhou.common.annotation.Repeat;
import com.jiuzhou.entity.QuestionGroup;
import com.jiuzhou.service.QuestionGroupService;
import com.jiuzhou.utils.RestResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 【问题】群组表
 * github地址 http://www.github.com/wanyushu
 * gitee地址 http://www.gitee.com/wanyushu
 * @author yushu
 * @email 
 * @date 2023-04-27 18:00:53
 */
@Api(tags = "【问题】群组表controller",value= "【问题】群组表相关接口")
@RestController
@RequestMapping("questionGroup")
public class QuestionGroupController {

    @Autowired
    private QuestionGroupService questionGroupService;


    /**
     * 根据id删除对应的问题组
     */

    @Repeat
    @ApiOperation(value = "新增聊天组")
    @PostMapping("save")
    public RestResult save(@RequestBody QuestionGroup questionGroup){
        if(StringUtils.isEmpty(questionGroup.getGroupName())){
            return  RestResult.failed("新增聊天组名不能为空");
        }
        questionGroup.setCreateTime(new Date());
        questionGroup.setIsDel(0);
        System.out.println("入参情况打印:"+JSON.toJSONString(questionGroup));
        return RestResult.ok();
    }


}

6.yaml配置

jiuzhou:
  repeat:
    swicth: true #是否开启防重
    type: memory  # 可选 redis | memory
    #    whiteIp: 127.0.0.1 #调用白名单
    cacheTime: 120000 #缓存key时间

四、演示源码分享

1.启动项目
在这里插入图片描述
2.通过postman请求接口url

post请求
http://127.0.0.1:8081/questionGroup/save   

在这里插入图片描述
返回请求链路未设置 添加请求头header requestId

在这里插入图片描述
响应成功,当我们继续用requestId 等于3请求是,系统将进行拦截并不做响应,此处可在yaml配置对应的时长来控制同一笔请求的id不能相同的时间

五、总结

本案例通过redis和内存两种模式来控制我们的项目处理防重的方案,在实际应用中,大家可以根据各自的项目情况:

1.在项目开发初期可以不打开防重策略,只需要修改yaml配置的是否开始防重的开关;
2.后期项目上线,大家可根据系统负载情况,判断是否做负载均衡,如果做负载均衡,请设置对应的redis或者对应的db来控制防重,在单点的情况下,大家可以用内存来进行防重。
3.如果大家需要做自己的防重机制的实现,请写对应的实现类 来实现 RepeatService类,并做好yaml配置即可

六、源码分享

以上代码已上传至链接: springboot框架使用技能demo
觉得小编写的好的,请大家关注并点赞收藏,后续给程序猿门带来更多的使用技巧,谢谢!

  • 28
    点赞
  • 38
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值