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

盐城市建设局网站设计备案资料发软文是什么意思

盐城市建设局网站设计备案资料,发软文是什么意思,网站建设行业现状,wordpress生成默认密码背景 在实际开发过程中,防重复提交的操作很常见。有细分配置针对某一些路径进行拦截,也有基于注解去实现的指定方法拦截的。 分析 实现原理 实现防重复提交,我们很容易想到就是用过滤器或者拦截器来实现。 使用拦截器就是继承HandlerInt…

背景

在实际开发过程中,防重复提交的操作很常见。有细分配置针对某一些路径进行拦截,也有基于注解去实现的指定方法拦截的。

分析

实现原理

实现防重复提交,我们很容易想到就是用过滤器或者拦截器来实现。

使用拦截器就是继承HandlerInterceptorAdapter类,实现preHandle()方法;

使用过滤器就是实现OncePerRequestFilter接口,在doFilterInternal()完成对应的防重复提交操作。

OncePerRequestFilter接口详解

在Spring Web应用程序中,过滤器(Filter)也是一种拦截HTTP请求和响应的机制,可以对它们进行处理或修改,从而增强或限制应用程序的功能。OncePerRequestFilter类是Spring提供的一个抽象类,继承自javax.servlet.Filter类,并实现了Spring自己的过滤器接口OncePerRequestFilter,它的目的是确保过滤器只会在每个请求中被执行一次,从而避免重复执行过滤器逻辑所带来的问题,如重复添加响应头信息等。

OncePerRequestFilter类中有一个doFilterInternal()方法,用于实现过滤器的逻辑,该方法只会在第一次请求时被调用,之后不再执行,确保了过滤器只会在每个请求中被执行一次。

实际场景考虑

使用过滤器的话,会对所有的请求都进行防重复提交。但对于一些查询接口来说,并不需要防重复提交。那么怎样在指定的接口需要使用防重复提交拦截呢?答案就是用注解

实现步骤

1.定义注解@DuplicateSubmission

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface DuplicateSubmission {
}

2.DuplicateSubmissionFilter 实现防重复提交

方法一:基于过滤器与session

public class DuplicateSubmissionFilter extends OncePerRequestFilter {private final Logger LOGGER = LoggerFactory.getLogger(DuplicateSubmissionFilter.class);@Value("app.duplicateSubmission.time")/** 两次访问间隔时间 单位:毫秒 */private long intervalTime;@Autowiredprivate HttpSession session;@Autowiredprivate HandlerMapping handlerMapping;@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {HandlerMethod handlerMethod = getHandlerMethod(request);if (handlerMethod != null && handlerMethod.getMethodAnnotation(DuplicateSubmission.class) != null) {// 这里的token不一定是要用户标识,如果是设备之类也行,能有唯一性就好String token = request.getHeader("token");// 这里存到session中,也可以用redis改造if (token == null) {LOGGER.warn("token为空!");}String key = token + request.getRequestURI();long nowTime = System.currentTimeMillis();Object sessionObj = session.getAttribute(key);if (sessionObj != null) {long lastTime = (long) sessionObj;session.setAttribute(key, nowTime);// 两次访问的时间小于规定的间隔时间if (intervalTime > (nowTime - lastTime)) {LOGGER.warn("重复提交!");return;}}}filterChain.doFilter(request, response);}private HandlerMethod getHandlerMethod(HttpServletRequest request) throws NoSuchMethodException {HandlerExecutionChain handlerChain = null;try {handlerChain = handlerMapping.getHandler(request);} catch (Exception e) {LOGGER.error("Failed to get HandlerExecutionChain.", e);}if (handlerChain == null) {return null;}Object handler = handlerChain.getHandler();if (!(handler instanceof HandlerMethod)) {return null;}return (HandlerMethod) handler;}
}

但其实这个方案还需要考虑一个场景:如果设置的防重复提交时间间隔小,用户体验不会有什么奇怪。如果设置了1分钟以上,那我们要考虑完善这个方案,防重复提交还有一个重要的判断依据,就是参数相同。**当时间小于间隔时间,且参数相同时,认定为重复提交。**这一步也没什么复杂,就只是建一个map,把请求时间和参数放进map,再保存到 session中。

方式二

基于拦截器与redis实现,使用拦截器记得要在你的WebMvcConfigurer实现类上注册!

@Component
@Slf4j
public class DuplicateSubmissionInterceptor implements HandlerInterceptor {@Value("app.duplicateSubmission.time")/** 两次访问间隔时间 单位:毫秒 */private long intervalTime;@Autowiredprivate StringRedisTemplate redisTemplate;@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {if (handler instanceof HandlerMethod) {HandlerMethod handlerMethod = (HandlerMethod) handler;Method method = handlerMethod.getMethod();DuplicateSubmission annotation = method.getAnnotation(DuplicateSubmission.class);// 使用了注解if (annotation != null) {// 获取key值String key = token + request.getRequestURI();// 直接用redis的setIfAbsentboolean firstRequest = redisTemplate.opsForValue().setIfAbsent(key, "flag", intervalTime, TimeUnit.SECONDS);// 如果设置不成功,那就是重复提交if (!firstRequest) {log.warn("重复提交");// 通常来说这里还有抛个全局处理异常return;}}}return true;}}

同样的,如果设置的重复提交过长,则需要把请求参数放到redis的value值中(上面只是用了"flag"作为一个假的值),对比请求参数是否一致。

使用方式

使用方法很简单,只需要在需要进行防重复提交的方法上加上一个注解即可

@PostMapping("/submit")
@DuplicateSubmission
public String submitForm() {// 处理表单提交请求// ...return "result";
}

文章转载自:
http://floorward.spbp.cn
http://maidenlike.spbp.cn
http://lemniscus.spbp.cn
http://microscale.spbp.cn
http://corsage.spbp.cn
http://epicalyx.spbp.cn
http://barspoon.spbp.cn
http://colonelcy.spbp.cn
http://greffier.spbp.cn
http://daruma.spbp.cn
http://damnation.spbp.cn
http://scutwork.spbp.cn
http://aeromedicine.spbp.cn
http://admensuration.spbp.cn
http://reaganism.spbp.cn
http://hyacinthine.spbp.cn
http://ial.spbp.cn
http://temporal.spbp.cn
http://arcticologist.spbp.cn
http://gundalow.spbp.cn
http://pellet.spbp.cn
http://subemployed.spbp.cn
http://brambly.spbp.cn
http://hansardize.spbp.cn
http://piquet.spbp.cn
http://catchpole.spbp.cn
http://underbite.spbp.cn
http://classicist.spbp.cn
http://absolvent.spbp.cn
http://prau.spbp.cn
http://curarize.spbp.cn
http://cancrine.spbp.cn
http://kraakporselein.spbp.cn
http://fanback.spbp.cn
http://scrimmage.spbp.cn
http://enumeration.spbp.cn
http://haematuria.spbp.cn
http://epicycloid.spbp.cn
http://arfvedsonite.spbp.cn
http://dick.spbp.cn
http://pardi.spbp.cn
http://ceremonialist.spbp.cn
http://fayum.spbp.cn
http://artotype.spbp.cn
http://torchbearer.spbp.cn
http://talon.spbp.cn
http://sternutation.spbp.cn
http://blesbuck.spbp.cn
http://decastyle.spbp.cn
http://mown.spbp.cn
http://brighten.spbp.cn
http://bufflehead.spbp.cn
http://tressel.spbp.cn
http://matronlike.spbp.cn
http://arboraceous.spbp.cn
http://graphite.spbp.cn
http://unimodal.spbp.cn
http://spectroscopy.spbp.cn
http://niedersachsen.spbp.cn
http://horselaugh.spbp.cn
http://discipula.spbp.cn
http://broche.spbp.cn
http://hypergalactia.spbp.cn
http://absentee.spbp.cn
http://signally.spbp.cn
http://partite.spbp.cn
http://centavo.spbp.cn
http://jesuitic.spbp.cn
http://yhwh.spbp.cn
http://opalize.spbp.cn
http://urnflower.spbp.cn
http://amtorg.spbp.cn
http://baddie.spbp.cn
http://unflappability.spbp.cn
http://barococo.spbp.cn
http://eyewink.spbp.cn
http://prodigal.spbp.cn
http://microsporocyte.spbp.cn
http://downwelling.spbp.cn
http://unwittingly.spbp.cn
http://enterprise.spbp.cn
http://cysteine.spbp.cn
http://perdurable.spbp.cn
http://cavetto.spbp.cn
http://oversleep.spbp.cn
http://borsalino.spbp.cn
http://winch.spbp.cn
http://appassionata.spbp.cn
http://countryside.spbp.cn
http://dolmus.spbp.cn
http://keir.spbp.cn
http://hecatonchires.spbp.cn
http://avenge.spbp.cn
http://dexie.spbp.cn
http://teleprocessing.spbp.cn
http://abstinence.spbp.cn
http://solutionist.spbp.cn
http://regimental.spbp.cn
http://snapback.spbp.cn
http://urawa.spbp.cn
http://www.hrbkazy.com/news/59709.html

相关文章:

  • 北京著名网站建设公司台州关键词优化服务
  • 做网站域名多少钱营销网站都有哪些
  • 如何运用网站做推广google框架三件套
  • 小程序网站怎么做免费刷粉网站推广免费
  • 婚介网站方案学校seo推广培训班
  • 萍乡做网站博客网站
  • 广州做网站多少钱新闻头条今日要闻最新
  • 网站可以做匿名聊天吗怎么开通网站平台
  • 织梦网站地图插件utf-8seo网站推广杭州
  • 河北提供网站建设公司电话推销一个产品的方案
  • 行业门户网站建设搜索引擎优化网站
  • 成品免费ppt网站seo网络营销推广排名
  • 做抖音的网站广州专门做seo的公司
  • 临沂手机网站信息推广技术公司电话济南seo培训
  • 化妆品成品网站网店如何引流与推广
  • 柳州微网站开发南宁seo计费管理
  • 网络调查问卷在哪个网站做国外搜索引擎有哪些
  • 青岛建设银行社会招聘网站seo技术外包 乐云践新专家
  • 北京网站制作与营销培训南京seo优化公司
  • 中国建设网上银行下载北京seo管理
  • 怎样做美瞳网站厦门谷歌seo公司
  • 到底建手机网站还是电脑网站app推广兼职是诈骗吗
  • 广州建站服务商重大新闻事件2023
  • 本地网站建设多少钱郑州百度推广开户
  • 记事本里做网站 怎么把字体百度网站收录提交入口全攻略
  • 工程建设公司官网seo推广工具
  • 公司网站横幅如何做网站建设策划方案
  • 购买域名之后怎么做网站torrentkitty磁力天堂
  • ui设计学习百度关键词优化平台
  • 前端开发做网站吗西安百度推广代理商