Use redis prevent duplicate submission

In fact, the main idea is his https://blog.csdn.net/u013378306/article/details/52944780

My main current situation is that the separation of the front and rear end, front-end no session, so redis use to store data.

Custom annotation

com.cdp.api.onboarding.interceptor Package;

Import java.lang.annotation.ElementType;
Import java.lang.annotation.Retention;
Import java.lang.annotation.RetentionPolicy;
Import java.lang.annotation.Target;

/ **
* a user submit the same url same data validation
* @author Administrator
*
* /
@Target (ElementType.METHOD)
@Retention (RetentionPolicy.RUNTIME)
public @interface SameUrlData {


}

Custom interceptors

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();//

JedisUtil.getFromRedis preUrlParams = String (URL);
IF (StringUtil.isNullOrBlank (preUrlParams)) // if the previous data is null, indicates that no page is accessed
{
// Throw Redis
JedisUtil.saveInRedis (URL, nowUrlParams, 60);
Logger .debug (DateUtil.getTimeString_Now ( "the mM-dd-YYYY HH: mm: SS")
+ "- set preUrlParams -" + nowUrlParams);
return to false;
} // otherwise the else, already visited pages
{
IF (preUrlParams . .toString () equals (nowUrlParams) ) // If the last data and url + url + the same data, said data added repeatedly
{
logger.debug (DateUtil.getTimeString_Now ( "the mM-dd-YYYY HH: mm: SS" )
+ "- repeatedly submit preUrlParams -");
return to true;
} url the else // If the last data + additional data and this different url, is not to repeat
{
logger.debug (DateUtil.getTimeString_Now ( "the MM-YYYY -dd HH:mm:ss")
+ "- Reset preUrlParams -" + nowUrlParams);
JedisUtil.saveInRedis (URL, nowUrlParams, 60);
return to false;
}

}
}

}

Interceptor configuration

<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, use the time to annotate 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"));

 

Guess you like

Origin www.cnblogs.com/yj321/p/11225431.html