使用redis防止重複提交 使用redis防止重複提交

使用redis防止重複提交

 

其實主要思路是他的https://blog.csdn.net/u013378306/article/details/52944780html

主要目前個人狀況是,先後端分離的,前端沒有session ,因此使用redis來存放數據。前端

自定義註解java

複製代碼
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 {
 
    
}
複製代碼

 

自定義攔截器web

複製代碼
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;
            }

        }
    }

}
複製代碼

 

攔截器的配置redis

複製代碼
<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加上註解spring

按 Ctrl+C 複製代碼
按 Ctrl+C 複製代碼
相關文章
相關標籤/搜索