All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.shoulder.web.interceptor.BaseRejectRepeatSubmitInterceptor Maven / Gradle / Ivy

Go to download

shoulder WEB 模块,基于Spring Boot Web提供了 Controller AOP 日志、AOP异常处理,统一返回值,健康检查,租户、用户解析,Web 安全防护,通用CrudController,动态字典,标签管理,HTTP client AOP日志、AOP异常处理等能力,助力Web飞速开发。

The newest version!
package org.shoulder.web.interceptor;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.shoulder.core.log.Logger;
import org.shoulder.core.log.ShoulderLoggers;
import org.springframework.web.servlet.AsyncHandlerInterceptor;

/**
 * 防止重复提交拦截器
 *
 * @author lym
 */
public abstract class BaseRejectRepeatSubmitInterceptor implements AsyncHandlerInterceptor {

    private static final Logger log = ShoulderLoggers.SHOULDER_WEB;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        if (needIntercept(request, handler) && isRepeatSubmit(request)) {
            // 重复提交,拒绝执行
            handleReject(request, response);
            return false;
        }
        // 允许执行,清理服务端 token
        cleanServerToken(request);
        return true;
    }

    /**
     * 是否为重复提交
     *
     * @param request 请求
     * @return 是否为重复提交
     */
    private boolean isRepeatSubmit(HttpServletRequest request) {
        Object serverToken = getServerToken(request);
        Object clientToken = getClientToken(request);
        return serverToken != null && serverToken.equals(clientToken);
    }

    /**
     * 是否需要拦截该处理器
     *
     * @param request 请求
     * @param handler 处理器
     * @return 是否需要拦截
     */
    protected abstract boolean needIntercept(HttpServletRequest request, Object handler);

    /**
     * 服务端 token
     *
     * @param request 请求
     * @return token
     */
    protected abstract Object getServerToken(HttpServletRequest request);

    /**
     * 清理服务端 token
     *
     * @param request 请求
     */
    protected abstract void cleanServerToken(HttpServletRequest request);

    /**
     * 客户端 token
     *
     * @param request 请求
     * @return token
     */
    protected abstract Object getClientToken(HttpServletRequest request);

    /**
     * 处理被拒绝的请求:默认只记录一条debug日志
     *
     * @param request  请求
     * @param response 响应
     */
    protected void handleReject(HttpServletRequest request, HttpServletResponse response) {
        log.debug("reject repeatSubmit request({})", request.getRequestURI());
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy