其實主要思路是他的https://blog.csdn.net/u013378306/article/details/52944780前端
主要目前個人狀況是,先後端分離的,前端沒有session ,因此使用redis來存放數據。java
自定義註解web
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 { }
自定義攔截器redis
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; } } } }
攔截器的配置spring
<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加上註解json
@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"));