Javaテクノロジスタック
www.javastack.cn
より質の高い記事を読むためにフォロー
電流制限の要求は、多くの一般的なシナリオで発生します。
スパイクアクティビティ。ソフトウェアを使用して悪意を持って注文をスキャンし、商品を入手します。マシンがアクティビティに参加しないようにするには、現在の制限が必要です
apiはさまざまなシステムによって広く呼び出され、ネットワークやメモリなどのリソースを深刻に消費し、妥当な電流制限を必要とします
Taobaoは、WeChatユーザーと他の開発インターフェースを識別するために、ip Cityインターフェース、WeChat公式アカウントを取得します。無料でユーザーに提供される場合は、電流を制限する必要があり、よりリアルタイムで正確なインターフェースを支払う必要があります。
API電流制限戦闘
まず、アノテーションクラスを記述AccessLimit
し、メソッドの上限フローでアノテーションメソッドを使用して、よりエレガントで便利にします。Spring BootがRedisをどのように統合するかについては、ここをクリックしてください。
3つのパラメーターはそれぞれ、有効時間、訪問の最大数、およびログインする必要があるかどうかを表します。これは、訪問の最大数maxCount回(秒以内)として理解できます。
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AccessLimit {
int seconds();
int maxCount();
boolean needLogin() default true;
}
現在の制限のアイデア:
パスとしてipを使用し、値としてアクセス時間を使用して、特定のユーザーのリクエストを一意に識別します
あなたが訪問するたびに、
key
それが存在するかどうか、それcount
が限られた訪問数を超えているかどうかを判断アクセスが制限を超えた場合は、フロントエンドに
response
戻しmsg:请求过于频繁
て表示する必要があります
import org.springframework.beans.factory.annotation.Autowired;
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;
@Component
public class AccessLimtInterceptor implements HandlerInterceptor {
@Autowired
private RedisService redisService;
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod hm = (HandlerMethod) handler;
AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);
if (null == accessLimit) {
return true;
}
int seconds = accessLimit.seconds();
int maxCount = accessLimit.maxCount();
boolean needLogin = accessLimit.needLogin();
if (needLogin) {
//判断是否登录
}
String key = request.getContextPath() + ":" + request.getServletPath() + ":" + ip ;
Integer count = redisService.get(key);
if (null == count || -1 == count) {
redisService.set(key, 1);
redisService.expire(seconds);
return true;
}
if (count < maxCount) {
redisService.inCr(key);
return true;
}
if (count >= maxCount) {
// response 返回 json 请求过于频繁请稍后再试
return false;
}
}
return true;
}
}
インターセプターを登録し、インターセプトパスと非インターセプトパスを構成します。
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;
// extends WebMvcConfigurerAdapter 已经废弃,java 8开始直接继承就可以
@Configuration
public class IntercepterConfig implements WebMvcConfigurer {
@Autowired
private AccessLimtInterceptor accessLimtInterceptor;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(accessLimtInterceptor)
.addPathPatterns("/拦截路径")
.excludePathPatterns("/不被拦截路径 通常为登录注册或者首页");
}
}
Controller
あなたは直接注釈を使用することができます上の層方法@AccessLimit
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("test")
public class TestControler {
@GetMapping("accessLimit")
@AccessLimit(seconds = 3, maxCount = 10)
public String testAccessLimit() {
//xxxx
return "";
}
}
著者:海へ
出典:cnblogs.com/haixiang/p/12012728.html
Javaテクノロジースタックに注目して、より多くのドライグッズを見る
元のテキストをクリックして、さらに多くの特典を手に入れましょう!