インターフェイスのアンチブラシを実装する方法を教えます
序文
ウェブサイトの背景を閲覧すると、頻繁にリクエストを行うと「繰り返し送信しないでください」というメッセージが表示されますが、この機能はどのような用途に使用され、どのように実現されているのでしょうか。
実はこれはインターフェースアンチブラシの処理方法で、一定時間内に同一ユーザーが同一インターフェースに対して行うリクエスト数を制限することで、悪意のあるアクセスによるサーバーへの負荷の増大を防ぐのが目的です。とデータベースを保存し、ユーザーが繰り返し送信できないようにします。
アイデア分析
インターフェイスのアンチブラシには、インターセプター/AOP+Redis、インターセプター/AOP+ローカル キャッシュ、フロントエンド制限など、多くの実装アイデアがあります。ここでは、 interceptor+ Redisの実装について説明します。
原理としては、インターフェースリクエストをインターセプターでインターセプトし、redisにアクセスしてリクエストが既に存在するかどうかを確認し、存在しない場合はリクエストをキャッシュし、既に存在する場合は例外を返します。詳細については、次の図を参照してください
実装
注: 次のコードでは
AjaxResult
オブジェクトを一律に返すため、コードはここには掲載されません。独自のビジネス シナリオに従って記述できます。
RedisUtils の書き込み
import com.apply.core.exception.MyRedidsException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
* Redis工具类
*/
@Component
public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/****************** common start ****************/
/**
* 指定缓存失效时间
*
* @param key 键
* @param time 时间(秒)
* @return
*/
public boolean expire(String key, long time) {
try {
if (time > 0) {
redisTemplate.expire(key, time, TimeUnit.SECONDS);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 根据key 获取过期时间
*
* @param key 键 不能为null
* @return 时间(秒) 返回0代表为永久有效
*/
public long getExpire(String key) {
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
}
/**
* 判断key是否存在
*
* @param key 键
* @return true 存在 false不存在
*/
public boolean hasKey(String key) {
try {
return redisTemplate.hasKey(key);
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 删除缓存
*
* @param key 可以传一个值 或多个
*/
@SuppressWarnings("unchecked")
public void del(String... key) {
if (key != null && key.length > 0) {
if (key.length == 1) {
redisTemplate.delete(key[0]);
} else {
redisTemplate.delete(CollectionUtils.arrayToList(key));
}
}
}
/****************** common end ****************/
/****************** String start ****************/
/**
* 普通缓存获取
*
* @param key 键
* @return 值
*/
public Object get(String key) {
return key == null ? null : redisTemplate.opsForValue().get(key);
}
/**
* 普通缓存放入
*
* @param key 键
* @param value 值
* @return true成功 false失败
*/
public boolean set(String key, Object value) {
try {
redisTemplate.opsForValue().set(key, value);
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 普通缓存放入并设置时间
*
* @param key 键
* @param value 值
* @param time 时间(秒) time要大于0 如果time小于等于0 将设置无限期
* @return true成功 false 失败
*/
public boolean set(String key, Object value, long time) {
try {
if (time > 0) {
redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS);
} else {
set(key, value);
}
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
/**
* 递增
*
* @param key 键
* @param delta 要增加几(大于0)
* @return
*/
public long incr(String key, long delta) {
if (delta < 0) {
throw new MyRedidsException("递增因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 递减
*
* @param key 键
* @param delta 要减少几(小于0)
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
throw new MyRedidsException("递减因子必须大于0");
}
return redisTemplate.opsForValue().increment(key, -delta);
}
/****************** String end ****************/
}
インターセプターの定義¶
import com.alibaba.fastjson.JSON;
import com.apply.common.utils.redis.RedisUtils;
import com.apply.common.validator.annotation.AccessLimit;
import com.apply.core.http.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Objects;
/**
* @author Bummon
* @description 重复请求拦截
* @date 2023-08-10 14:14
*/
@Component
public class RepeatRequestIntercept extends HandlerInterceptorAdapter {
@Autowired
private RedisUtils redisUtils;
/**
* 限定时间 单位:秒
*/
private final int seconds = 1;
/**
* 限定请求次数
*/
private final int max = 1;
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//判断请求是否为方法的请求
if (handler instanceof HandlerMethod) {
String key = request.getRemoteAddr() + "-" + request.getMethod() + "-" + request.getRequestURL();
Object requestCountObj = redisUtils.get(key);
if (Objects.isNull(requestCountObj)) {
//若为空则为第一次请求
redisUtils.set(key, 1, seconds);
} else {
response.setContentType("application/json;charset=utf-8");
ServletOutputStream os = response.getOutputStream();
AjaxResult<Void> result = AjaxResult.error(100, "请求已提交,请勿重复请求");
String jsonString = JSON.toJSONString(result);
os.write(jsonString.getBytes());
os.flush();
os.close();
return false;
}
}
return true;
}
}
次に、インターセプターをコンテナーに登録します
import com.apply.common.validator.intercept.RepeatRequestIntercept;
import com.apply.core.base.entity.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* @author Bummon
* @description
* @date 2023-08-10 14:17
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private RepeatRequestIntercept repeatRequestIntercept;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(repeatRequestIntercept);
}
}
テスト用に別のインターフェースを書いてみましょう
import com.apply.common.validator.annotation.AccessLimit;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* @author Bummon
* @description
* @date 2023-08-10 14:35
*/
@RestController
public class TestController {
@GetMapping("/test")
public String test(){
return "SUCCESS";
}
}
最後に、結果が期待どおりかどうかを確認してみましょう。
1 秒以内の最初のリクエスト:
1 秒以内の 2 番目のリクエスト:
確かに期待通りでしたが、特定のインターフェースをインターセプトする場合や、インターセプト制限時間やインターフェースごとに時間が異なる場合には、この実装方法ではニーズを満たすことができないため、改善案を提案する必要があります。
改善する
カスタム アノテーションを記述し、そのアノテーションの属性として と を設定し、要求されたメソッドにインターセプターにアノテーションが含まれているかどうかを判断し、含まれている場合はインターセプト メソッドを実行し、含まれていない場合は直接返すことができますseconds
。max
カスタムアノテーションRequestLimit
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* @author Bummon
* @description 幂等性注解
* @date 2023-08-10 15:10
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface RequestLimit {
/**
* 限定时间
*/
int seconds() default 1;
/**
* 限定请求次数
*/
int max() default 1;
}
改善されたRepeatRequestIntercept
/**
* @author Bummon
* @description 重复请求拦截
* @date 2023-08-10 15:14
*/
@Component
public class RepeatRequestIntercept extends HandlerInterceptorAdapter {
@Autowired
private RedisUtils redisUtils;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//判断请求是否为方法的请求
if (handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod) handler;
//获取方法中是否有幂等性注解
RequestLimit anno = hm.getMethodAnnotation(RequestLimit.class);
//若注解为空则直接返回
if (Objects.isNull(anno)) {
return true;
}
int seconds = anno.seconds();
int max = anno.max();
String key = request.getRemoteAddr() + "-" + request.getMethod() + "-" + request.getRequestURL();
Object requestCountObj = redisUtils.get(key);
if (Objects.isNull(requestCountObj)) {
//若为空则为第一次请求
redisUtils.set(key, 1, seconds);
} else {
//限定时间内的第n次请求
int requestCount = Integer.parseInt(requestCountObj.toString());
//判断是否超过最大限定请求次数
if (requestCount < max) {
//未超过则请求次数+1
redisUtils.incr(key, 1);
} else {
//否则拒绝请求并返回信息
refuse(response);
return false;
}
}
}
return true;
}
/**
* @param response
* @date 2023-08-10 15:25
* @author Bummon
* @description 拒绝请求并返回结果
*/
private void refuse(HttpServletResponse response) throws IOException {
response.setContentType("application/json;charset=utf-8");
ServletOutputStream os = response.getOutputStream();
AjaxResult<Void> result = AjaxResult.error(100, "请求已提交,请勿重复请求");
String jsonString = JSON.toJSONString(result);
os.write(jsonString.getBytes());
os.flush();
os.close();
}
}
このようにして、私たちはニーズを達成することができます。
推薦する
ブログと公式アカウントをフォローして最新記事を入手してください