1、配置拦截器

spring-mvc.xml

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**/*"/>
<mvc:exclude-mapping path="/css/**"/>
<mvc:exclude-mapping path="/images/**"/>
<mvc:exclude-mapping path="/js/**"/>
<mvc:exclude-mapping path="/upload/**"/>
<bean class="com.tp.soft.common.interceptor.AvoidDuplicateSubmissionInterceptor" />
</mvc:interceptor>
</mvc:interceptors>

2、写注解接口

Token.java

package com.tp.soft.common.interceptor;

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 Token {
boolean save() default false ;
boolean remove() default false ;
}

3、写拦截器

AvoidDuplicateSubmissionInterceptor.java

package com.tp.soft.common.interceptor;

import java.lang.reflect.Method;
import java.util.UUID; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; public class AvoidDuplicateSubmissionInterceptor extends HandlerInterceptorAdapter { @Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token annotation = method.getAnnotation(Token.class);
if (annotation != null ) {
boolean needSaveSession = annotation.save();
if (needSaveSession) {
request.getSession( false ).setAttribute("token", UUID.randomUUID().toString());
}
boolean needRemoveSession = annotation.remove();
if (needRemoveSession) {
if (isRepeatSubmit(request)) {
return false ;
}
request.getSession( false ).removeAttribute( "token" );
}
}
return true ;
} else {
return super .preHandle(request, response, handler);
}
} private boolean isRepeatSubmit(HttpServletRequest request) {
String serverToken = (String) request.getSession( false ).getAttribute( "token" );
if (serverToken == null ) {
return true ;
}
String clinetToken = request.getParameter( "token" );
if (clinetToken == null ) {
return true ;
}
if (!serverToken.equals(clinetToken)) {
return true ;
}
return false ;
} }

4、在需要生成token的方法上写上@Token(save=true) 在重复请求的方法上写上@Token(remove=true)

5、在页面上如果是form表单写上

<input type="hidden" name="token" value="${token}" />

如果是异步的则直接用${token}传入参数

spring 注解重复(防重复请求)的更多相关文章

  1. Spring注解开发系列Ⅸ --- 异步请求

    一. Servlet中的异步请求 在Servlet 3.0之前,Servlet采用Thread-Per-Request的方式处理请求,即每一次Http请求都由某一个线程从头到尾负责处理.如果要处理一些 ...

  2. 防止跨站请求伪造(CSRF)攻击 和 防重复提交 的方法的实现

    CSRF的概念可以参考:http://netsecurity.51cto.com/art/200812/102951.htm 本文介绍的是基于spring拦截器的Spring MVC实现 首先配置拦截 ...

  3. 使用aop注解实现表单防重复提交功能

    原文:https://www.cnblogs.com/manliu/articles/5983888.html 1.这里采用的方法是:使用get请求进入表单页面时,后台会生成一个tokrn_flag分 ...

  4. Spring MVC表单防重复提交

    利用Spring MVC的过滤器及token传递验证来实现表单防重复提交. 创建注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RU ...

  5. SpringMVC后台token防重复提交解决方案

    本文介绍如何使用token来防止前端重复提交的问题. 目录 1.思路 2.拦截器源码实现 3.注解源码 4.拦截器的配置 5.使用指南 6.结语 思路 1.添加拦截器,拦截需要防重复提交的请求 2.通 ...

  6. (亿级流量)分布式防重复提交token设计

    大型互联网项目中,很多流量都达到亿级.同一时间很多的人在使用,而每个用户提交表单的时候都可能会出现重复点击的情况,此时如果不做好控制,那么系统将会产生很多的数据重复的问题.怎样去设计一个高可用的防重复 ...

  7. resubmit 渐进式防重复提交框架简介

    resubmit resubmit 是一款为 java 设计的渐进式防止重复提交框架. 推荐阅读: 面试官:你们的项目中是怎么做防止重复提交的? resubmit 渐进式防重复提交框架简介 创作目的 ...

  8. springboot实现防重复提交和防重复点击

    背景 同一条数据被用户点击了多次,导致数据冗余,需要防止弱网络等环境下的重复点击 目标 通过在指定的接口处添加注解,实现根据指定的接口参数来防重复点击 说明 这里的重复点击是指在指定的时间段内多次点击 ...

  9. (九)Struts2 防重复提交

    所有的学习我们必须先搭建好Struts2的环境(1.导入对应的jar包,2.web.xml,3.struts.xml) 第一节:重复提交示例演示 struts.xml <?xml version ...

随机推荐

  1. FusionChart 数据的传入方式

    已有案例,懒得写了,放个链接,大家看看吧.http://www.cnblogs.com/liujian21st/archive/2013/03/22/2975124.html

  2. C++之路进阶——codevs3333(高级打字机)

    3333 高级打字机  时间限制: 1 s  空间限制: 256000 KB  题目等级 : 大师 Master     题目描述 Description 早苗入手了最新的高级打字机.最新款自然有着与 ...

  3. linux启动时文件系统错误问题

    linux开机启动时,文件系统错误(磁盘有坏轨或文件系统出现错误)   原因:        1.系统运行时,突然断电或不正常关机,导致文件系统错误(文件系统错误并不是硬件错误,而是软件数据的问题)  ...

  4. MFC获取Windows启动状态(正常启动、安全模式启动、网络安全模式启动)

    UINT nFlags = GetSystemMetrics(SM_CLEANBOOT); switch(nFlags) { : AfxMessageBox(TEXT("正常启动" ...

  5. net core 控制台中文乱码的解决方案

    net core 对控制台中文的支持需要加入下面的语句 Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);

  6. redhat 更新 python 为 2.7.6

    1. 下载 wget http://python.org/ftp/python/2.7.6/Python-2.7.6.tgz 2. 解压,编译 tar zxvf Python-2.7.6.tgz ./ ...

  7. 227 Entering Passive Mode (xxx,xxx,,xxx,xxx,x)

    登录ftp时显示227 Entering Passive Mode (xxx,xxx,,xxx,xxx,x) 因为FTP有两种工作模式,PORT方式和PASV方式,中文意思为主动式和被动式 ,详细介绍 ...

  8. margin padding

    margin and padding有时候可以实现相同的效果但是 用padding的场合更多 2者的区别和用法的细节我还是没掌握好

  9. String空值判定

    不能用 str.equalsIgnoreCase(null). 写了个判定的方法,供以后直接调用: public static boolean isEmpty(String str) { if (st ...

  10. 腾达和小云无线路由中继(WISP)解决

    记录一下: ============================================ A路由为主路由-接光纤 (小云智能路由) B路由为中继放大(腾达),B路由的电脑 有线连接,网卡设 ...