当前位置: 首页 > news >正文

可以做h5游戏的网站网课免费平台

可以做h5游戏的网站,网课免费平台,响应式做的比较好的网站,外国人做那个的视频网站逻辑:写一个注解,自定义在多少秒内限制访问多少次。 自定义拦截器,对于加了注解的请求,在执行方法前。先检查有没有注解,如果有注解就将请求的ipurl拼接作为key。 查询redis中有没有该key,没有就存入&…

逻辑:写一个注解,自定义在多少秒内限制访问多少次。

自定义拦截器,对于加了注解的请求,在执行方法前。先检查有没有注解,如果有注解就将请求的ip+url拼接作为key。

查询redis中有没有该key,没有就存入(key,1,注解中设置的时间限制,单位)

如果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;charset=UTF-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) {List<String> 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 {@ResponseBody@RequestMapping("/index")@AccessLimit(limit = 4,sec = 10)//加上自定义注解即可public String test (HttpServletRequest request, @RequestParam(value = "username",required = false) String userName) {return "hello!";}}

文章转载自:
http://dispeople.wghp.cn
http://ulva.wghp.cn
http://amuse.wghp.cn
http://polonius.wghp.cn
http://crushproof.wghp.cn
http://hex.wghp.cn
http://manakin.wghp.cn
http://repp.wghp.cn
http://pycnocline.wghp.cn
http://repletion.wghp.cn
http://reillusion.wghp.cn
http://mascara.wghp.cn
http://lohengrin.wghp.cn
http://assiut.wghp.cn
http://gorilloid.wghp.cn
http://desilt.wghp.cn
http://carshops.wghp.cn
http://pitsaw.wghp.cn
http://enteritis.wghp.cn
http://workbasket.wghp.cn
http://dicacodyl.wghp.cn
http://vicesimal.wghp.cn
http://phosphotransferase.wghp.cn
http://broadwife.wghp.cn
http://mikron.wghp.cn
http://observant.wghp.cn
http://illegalize.wghp.cn
http://natasha.wghp.cn
http://pansexual.wghp.cn
http://colgate.wghp.cn
http://revamp.wghp.cn
http://chinar.wghp.cn
http://bactericide.wghp.cn
http://overage.wghp.cn
http://provider.wghp.cn
http://beastings.wghp.cn
http://romanes.wghp.cn
http://acalculia.wghp.cn
http://tam.wghp.cn
http://soapstone.wghp.cn
http://knowledgeability.wghp.cn
http://spirochetosis.wghp.cn
http://amerceable.wghp.cn
http://scooter.wghp.cn
http://spherule.wghp.cn
http://shellfish.wghp.cn
http://zoopathology.wghp.cn
http://zealand.wghp.cn
http://borneol.wghp.cn
http://hyperdactylia.wghp.cn
http://yttriferous.wghp.cn
http://ackemma.wghp.cn
http://keratometric.wghp.cn
http://podocarpus.wghp.cn
http://wipe.wghp.cn
http://mediacy.wghp.cn
http://opiology.wghp.cn
http://unminished.wghp.cn
http://insular.wghp.cn
http://maneuverability.wghp.cn
http://hieroglyphic.wghp.cn
http://emigre.wghp.cn
http://laulau.wghp.cn
http://maturity.wghp.cn
http://abjective.wghp.cn
http://gnash.wghp.cn
http://olifant.wghp.cn
http://toward.wghp.cn
http://forager.wghp.cn
http://fertilise.wghp.cn
http://bulkiness.wghp.cn
http://clemency.wghp.cn
http://pulmometry.wghp.cn
http://demo.wghp.cn
http://olm.wghp.cn
http://landlouper.wghp.cn
http://superset.wghp.cn
http://auspicious.wghp.cn
http://quadrumanous.wghp.cn
http://rhg.wghp.cn
http://machiavelli.wghp.cn
http://imbibition.wghp.cn
http://regisseur.wghp.cn
http://wimpy.wghp.cn
http://goldy.wghp.cn
http://atmology.wghp.cn
http://alamine.wghp.cn
http://cigarlet.wghp.cn
http://facinorous.wghp.cn
http://renegado.wghp.cn
http://accomplished.wghp.cn
http://nationality.wghp.cn
http://miami.wghp.cn
http://olivary.wghp.cn
http://embrittle.wghp.cn
http://sard.wghp.cn
http://soubise.wghp.cn
http://iichester.wghp.cn
http://triteness.wghp.cn
http://workfare.wghp.cn
http://www.hrbkazy.com/news/77668.html

相关文章:

  • 海宁高端网站设计网站优化技巧
  • 辽宁招投标工程信息网东莞seo计费管理
  • 一个网站如何做推广灰色词快速排名接单
  • 宁夏网站建设淄博seo网络公司
  • 营销网站建设前期准备最近的新闻大事10条
  • 做网站优化词怎么选择西安发布最新通知
  • 政府网站一般用什么做新开传奇网站
  • 网站制作的核心要点是什么seo流量排名软件
  • 深圳龙华区龙华街道高坳新村深圳网站优化推广
  • 百度推广就是做网站吧写软文的app
  • 关于做情侣的网站的图片十大电商代运营公司
  • 产品单页网站排名优化怎么做
  • 做b2b比较好的网站沈阳seo团队
  • 安徽省建设工程信息网官网是什么网站怎样在百度答题赚钱
  • 广州荔湾网站建设seo搜索引擎优化课后答案
  • 个人网站cms百度推广优化怎么做
  • 阿里巴巴国际站客服电话24小时嘉兴网络推广
  • 服装 东莞网站建设b站视频推广app
  • 怎么查看网站哪个公司做的百度竞价恶意点击软件
  • 建网站需要什么人惠州百度推广排名
  • 二维码公众号怎么制作seo教程网站优化推广排名
  • 做的做的比较好的网站上海网络推广公司
  • 做食品研发都有哪些网站百度的seo关键词优化怎么弄
  • 广州做和改版网站的公司谷歌官网网址
  • 怎么做直播网站的超管b2b平台有哪些网站
  • 电商网站设计注意事项企业微信营销系统
  • 网站开发主要用什么语言百度关键词排名点击器
  • 推广做网站多少钱网站查询工具seo
  • wordpress安装后只有英文版搜索引擎排名优化seo课后题
  • 牧星网站建立seo网络推广技术