第一种方法:判断session中保存的token
比较麻烦,每次在提交表单时都必须传入上次的token。而且当一个页面使用ajax时,多个表单提交就会有问题。
注解Token代码:
- package com.thinkgem.jeesite.common.repeat_form_validator;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- /**
- * 页面form token
- * @author Administrator
- *
- */
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface FormToken {
- boolean save() default false;
- boolean remove() default false;
- }
package com.thinkgem.jeesite.common.repeat_form_validator; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
- 页面form token
- @author Administrator
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FormToken {
boolean save() default false;
boolean remove() default false;
}
拦截器TokenInterceptor代码:
- package com.thinkgem.jeesite.common.repeat_form_validator;
- 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.handler.HandlerInterceptorAdapter;
- public class FormTokenInterceptor 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();
- FormToken annotation = method.getAnnotation(FormToken.class);
- if (annotation != null) {
- boolean needSaveSession = annotation.save();
- if (needSaveSession) {
- request.getSession(false).setAttribute("formToken", UUID.randomUUID().toString());
- }
- boolean needRemoveSession = annotation.remove();
- if (needRemoveSession) {
- if (isRepeatSubmit(request)) {
- return false;
- }
- request.getSession(false).removeAttribute("formToken");
- }
- }
- return true;
- } else {
- return super.preHandle(request, response, handler);
- }
- }
- private boolean isRepeatSubmit(HttpServletRequest request) {
- String serverToken = (String) request.getSession(false).getAttribute("formToken");
- if (serverToken == null) {
- return true;
- }
- String clinetToken = request.getParameter("formToken");
- if (clinetToken == null) {
- return true;
- }
- if (!serverToken.equals(clinetToken)) {
- return true;
- }
- return false;
- }
- }
package com.thinkgem.jeesite.common.repeat_form_validator; 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.handler.HandlerInterceptorAdapter; public class FormTokenInterceptor 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();
FormToken annotation = method.getAnnotation(FormToken.class);
if (annotation != null) {
boolean needSaveSession = annotation.save();
if (needSaveSession) {
request.getSession(false).setAttribute("formToken", UUID.randomUUID().toString());
}
boolean needRemoveSession = annotation.remove();
if (needRemoveSession) {
if (isRepeatSubmit(request)) {
return false;
}
request.getSession(false).removeAttribute("formToken");
}
}
return true;
} else {
return super.preHandle(request, response, handler);
}
} private boolean isRepeatSubmit(HttpServletRequest request) {
String serverToken = (String) request.getSession(false).getAttribute("formToken");
if (serverToken == null) {
return true;
}
String clinetToken = request.getParameter("formToken");
if (clinetToken == null) {
return true;
}
if (!serverToken.equals(clinetToken)) {
return true;
}
return false;
}}
然后在Spring MVC的配置文件里加入:
- <mvc:interceptors>
- <mvc:interceptor>
- <mvc:mapping path="/**"/>
- <bean class="com.thinkgem.jeesite.common.repeat_form_validator.FormTokenInterceptor"/>
- </mvc:interceptor>
- </mvc:interceptors>
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.thinkgem.jeesite.common.repeat_form_validator.FormTokenInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
相关代码已经注释,相信你能看懂。
关于这个方法的用法是:在需要生成token的controller上增加@FormToken(save=true),而在需要检查重复提交的controller上添加@FormToken(remove=true)就可以了。
另外,你需要在view里在form里增加下面代码:
- <inputtypeinputtype="hidden"name="formToken"value="${formToken}" />
<inputtype="hidden"name="formToken"value="${formToken}" />
已经完成了,去试试看你的数据还能重复提交了吧。
注意在ajax提交时 要加上 formToken参数
第二种方法(判断请求url和数据是否和上一次相同)
推荐,非常简单,页面不需要任何传入,只需要在验证的controller方法上写上自定义注解即可
写好自定义注解
- package com.thinkgem.jeesite.common.repeat_form_validator;
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
- /**
- * 一个用户 相同url 同时提交 相同数据 验证
- * @author Administrator
- *
- */
- @Target(ElementType.METHOD)
- @Retention(RetentionPolicy.RUNTIME)
- public @interface SameUrlData {
- }
package com.thinkgem.jeesite.common.repeat_form_validator; import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; /**
- 一个用户 相同url 同时提交 相同数据 验证
- @author Administrator
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface SameUrlData { }
写好拦截器
- package com.thinkgem.jeesite.common.repeat_form_validator;
- import java.lang.reflect.Method;
- import java.util.HashMap;
- import java.util.Map;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.method.HandlerMethod;
- import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
- import com.thinkgem.jeesite.common.mapper.JsonMapper;
- /**
- * 一个用户 相同url 同时提交 相同数据 验证
- * 主要通过 session中保存到的url 和 请求参数。如果和上次相同,则是重复提交表单
- * @author Administrator
- *
- */
- public class SameUrlDataInterceptor 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();
- SameUrlData annotation = method.getAnnotation(SameUrlData.class);
- if (annotation != null) {
- if(repeatDataValidator(request))//如果重复相同数据
- return false;
- else
- return true;
- }
- return true;
- } else {
- return super.preHandle(request, response, handler);
- }
- }
- /**
- * 验证同一个url数据是否相同提交 ,相同返回true
- * @param httpServletRequest
- * @return
- */
- public boolean repeatDataValidator(HttpServletRequest httpServletRequest)
- {
- String params=JsonMapper.toJsonString(httpServletRequest.getParameterMap());
- String url=httpServletRequest.getRequestURI();
- Map<String,String> map=new HashMap<String,String>();
- map.put(url, params);
- String nowUrlParams=map.toString();//
- Object preUrlParams=httpServletRequest.getSession().getAttribute("repeatData");
- if(preUrlParams==null)//如果上一个数据为null,表示还没有访问页面
- {
- httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
- return false;
- }
- else//否则,已经访问过页面
- {
- if(preUrlParams.toString().equals(nowUrlParams))//如果上次url+数据和本次url+数据相同,则表示城府添加数据
- {
- return true;
- }
- else//如果上次 url+数据 和本次url加数据不同,则不是重复提交
- {
- httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
- return false;
- }
- }
- }
- }
package com.thinkgem.jeesite.common.repeat_form_validator; import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.thinkgem.jeesite.common.mapper.JsonMapper; /**
- 一个用户 相同url 同时提交 相同数据 验证
- 主要通过 session中保存到的url 和 请求参数。如果和上次相同,则是重复提交表单
- @author Administrator
public class SameUrlDataInterceptor 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();
SameUrlData annotation = method.getAnnotation(SameUrlData.class);
if (annotation != null) {
if(repeatDataValidator(request))//如果重复相同数据
return false;
else
return true;
}
return true;
} else {
return super.preHandle(request, response, handler);
}
}
/**
* 验证同一个url数据是否相同提交 ,相同返回true
* @param httpServletRequest
* @return
*/
public boolean repeatDataValidator(HttpServletRequest httpServletRequest)
{
String params=JsonMapper.toJsonString(httpServletRequest.getParameterMap());
String url=httpServletRequest.getRequestURI();
Map<String,String> map=new HashMap<String,String>();
map.put(url, params);
String nowUrlParams=map.toString();//
Object preUrlParams=httpServletRequest.getSession().getAttribute("repeatData");
if(preUrlParams==null)//如果上一个数据为null,表示还没有访问页面
{
httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
return false;
}
else//否则,已经访问过页面
{
if(preUrlParams.toString().equals(nowUrlParams))//如果上次url+数据和本次url+数据相同,则表示城府添加数据
{
return true;
}
else//如果上次 url+数据 和本次url加数据不同,则不是重复提交
{
httpServletRequest.getSession().setAttribute("repeatData", nowUrlParams);
return false;
}
}
}
}
配置spring mvc
- <mvc:interceptor>
- <mvc:mapping path="/**"/>
- <bean class="com.thinkgem.jeesite.common.repeat_form_validator.SameUrlDataInterceptor"/>
- </mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.thinkgem.jeesite.common.repeat_form_validator.SameUrlDataInterceptor"/>
</mvc:interceptor>