使用redis防止重复提交
其实主要思路是他的https://blog.csdn.net/u013378306/article/details/52944780
主要目前我的情况是,前后端分离的,前端没有session ,所以使用redis来存放数据。
自定义注解
package com.cdp.api.onboarding.interceptor; 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.cdp.api.onboarding.interceptor; import java.io.BufferedReader; import java.io.InputStreamReader; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.method.HandlerMethod; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.alibaba.druid.util.IOUtils; import com.cdp.api.common.util.CommonUtil; import com.cdp.api.common.util.DateUtil; import com.cdp.api.common.util.JedisUtil; import com.cdp.api.common.util.StringUtil; /** * * @author Administrator * */ public class SameUrlDataInterceptor extends HandlerInterceptorAdapter { private static final Logger logger = LoggerFactory .getLogger(CommonUtil.class); @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)) { logger.debug(DateUtil .getTimeString_Now("yyyy-MM-dd HH:mm:ss") + " -- 重复请求 -- "); return false; }// 如果重复相同数据 else return true; } return true; } else { return super.preHandle(request, response, handler); } } /** * 验证同一个url数据是否相同提交 ,相同返回true * * @param request * @return * @throws Exception */ public boolean repeatDataValidator(HttpServletRequest request) throws Exception { BufferedReader reader = new BufferedReader(new InputStreamReader( request.getInputStream())); // 判断body String body = IOUtils.read(reader); String url = request.getRequestURI(); Map<String, String> map = new HashMap<String, String>(); map.put(url, body); String nowUrlParams = map.toString();// String preUrlParams = JedisUtil.getFromRedis(url); if (StringUtil.isNullOrBlank(preUrlParams))// 如果上一个数据为null,表示还没有访问页面 { // 扔redis JedisUtil.saveInRedis(url, nowUrlParams, 60); logger.debug(DateUtil.getTimeString_Now("yyyy-MM-dd HH:mm:ss") + " -- 设置preUrlParams -- " + nowUrlParams); return false; } else// 否则,已经访问过页面 { if (preUrlParams.toString().equals(nowUrlParams))// 如果上次url+数据和本次url+数据相同,则表示重复添加数据 { logger.debug(DateUtil.getTimeString_Now("yyyy-MM-dd HH:mm:ss") + " -- 重复提交preUrlParams -- "); return true; } else// 如果上次 url+数据 和本次url加数据不同,则不是重复提交 { logger.debug(DateUtil.getTimeString_Now("yyyy-MM-dd HH:mm:ss") + " -- 重新设置preUrlParams -- " + nowUrlParams); JedisUtil.saveInRedis(url, nowUrlParams, 60); return false; } } } }
拦截器的配置
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/login" />
<mvc:exclude-mapping path="/token/get" />
<mvc:exclude-mapping path="/file/**" />
<mvc:exclude-mapping path="/wx/**" />
<mvc:exclude-mapping path="/wxCompany/**" />
<mvc:exclude-mapping path="/wxOnFamily/**" />
<mvc:exclude-mapping path="/wxlogin/**" />
<mvc:exclude-mapping path="/wxMaterial/**" />
<mvc:exclude-mapping path="/wxOfferManagement/**" />
<mvc:exclude-mapping path="/wxOnEmployee/**" />
<mvc:exclude-mapping path="/wxOnEducation/**" />
<mvc:exclude-mapping path="/wxOnTraining/**" />
<mvc:exclude-mapping path="/wxOnWork/**" />
<mvc:exclude-mapping path="/wxTeam/**" />
<mvc:exclude-mapping path="/wxTemplatePage/**" />
<mvc:exclude-mapping path="/wxWelcomePage/**" />
<mvc:exclude-mapping path="/wxIdPhoto/**" />
<mvc:exclude-mapping path="/offer/excel_offer/import" />
<bean class="com.cdp.api.onboarding.interceptor.SameUrlDataInterceptor"></bean>
</mvc:interceptor>
ps,使用的时候要在controller加上注解
@RequestMapping(value = "create", method = RequestMethod.POST) @SameUrlData public void create(HttpServletRequest request, HttpServletResponse response, @RequestBody JSONObject json) { Map<String, Object> map = new HashMap<String, Object>(); String lang = langService.getLang(request); try { OnOffer offer = new OnOffer(); Integer type = json.getInt("type"); onOfferService.cheakParam(json); if (type.equals(0)) { // 保存草稿 offer.setCorpCode(json.getString("corp_code")); offer.setEmployeeName(json.getString("employee_name")); offer.setCompanyName((json.containsKey("company_name"))?json.getString("company_name"):null); offer.setCompanyCode((json.containsKey("company_code"))?json.getString("company_code"):null); offer.setPositionName((json.containsKey("position_name"))?json.getString("position_name"):null); offer.setPositionCode((json.containsKey("position_code"))?json.getString("position_code"):null); offer.setDepartmentName((json.containsKey("department_name"))?json.getString("department_name"):null); offer.setDepartmentCode((json.containsKey("department_code"))?json.getString("department_code"):null); offer.setJobName((json.containsKey("job_name"))?json.getString("job_name"):null); offer.setJobCode((json.containsKey("job_code"))?json.getString("job_code"):null); offer.setWorkingLocation((json.containsKey("working_location"))?json.getString("working_location"):null); offer.setOnNoticeTemplateId(json.getInt("on_notice_template_id")); offer.setEmaiAddress(json.getString("email_address")); offer.setCellphone(json.getString("cellphone")); offer.setOnboardDate(json.getLong("onboard_date"));