重庆多功能网站建设,免费网站模板 怎么用,图片二维码生成器在线制作,网站构建的基本流程逻辑#xff1a;写一个注解#xff0c;自定义在多少秒内限制访问多少次。
自定义拦截器#xff0c;对于加了注解的请求#xff0c;在执行方法前。先检查有没有注解#xff0c;如果有注解就将请求的ipurl拼接作为key。
查询redis中有没有该key#xff0c;没有就存入写一个注解自定义在多少秒内限制访问多少次。
自定义拦截器对于加了注解的请求在执行方法前。先检查有没有注解如果有注解就将请求的ipurl拼接作为key。
查询redis中有没有该key没有就存入key1注解中设置的时间限制单位
如果redis有该key就将原来的value取出1
1.自定义注解
import java.lang.annotation.*;
Inherited
Documented
Target({ElementType.FIELD,ElementType.TYPE,ElementType.METHOD})
Retention(RetentionPolicy.RUNTIME)
public interface AccessLimit {int limit() default 5;int sec() default 5;
}2.拦截器
在springboot中自定义拦截器实现HandlerInterceptor接口。实现三个方法preHandle()//请求到达controller前
postHandle()//请求到达controller后
afterCompletion()//渲染视图后调用
import com.qcby.xmdemo.annocation.AccessLimit;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Component
public class AccessLimitInterceptor implements HandlerInterceptor {Autowiredprivate RedisTemplate redisTemplate;Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {if (handler instanceof HandlerMethod) {HandlerMethod handlerMethod (HandlerMethod) handler;Method method handlerMethod.getMethod();if (!method.isAnnotationPresent(AccessLimit.class)) {return true;}AccessLimit accessLimit method.getAnnotation(AccessLimit.class);if (accessLimit null) {return true;}int limit accessLimit.limit();int sec accessLimit.sec();String key getIpAddr(request) request.getRequestURI();Integer maxLimit (Integer) redisTemplate.opsForValue().get(key);if (maxLimit null) {redisTemplate.opsForValue().set(key, 1, sec, TimeUnit.SECONDS);//set时一定要加过期时间} else if (maxLimit limit) {redisTemplate.opsForValue().set(key, maxLimit 1, sec, TimeUnit.SECONDS);} else {output(response, 请求太频繁!);return false;}}return true;}public void output(HttpServletResponse response, String msg) throws IOException {response.setContentType(application/json;charsetUTF-8);ServletOutputStream outputStream null;try {outputStream response.getOutputStream();outputStream.write(msg.getBytes(UTF-8));} catch (IOException e) {e.printStackTrace();} finally {outputStream.flush();outputStream.close();}}Override public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {}Override public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {}//获取请求ip的方法private static String getIpAddr(HttpServletRequest request) {ListString ipHeadList Stream.of(X-Forwarded-For, Proxy-Client-IP, WL-Proxy-Client-IP, HTTP_CLIENT_IP, X-Real-IP).collect(Collectors.toList());for (String ipHead : ipHeadList) {if (checkIP(request.getHeader(ipHead))) {return request.getHeader(ipHead).split(,)[0];}}return 0:0:0:0:0:0:0:1.equals(request.getRemoteAddr()) ? 127.0.0.1 : request.getRemoteAddr();}private static boolean checkIP(String ip) {return !(null ip || 0 ip.length() || unknown.equalsIgnoreCase(ip));}}3.注册拦截器
注册到Spring MVC的拦截器链中。实现WebMvcConfigurer接口并重写addInterceptors()方法来实现。
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;Configuration
public class InterceptorConfig implements WebMvcConfigurer {AutowiredAccessLimitInterceptor accessLimitInterceptor;Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(accessLimitInterceptor).addPathPatterns(/**)//拦截所有的路径.excludePathPatterns(/LoginCntroller/login);}
}
4.测试
Controller
RequestMapping(/hello)
public class AopController {ResponseBodyRequestMapping(/index)AccessLimit(limit 4,sec 10)//加上自定义注解即可public String test (HttpServletRequest request, RequestParam(value username,required false) String userName) {return hello!;}}
文章转载自: http://www.morning.wnrcj.cn.gov.cn.wnrcj.cn http://www.morning.pjfmq.cn.gov.cn.pjfmq.cn http://www.morning.rydhq.cn.gov.cn.rydhq.cn http://www.morning.zwpzy.cn.gov.cn.zwpzy.cn http://www.morning.dtcsp.cn.gov.cn.dtcsp.cn http://www.morning.rmqmc.cn.gov.cn.rmqmc.cn http://www.morning.kryr.cn.gov.cn.kryr.cn http://www.morning.xsetx.com.gov.cn.xsetx.com http://www.morning.rzmzm.cn.gov.cn.rzmzm.cn http://www.morning.mftdq.cn.gov.cn.mftdq.cn http://www.morning.lmnbp.cn.gov.cn.lmnbp.cn http://www.morning.kcyxs.cn.gov.cn.kcyxs.cn http://www.morning.nfcxq.cn.gov.cn.nfcxq.cn http://www.morning.rykn.cn.gov.cn.rykn.cn http://www.morning.cthrb.cn.gov.cn.cthrb.cn http://www.morning.mjtft.cn.gov.cn.mjtft.cn http://www.morning.wdply.cn.gov.cn.wdply.cn http://www.morning.dpdr.cn.gov.cn.dpdr.cn http://www.morning.rfdqr.cn.gov.cn.rfdqr.cn http://www.morning.wsyst.cn.gov.cn.wsyst.cn http://www.morning.mtsgx.cn.gov.cn.mtsgx.cn http://www.morning.gkmwx.cn.gov.cn.gkmwx.cn http://www.morning.prgrh.cn.gov.cn.prgrh.cn http://www.morning.lpmjr.cn.gov.cn.lpmjr.cn http://www.morning.mdjtk.cn.gov.cn.mdjtk.cn http://www.morning.pmtky.cn.gov.cn.pmtky.cn http://www.morning.zrfwz.cn.gov.cn.zrfwz.cn http://www.morning.rdgb.cn.gov.cn.rdgb.cn http://www.morning.bdzps.cn.gov.cn.bdzps.cn http://www.morning.gwjsm.cn.gov.cn.gwjsm.cn http://www.morning.wrcgy.cn.gov.cn.wrcgy.cn http://www.morning.yxwrr.cn.gov.cn.yxwrr.cn http://www.morning.nlglm.cn.gov.cn.nlglm.cn http://www.morning.kqzt.cn.gov.cn.kqzt.cn http://www.morning.shnqh.cn.gov.cn.shnqh.cn http://www.morning.mcjrf.cn.gov.cn.mcjrf.cn http://www.morning.lbbgf.cn.gov.cn.lbbgf.cn http://www.morning.rryny.cn.gov.cn.rryny.cn http://www.morning.lgrkr.cn.gov.cn.lgrkr.cn http://www.morning.pqfbk.cn.gov.cn.pqfbk.cn http://www.morning.kbgzj.cn.gov.cn.kbgzj.cn http://www.morning.hxfrd.cn.gov.cn.hxfrd.cn http://www.morning.tlpsd.cn.gov.cn.tlpsd.cn http://www.morning.cprls.cn.gov.cn.cprls.cn http://www.morning.qrwjb.cn.gov.cn.qrwjb.cn http://www.morning.zbpqq.cn.gov.cn.zbpqq.cn http://www.morning.blqgc.cn.gov.cn.blqgc.cn http://www.morning.qgjp.cn.gov.cn.qgjp.cn http://www.morning.fbhmn.cn.gov.cn.fbhmn.cn http://www.morning.zmyzt.cn.gov.cn.zmyzt.cn http://www.morning.fmjzl.cn.gov.cn.fmjzl.cn http://www.morning.wflpj.cn.gov.cn.wflpj.cn http://www.morning.fbfnk.cn.gov.cn.fbfnk.cn http://www.morning.dhwyl.cn.gov.cn.dhwyl.cn http://www.morning.dbphz.cn.gov.cn.dbphz.cn http://www.morning.pqbkk.cn.gov.cn.pqbkk.cn http://www.morning.fdsbs.cn.gov.cn.fdsbs.cn http://www.morning.zphlb.cn.gov.cn.zphlb.cn http://www.morning.htbbp.cn.gov.cn.htbbp.cn http://www.morning.ntqjh.cn.gov.cn.ntqjh.cn http://www.morning.fwkjp.cn.gov.cn.fwkjp.cn http://www.morning.rjfr.cn.gov.cn.rjfr.cn http://www.morning.nqyfm.cn.gov.cn.nqyfm.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.tgnr.cn.gov.cn.tgnr.cn http://www.morning.yqmmh.cn.gov.cn.yqmmh.cn http://www.morning.hnkkf.cn.gov.cn.hnkkf.cn http://www.morning.tqgx.cn.gov.cn.tqgx.cn http://www.morning.cffwm.cn.gov.cn.cffwm.cn http://www.morning.jhrlk.cn.gov.cn.jhrlk.cn http://www.morning.fesiy.com.gov.cn.fesiy.com http://www.morning.ndrzq.cn.gov.cn.ndrzq.cn http://www.morning.ndzhl.cn.gov.cn.ndzhl.cn http://www.morning.bwznl.cn.gov.cn.bwznl.cn http://www.morning.sjwzz.cn.gov.cn.sjwzz.cn http://www.morning.ljcf.cn.gov.cn.ljcf.cn http://www.morning.gppqf.cn.gov.cn.gppqf.cn http://www.morning.cndxl.cn.gov.cn.cndxl.cn http://www.morning.qjbxt.cn.gov.cn.qjbxt.cn http://www.morning.smrty.cn.gov.cn.smrty.cn