使用Redisのは、重複送信を防ぎます

実際には、主なアイデアは、彼のhttps://blog.csdn.net/u013378306/article/details/52944780です

私の主な現在の状況は、フロントとリアエンド、フロントエンドの分離なしのセッションなので、Redisの使用は、データを格納することです。

カスタム注釈

com.cdp.api.onboarding.interceptorパッケージ;

インポートjava.lang.annotation.ElementType;
インポートjava.lang.annotation.Retention;
インポートjava.lang.annotation.RetentionPolicy;
インポートjava.lang.annotation.Target;

/ **
*ユーザが同じURL同一のデータ検証を提出
* @author管理
*
* /
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
パブリック@interface SameUrlData {


}

カスタムインターセプタ

パッケージcom.cdp.api.onboarding.interceptor。

輸入java.io.BufferedReader;
輸入java.io.InputStreamReader。
輸入java.lang.reflect.Methodオブジェクト。
輸入java.util.HashMapを;
輸入java.util.Map;

インポートのjavax.servlet.http.HttpServletRequest;
インポートのjavax.servlet.http.HttpServletResponse;

輸入org.slf4j.Logger。
輸入org.slf4j.LoggerFactory;
輸入org.springframework.web.method.HandlerMethod;
輸入org.springframework.web.servlet.handler.HandlerInterceptorAdapter。

輸入com.alibaba.druid.util.IOUtils。
輸入com.cdp.api.common.util.CommonUtil。
輸入com.cdp.api.common.util.DateUtil。
輸入com.cdp.api.common.util.JedisUtil。
輸入com.cdp.api.common.util.StringUtil。

/ **
*
* @author管理
*
* /
publicクラスSameUrlDataInterceptorはHandlerInterceptorAdapterを{拡張します

プライベート静的最終ロガーロガー= LoggerFactory
.getLogger(CommonUtil.class)。

@Override
パブリックブールpreHandleを(HttpServletRequestのリクエストは、
HttpServletResponseの応答は、オブジェクト・ハンドラは){例外をスロー
する場合(HandlerMethod instanceofのハンドラ){
HandlerMethod handlerMethod =(HandlerMethod)ハンドラ。
方法メソッド= handlerMethod.getMethod()。
SameUrlData注釈= method.getAnnotation(SameUrlData.class)。
IF(!注釈= NULL){
IF(repeatDataValidator(リクエスト)){
logger.debug(DateUtil
.getTimeString_Now( "YYYY-MM-DD HH:MM:SS")
+ " -重复请求- ")。
falseを返します。
} //如果重复相同数据
他に
trueを返します。
}
trueを返します。
}他{
super.preHandle(要求、応答、ハンドラ)を返します。
}
}

/ **
*验证同一个URL数据是否相同提交、相同返回真
*
* @param要求
* @return
* @throws例外
* /
パブリックブールrepeatDataValidator(HttpServletRequestのリクエスト)
スロー例外{
BufferedReaderのリーダー=新しいBufferedReaderの(新しいInputStreamReaderの(
リクエスト.getInputStream()))。
//判断本体
文字列本体= IOUtils.read(リーダー)。
文字列のURL = request.getRequestURI();
地図<文字列、文字列>マップ=新しいHashMapの<文字列、文字列>();
map.put(URL、ボディ);
文字列nowUrlParams = map.toString(); //

JedisUtil.getFromRedis preUrlParams =文字列(URL);
IF(StringUtil.isNullOrBlank(preUrlParams))//前のデータがnullの場合、ページがアクセスされていないことを示し
{
// Redisのスロー
JedisUtil.saveInRedis(URL、nowUrlParams、60);
ロガー.DEBUG(DateUtil.getTimeString_Now( "MM-DD-YYYYのHH:MM:SS")
; + nowUrlParams + " - -セットpreUrlParams")
;偽に戻り
、さもなければ} //他、既に訪問したページ
{
IF(preUrlParams 。)(.toString(nowUrlParams)に等しい )// 最後のデータおよびURL + URL +同一のデータが、データが繰り返し追加前記場合
{
logger.debug(DateUtil.getTimeString_Now( "MM-DD-YYYYのHH:MM:SS" )
+ " -繰り返しpreUrlParamsを提出- ");
真に戻り、
最後のデータ+追加データと、この別のURLを、繰り返していない場合} //他のURL
{
logger.debug(DateUtil.getTimeString_Now(「MM-YYYYを-dd HH:MM:SS ")
+ " - preUrlParamsリセット- " + nowUrlParams)を、
JedisUtil.saveInRedis(URL、nowUrlParams、60);
falseを返します。
}

}
}

}

インターセプタの設定

<MVC:インターセプタ>
<MVC:インターセプター>
<MVC:マッピングパス= "/ **" />
<MVC:除外マッピングパス= "/ログイン" />
<MVC:除外マッピングパス= "/トークン/ GET "/>
<MVC:除外マッピングパス=" /ファイル/ ** "/>
<MVC:除外マッピングパス= "/ WX / **"/>
<MVC:除外マッピングパス=" / wxCompany / * * "/>
<MVC:除外マッピングパス= "/ wxOnFamily / **"/>
<MVC:除外マッピングパス= "/ wxlogin / **"/>
<MVC:除外マッピングパス=" / wxMaterial / **」/>
<MVC:除外マッピングパス= "/ wxOfferManagement / **" />
<MVC:除外マッピングパス= "/ wxOnEmployee / **" />
<MVC:除外マッピングパス= "/ wxOnEducation / **" />
<MVC:除外マッピングパス= "/ wxOnTraining / **" />
<MVC:除外マッピングパス= "/ wxOnWork / **" />
<MVC:除外マッピングパス= "/ wxTeam / **" />
<MVC:除外マッピングパス= "/ wxTemplatePage / **" />
<MVC:除外マッピングパス= "/ wxWelcomePage / **" / >
<MVC:除外マッピングパス= "/ wxIdPhoto / **" />
<MVC:除外マッピングパス= "/オファー/ excel_offer /インポート" />
<beanクラス= "com.cdp.api.onboarding.interceptor .SameUrlDataInterceptor "> </豆>
</ MVC:インターセプター>

 

PSは、コントローラに注釈を付けるために時間を使います

@RequestMapping(値= "作成"メソッド= RequestMethod.POST)
@SameUrlData
公共ボイド作成(HttpServletRequestのリクエスト、
HttpServletResponseの応答、@RequestBody JSONObject JSON){
マップの<string、オブジェクト>マップ=新しいハッシュマップの<string、オブジェクト>() ;
ストリングのlang = langService.getLang(リクエスト)。
試す{
OnOfferプラン=新しいOnOffer();

整数タイプ= json.getInt(「タイプ」)。
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"):ヌル?)。
offer.setCompanyCode((json.containsKey( "COMPANY_CODE"))json.getString( "COMPANY_CODE"):ヌル?)。
offer.setPositionName((json.containsKey( "position_name"))json.getString( "position_name"):ヌル?)。
offer.setPositionCode((json.containsKey( "position_code"))json.getString( "position_code"):ヌル?)。
offer.setDepartmentName((JSON。
offer.setDepartmentCode((json.containsKey( "department_code"))json.getString( "department_code"):ヌル?)。
offer.setJobName((json.containsKey( "JOB_NAME"))json.getString( "JOB_NAME"):ヌル?)。
offer.setJobCode((json.containsKey( "JOB_CODE"))json.getString( "JOB_CODE"):ヌル?)。
offer.setWorkingLocation((json.containsKey( "working_location"))json.getString( "working_location"):ヌル?)。
offer.setOnNoticeTemplateId(json.getInt( "on_notice_template_id"));
offer.setEmaiAddress(json.getString( "EMAIL_ADDRESS"));
offer.setCellphone(json.getString( "携帯電話"));
offer.setOnboardDate(json.getLong( "onboard_date"));

 

おすすめ

転載: www.cnblogs.com/yj321/p/11225431.html
おすすめ