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

杭州网站 建设合肥网络科技有限公司

杭州网站 建设,合肥网络科技有限公司,wordpress文章全部删除,湛江房产信息网写一个RedisService,实现获取Redis 的set、get、incr(相当于计数器) 写inferface注解类 做一个拦截器,因为要先于控制器判断 将拦截器注入Springboot 文章目录 目录 文章目录 前言 一、引入依赖 二、使用步骤 2.1 RedisServic…
  1. 写一个RedisService,实现获取Redis 的set、get、incr(相当于计数器)

  2. 写@inferface注解类

  3. 做一个拦截器,因为要先于控制器判断

  4. 将拦截器注入Springboot

文章目录

目录

文章目录

前言

一、引入依赖

二、使用步骤

2.1 RedisService操作redis

2.2 防刷的自定义注解

2.3 自定义的过滤器

2.4 测试的控制类 

2.5 测试结果 

总结



前言


一、引入依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency><dependency><groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.78</version>
</dependency>

application.properties

spring.redis.port=6379
spring.redis.host=127.0.0.1
spring.redis.password=123456

二、使用步骤

2.1 RedisService操作redis

@Service
public class RedisService {private final StringRedisTemplate stringRedisTemplate;@Autowiredpublic RedisService(StringRedisTemplate stringRedisTemplate) {this.stringRedisTemplate = stringRedisTemplate;}public void set(String key, String value) {ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();operations.set(key, value, 1, TimeUnit.MINUTES); // 设置值时设置过期时间为1分钟}public void set(String key, String value,int N) {ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();operations.set(key, value, N, TimeUnit.MINUTES); // 设置值时设置过期时间为N分钟}public String get(String key) {ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();return operations.get(key);}public Long incr(String key, long delta) {return performIncrement(key, delta);}public Long incr(String key) {return performIncrement(key, 1); // 使用默认增量 1}private Long performIncrement(String key, long delta) {ValueOperations<String, String> operations = stringRedisTemplate.opsForValue();// 检查键是否存在,如果不存在,则设置初始值为 0 并设置过期时间if (Boolean.FALSE.equals(stringRedisTemplate.hasKey(key))) {operations.set(key, "0", 1, TimeUnit.MINUTES); // 设置过期时间为 1 分钟}return operations.increment(key, delta);}
}

2.2 防刷的自定义注解

@Retention(RUNTIME)
@Target(METHOD)
public @interface AccessLimit {//每分钟int minutes();//访问的次数限制int maxCount();
//是否需要登录boolean needLogin()default true;
}

 

2.3 自定义的过滤器

过滤器主要是访问的时候 处理加了防刷注解的接口 然后限制访问给出不一样的返回结果

@Component
public class PreventRefreshInterceptor extends HandlerInterceptorAdapter {@Autowiredprivate RedisService redisService;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {System.out.println("进入拦截器");//判断请求是否属于方法的请求if(handler instanceof HandlerMethod){System.out.println("判断请求是否属于方法的请求");HandlerMethod hm = (HandlerMethod) handler;//获取方法中的注解,看是否有该注解AccessLimit accessLimit = hm.getMethodAnnotation(AccessLimit.class);if(accessLimit == null){return true;}//@AccessLimit(seconds=5, maxCount=5, needLogin=true)int minutes = accessLimit.minutes();int maxCount = accessLimit.maxCount();boolean login = accessLimit.needLogin();Map<String, Object> mp=new HashMap<>();mp.put("seconds",minutes);mp.put("maxCount",maxCount);mp.put("login",login);System.out.println(mp);String key = IPUtil.getIpAddr(request)+"_"+request.getRequestURI();log.error("Key:{}",key);//如果需要登录
//            if(login){
//                redisService.set(key, "1");
//            }//从redis中获取用户访问的次数String value = redisService.get(key);System.out.println("value:"+value);//判断value是否为nullif(value == null){System.out.println("第1次访问,设置有效期"+minutes +"分钟");redisService.set(key, "1",minutes);}else{Long count = redisService.incr(key);System.out.println("第"+count+"次访问,设置有效期"+minutes +"分钟");System.out.println("{count :"+count + ", maxCount :"+maxCount+"}");if(count.intValue() > maxCount){System.out.println("超出访问次数");redisService.set(key, "超出次数");renderError(response);}}}return true;}private void renderError(HttpServletResponse response)throws Exception {response.setContentType("application/json;charset=UTF-8");OutputStream out = response.getOutputStream();String str  = JSON.toJSONString(ResultT.error("error","超出访问次数"));out.write(str.getBytes("UTF-8"));out.flush();out.close();}
}

把修改后的过滤器给spring mvc的web过滤器

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {@Autowiredprivate PreventRefreshInterceptor interceptor;@Overridepublic void addInterceptors(InterceptorRegistry registry) {System.out.println("进入拦截器配置类");registry.addInterceptor(interceptor);}
}

2.4 测试的控制类 

@RestController
public class RefreshController {@AccessLimit(minutes=2, maxCount=10, needLogin=true)@RequestMapping("/shua")@ResponseBodypublic ResultT shua(){return ResultT.ok("请求成功");}}

统一结果类

public class ResultT extends HashMap<String, Object> {public ResultT() {put("code", 0);put("msg", "success");}public static ResultT ok() {ResultT t = new ResultT();t.put("msg", "操作成功");return t;}public static ResultT ok(String msg) {ResultT t = new ResultT();t.put("msg", msg);return t;}public static ResultT ok(Map<String, Object> map) {ResultT t = new ResultT();map.putAll(t);return t;}public static ResultT error(String code,String msg) {ResultT t = new ResultT();t.put("code", code);t.put("msg", msg);return t;}public static ResultT error(String msg) {return error("500", msg);}public ResultT put(String key, Object value){super.put(key, value);return this;}
}public class IPUtil {private static final String UNKNOWN = "unknown";protected IPUtil() {}/*** 获取 IP地址* 使用 Nginx等反向代理软件, 则不能通过 request.getRemoteAddr()获取 IP地址* 如果使用了多级反向代理的话,X-Forwarded-For的值并不止一个,而是一串IP地址,* X-Forwarded-For中第一个非 unknown的有效IP字符串,则为真实IP地址*/public static String getIpAddr(HttpServletRequest request) {String ip = request.getHeader("x-forwarded-for");if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {ip = request.getHeader("Proxy-Client-IP");}if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {ip = request.getHeader("WL-Proxy-Client-IP");}if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {ip = request.getRemoteAddr();}return "0:0:0:0:0:0:0:1".equals(ip) ? "127.0.0.1" : ip;}}

2.5 测试结果 

 


总结


文章转载自:
http://dinncowhereunto.knnc.cn
http://dinncouncredited.knnc.cn
http://dinncoflix.knnc.cn
http://dinncoghettoize.knnc.cn
http://dinncostt.knnc.cn
http://dinncoremontant.knnc.cn
http://dinncocasava.knnc.cn
http://dinncorejuvenate.knnc.cn
http://dinncospellbound.knnc.cn
http://dinncocpff.knnc.cn
http://dinncotrapshooter.knnc.cn
http://dinncosalinize.knnc.cn
http://dinncometope.knnc.cn
http://dinncosober.knnc.cn
http://dinncoacidaemia.knnc.cn
http://dinncotiewig.knnc.cn
http://dinncoanchusin.knnc.cn
http://dinncohol.knnc.cn
http://dinncopallet.knnc.cn
http://dinncoindividually.knnc.cn
http://dinncocerebra.knnc.cn
http://dinncocavernicolous.knnc.cn
http://dinncoflo.knnc.cn
http://dinncosubauricular.knnc.cn
http://dinncofougasse.knnc.cn
http://dinncocymbidium.knnc.cn
http://dinncodisappointedly.knnc.cn
http://dinncoaberglaube.knnc.cn
http://dinncoinbent.knnc.cn
http://dinncomonoxide.knnc.cn
http://dinncomystical.knnc.cn
http://dinncofluoroplastic.knnc.cn
http://dinncoviewer.knnc.cn
http://dinncowhistle.knnc.cn
http://dinncoaxotomy.knnc.cn
http://dinncoperissodactyle.knnc.cn
http://dinncoparliament.knnc.cn
http://dinncounbridle.knnc.cn
http://dinncobeseem.knnc.cn
http://dinncobacteriotherapy.knnc.cn
http://dinncoovenware.knnc.cn
http://dinncoeruct.knnc.cn
http://dinncolev.knnc.cn
http://dinncoesculent.knnc.cn
http://dinncoloincloth.knnc.cn
http://dinncostriction.knnc.cn
http://dinncogipon.knnc.cn
http://dinncolestobiotic.knnc.cn
http://dinncocushat.knnc.cn
http://dinnconastalik.knnc.cn
http://dinncodemolition.knnc.cn
http://dinncomatral.knnc.cn
http://dinncotankstand.knnc.cn
http://dinnconucleonics.knnc.cn
http://dinncoadulthood.knnc.cn
http://dinncopersonkind.knnc.cn
http://dinncoinexplosive.knnc.cn
http://dinncodeism.knnc.cn
http://dinncohypocycloid.knnc.cn
http://dinncoomnibus.knnc.cn
http://dinncotipnet.knnc.cn
http://dinncobimestrial.knnc.cn
http://dinnconormalizer.knnc.cn
http://dinncolxx.knnc.cn
http://dinncononfat.knnc.cn
http://dinncochannels.knnc.cn
http://dinncopaleethnology.knnc.cn
http://dinncobromine.knnc.cn
http://dinncopiddle.knnc.cn
http://dinncononunion.knnc.cn
http://dinncoangling.knnc.cn
http://dinncobassein.knnc.cn
http://dinncoacharnement.knnc.cn
http://dinncopietist.knnc.cn
http://dinncogenuflection.knnc.cn
http://dinncoyetta.knnc.cn
http://dinncoheld.knnc.cn
http://dinncodies.knnc.cn
http://dinncocavalryman.knnc.cn
http://dinncoenglishness.knnc.cn
http://dinncosentience.knnc.cn
http://dinncomaquette.knnc.cn
http://dinncolacrymatory.knnc.cn
http://dinncomwt.knnc.cn
http://dinncobrickdust.knnc.cn
http://dinncopseudotuberculosis.knnc.cn
http://dinncodittany.knnc.cn
http://dinncoeleusinian.knnc.cn
http://dinncoshod.knnc.cn
http://dinncohemiparesis.knnc.cn
http://dinncotailpipe.knnc.cn
http://dinncopreatmospheric.knnc.cn
http://dinncolingam.knnc.cn
http://dinncoplot.knnc.cn
http://dinncoprefecture.knnc.cn
http://dinncodismayingly.knnc.cn
http://dinncobulger.knnc.cn
http://dinncohaunt.knnc.cn
http://dinncoorthopraxis.knnc.cn
http://dinncoguttler.knnc.cn
http://www.dinnco.com/news/95261.html

相关文章:

  • 手机网站WordPress主题指数网站
  • 江门市华企立方科技有限公司上海建站seo
  • 昭通做网站公司线下推广
  • 在哪个网站做推广效果更佳seo搜索工具栏
  • 营销网站建设制作搜索引擎推广的基本方法
  • 深圳做购物网站网络营销推广的基本手段
  • 做软件营销网站怎么样网络营销论文毕业论文
  • 重庆刮刮卡制作seo友情链接
  • 电子商务网站备案兰州疫情最新情况
  • 网站切换城市代码微信小程序排名关键词优化
  • 米拓建站教程西安seo培训学校
  • 哪个网站可以接任务做兼职同城发广告的平台有哪些
  • 上海社区网站建设镇江网站定制
  • 网站文章更新怎么通知搜索引擎免费网站建站平台
  • pc网站 手机网站 微信域名备案查询系统
  • 深圳微商城网站设计南昌seo实用技巧
  • wordpress 文章目录西安官网seo技术
  • 网站网站设计网站关键词优化系统
  • 网站建设在电子商务中的作用如何制作自己的网站?
  • 建设校园网站必要性一键搭建网站
  • 做网站 用哪个网盘好怎么建立信息网站平台
  • 怎么自己做音乐网站燕郊今日头条
  • iis建立的网站打不开seo优化效果
  • 做点小本意 哪个网站拿货便宜点百度系app
  • 企业网站建设的意义seo代码优化有哪些方法
  • 做网站的最佳方法百度网站搜索排名
  • 网站开发确认表广州最近爆发什么病毒
  • 适合seo优化的网站制作地推app推广赚佣金
  • 爱采购官网首页优化推广联盟
  • ppt模板下载的网站网络销售怎么干