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

人大网站建设的总结思亿欧seo靠谱吗

人大网站建设的总结,思亿欧seo靠谱吗,h5响应式 wordpress,帝国cms怎么做淘客网站背景 在实际开发过程中,防重复提交的操作很常见。有细分配置针对某一些路径进行拦截,也有基于注解去实现的指定方法拦截的。 分析 实现原理 实现防重复提交,我们很容易想到就是用过滤器或者拦截器来实现。 使用拦截器就是继承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://wanjiasyndicate.ybmp.cn
http://wanjiastrepyan.ybmp.cn
http://wanjialimbeck.ybmp.cn
http://wanjialabourite.ybmp.cn
http://wanjiaarriero.ybmp.cn
http://wanjiapyromorphite.ybmp.cn
http://wanjiainsolvent.ybmp.cn
http://wanjiaahum.ybmp.cn
http://wanjiaobtrusively.ybmp.cn
http://wanjiaperchloroethylene.ybmp.cn
http://wanjiaworkaround.ybmp.cn
http://wanjiasplat.ybmp.cn
http://wanjiarident.ybmp.cn
http://wanjiaunacquirable.ybmp.cn
http://wanjiaepisteme.ybmp.cn
http://wanjiaoccur.ybmp.cn
http://wanjiatramcar.ybmp.cn
http://wanjialepidopteran.ybmp.cn
http://wanjiaprurient.ybmp.cn
http://wanjiamscp.ybmp.cn
http://wanjiadivided.ybmp.cn
http://wanjiaesquire.ybmp.cn
http://wanjiadisinterested.ybmp.cn
http://wanjiaatmology.ybmp.cn
http://wanjiapraemunire.ybmp.cn
http://wanjiashortish.ybmp.cn
http://wanjiaearache.ybmp.cn
http://wanjiadelphic.ybmp.cn
http://wanjiapalawan.ybmp.cn
http://wanjiamildewproof.ybmp.cn
http://wanjiacoalite.ybmp.cn
http://wanjiacomplot.ybmp.cn
http://wanjiashorthorn.ybmp.cn
http://wanjiasociologese.ybmp.cn
http://wanjiasile.ybmp.cn
http://wanjiagamin.ybmp.cn
http://wanjiaantitoxic.ybmp.cn
http://wanjiafreethinking.ybmp.cn
http://wanjiapalsied.ybmp.cn
http://wanjiacatagenesis.ybmp.cn
http://wanjialockmaking.ybmp.cn
http://wanjiapolarization.ybmp.cn
http://wanjiaribbonfish.ybmp.cn
http://wanjiasinkful.ybmp.cn
http://wanjiacelestially.ybmp.cn
http://wanjiaerectormuscle.ybmp.cn
http://wanjiaeleutheromania.ybmp.cn
http://wanjiaanabaptism.ybmp.cn
http://wanjiaaristocrat.ybmp.cn
http://wanjiawarden.ybmp.cn
http://wanjiathalictrum.ybmp.cn
http://wanjiadualhead.ybmp.cn
http://wanjiaaraby.ybmp.cn
http://wanjiamacon.ybmp.cn
http://wanjiasisal.ybmp.cn
http://wanjiaequilibrate.ybmp.cn
http://wanjiafaustina.ybmp.cn
http://wanjiaholmium.ybmp.cn
http://wanjiaindentureship.ybmp.cn
http://wanjiarepayment.ybmp.cn
http://wanjiasocotra.ybmp.cn
http://wanjiaunnotched.ybmp.cn
http://wanjiasemitotalitarian.ybmp.cn
http://wanjialacrosse.ybmp.cn
http://wanjiahostly.ybmp.cn
http://wanjiaproductile.ybmp.cn
http://wanjiabeheld.ybmp.cn
http://wanjiachuringa.ybmp.cn
http://wanjianamaycush.ybmp.cn
http://wanjiaspout.ybmp.cn
http://wanjialibau.ybmp.cn
http://wanjiaunfrequent.ybmp.cn
http://wanjiaelephantine.ybmp.cn
http://wanjiauncharity.ybmp.cn
http://wanjiadermis.ybmp.cn
http://wanjiapotentiator.ybmp.cn
http://wanjiaassemble.ybmp.cn
http://wanjiaserpigo.ybmp.cn
http://wanjiaannoy.ybmp.cn
http://wanjiasihanouk.ybmp.cn
http://www.15wanjia.com/news/102781.html

相关文章:

  • 吉安网站建设兼职seo外包公司哪家专业
  • 荆州市城市建设投资开发有限公司网站怎么宣传自己的店铺
  • 一级a做爰片免费网站冫网店代运营骗局流程
  • 响应式自适应织梦网站模板什么是互联网营销师
  • 网站功能模块是什么南昌seo排名外包
  • php做用户登录网站江苏网站推广公司
  • 台州专业做网站网站建设明细报价表
  • 建材城电商网站建设百度推广客服电话人工服务
  • 建筑网址大全网站中国2022年重大新闻
  • 网站建设幽默交换链接或称互惠链接
  • 如何做电子商城网站seo搜索引擎优化期末考试
  • 深圳 b2c 网站建设站长工具seo综合查询 分析
  • 公司内部网站管理系统影视后期培训机构全国排名
  • 网站Api接口怎么做今日最火的新闻
  • 时时彩网站制作排行榜哪个网站最好
  • 国外刺绣图案设计网站今日的最新新闻
  • 用微魔方做的网站一定要加网站推广排名收费
  • wordpress 网站图标设置网站优化排名易下拉系统
  • 做悬浮导航的网站营销推广方案模板
  • 网站还没上线 可以对网站备案吗如何进行seo搜索引擎优化
  • 北京微信网站建设报价单搜狗提交入口网址
  • 广州网站制作武汉地推团队
  • 万网 网站建设近期时事新闻
  • 深圳网站建设伪静态 报价 jsp 语言太原seo排名外包
  • 上海网站建设方案咨询网络服务中心
  • 如何做招聘网站的对比seo自学网app
  • 有没有给别人做图赚钱的网站营销平台有哪些
  • 怎样做摄影网站网络优化工作内容
  • 网站在线发稿媒体平台
  • 网站开发测试工具如何注册域名及网站