🚀 添加限流

master
linhaojun 4 years ago
parent ff020ec912
commit 2fdfffc67c

@ -2,6 +2,8 @@ package com.aurora.config;
import com.aurora.handler.PageableHandlerInterceptor;
import com.aurora.handler.WebSecurityHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
@ -14,6 +16,10 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
@Bean
public WebSecurityHandler getWebSecurityHandler() {
return new WebSecurityHandler();
}
@Override
public void addCorsMappings(CorsRegistry registry) {
@ -27,9 +33,7 @@ public class WebMvcConfig implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(new PageableHandlerInterceptor());
registry.addInterceptor(getWebSecurityHandler());
}
}

@ -1,5 +1,6 @@
package com.aurora.controller;
import com.aurora.annotation.AccessLimit;
import com.aurora.annotation.OptLog;
import com.aurora.dto.CommentAdminDTO;
import com.aurora.dto.CommentDTO;
@ -23,6 +24,7 @@ public class CommentController {
@Autowired
private CommentService commentService;
@AccessLimit(seconds = 60,maxCount = 3)
@OptLog(optType = SAVE)
@ApiOperation("添加评论")
@PostMapping("/comments/save")

@ -1,6 +1,7 @@
package com.aurora.controller;
import com.aurora.annotation.AccessLimit;
import com.aurora.dto.UserAdminDTO;
import com.aurora.dto.UserAreaDTO;
import com.aurora.dto.UserInfoDTO;
@ -23,6 +24,7 @@ public class UserAuthController {
@Autowired
private UserAuthService userAuthService;
@AccessLimit(seconds = 60,maxCount = 1)
@ApiOperation(value = "发送邮箱验证码")
@ApiImplicitParam(name = "username", value = "用户名", required = true, dataType = "String")
@GetMapping("/users/code")

@ -46,7 +46,7 @@ public class WebSecurityHandler implements HandlerInterceptor {
// 此操作代表获取该key对应的值自增1后的结果
long q = redisService.incrExpire(key, seconds);
if (q > maxCount) {
render(httpServletResponse, Result.fail("请求过于频繁,请稍候再试"));
render(httpServletResponse, Result.fail("请求过于频繁,"+seconds+"后再试"));
log.warn(key + "请求次数超过每" + seconds + "秒" + maxCount + "次");
return false;
}

@ -105,7 +105,6 @@ export default defineComponent({
})
}
})
reactiveData.commentContent = ''
}
const fetchReplies = async () => {

Loading…
Cancel
Save