发布于2026-07-01 阅读(0)
扫一扫,手机访问
package jyuxuan.openpose.config;
import ja va.lang.annotation.*;
/**
* 防止表单重复提交注解
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface DuplicateSubmitToken {
// 一次请求完成之前防止重复提交
public static final int REQUEST = 1;
// 一次会话中防止重复提交
public static final int SESSION = 2;
// 保存重复提交标记 默认为需要保存
boolean sa ve() default true;
// 防止重复提交类型,默认:一次请求完成之前防止重复提交
int type() default REQUEST;
}
先从注解入手。注解先定义了两种重复提交类型,一种是请求级别的,一种是会话级别的。注释里写得很清楚,一次请求处理完之前不允许多次提交;另一种则是在整个会话生命周期内只允许一次,典型的场景比如用户登录——用户只给一次提交机会,能有效防止暴力破解。sa ve参数用来控制是否在Session中保存标记,默认是保存的。
package jyuxuan.openpose.config;
/**
* 自定义异常
*/
public class DuplicateSubmitException extends Exception {
public DuplicateSubmitException(String msg){
super(msg);
}
}
异常类很轻量。就是自定义了一个检查型异常,交给切面往外抛,控制器那边可以统一处理,返回友好的错误提示。
package jyuxuan.openpose.config;
public class TextConstants {
public static final String REQUEST_REPEAT = "========this is a duplicate submit exception=====";
}
一个简单的常量类,定义了重复提交时抛出的异常信息。当然,实际生产环境不会用这种debug式的消息,但作为示例足够了。
package jyuxuan.openpose.config;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import ja vax.servlet.http.HttpServletRequest;
import ja va.util.UUID;
/**
* 防止表单重复提交拦截器
*/
@Aspect
@Component
@Slf4j
public class DuplicateSubmitAspect {
public static final String DUPLICATE_TOKEN_KEY = "duplicate_token_key";
@Pointcut("execution(public * jyuxuan.openpose.controller..*(..))")
public void webLog() {
}
@Before("webLog() && @annotation(token)")
public void before(final JoinPoint joinPoint, DuplicateSubmitToken token) throws DuplicateSubmitException {
if (token != null) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
boolean isSa veSession = token.sa ve();
if (isSa veSession) {
String key = getDuplicateTokenKey(joinPoint);
Object t = request.getSession().getAttribute(key);
if (null == t) {
String uuid = UUID.randomUUID().toString();
request.getSession().setAttribute(key.toString(), uuid);
log.info("token-key=" + key);
log.info("token-value=" + uuid.toString());
} else {
throw new DuplicateSubmitException(TextConstants.REQUEST_REPEAT);
}
}
}
}
/**
* 获取重复提交key
* @param joinPoint
* @return
*/
public String getDuplicateTokenKey(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
StringBuilder key = new StringBuilder(DUPLICATE_TOKEN_KEY);
key.append(",").append(methodName);
return key.toString();
}
@AfterReturning("webLog() && @annotation(token)")
public void doAfterReturning(JoinPoint joinPoint, DuplicateSubmitToken token) {
// 处理完请求,返回内容
log.info("出方法:");
if (token != null) {
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
boolean isSa veSession = token.sa ve();
if (isSa veSession) {
String key = getDuplicateTokenKey(joinPoint);
Object t = request.getSession().getAttribute(key);
if (null != t && token.type() == DuplicateSubmitToken.REQUEST) {
request.getSession(false).removeAttribute(key);
}
}
}
}
/**
* 异常
* @param joinPoint
* @param e
* @param token
*/
@AfterThrowing(pointcut = "webLog()&& @annotation(token)", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e, DuplicateSubmitToken token) {
if (null != token
&& e instanceof DuplicateSubmitException == false) {
//处理处理重复提交本身之外的异常
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
boolean isSa veSession = token.sa ve();
//获得方法名称
if (isSa veSession) {
String key = getDuplicateTokenKey(joinPoint);
Object t = request.getSession().getAttribute(key);
if (null != t) {
//方法执行完毕移除请求重复标记
request.getSession(false).removeAttribute(key);
log.info("异常情况--移除标记!");
}
}
}
}
}
核心逻辑在切面类里。切点定在controller包下所有公共方法上,然后通过注解的sa ve参数控制是否需要做重复提交校验。进入方法前,先检查Session中是否有token,没有就生成一个UUID塞进去,有就直接抛异常。请求类型下,方法正常返回后会把token移除,表示这次请求已处理完毕,允许下一次提交。异常情况也有兜底逻辑——如果方法执行过程中抛出了非重复提交本身的异常,也会清理掉token标记,避免因为一次异常堵死后面的正常请求。
/**
* 用户登录
*
* @param request
* @param model
* @return
*/
@DuplicateSubmitToken(type = DuplicateSubmitToken.SESSION)
@RequestMapping(value = "userLogin", method = RequestMethod.GET)
public String userLogin_(HttpServletRequest request, Model model) {
String username = request.getParameter("username");
String password = request.getParameter("password");
String pwd = userService.userLogin(username);
String msg;
if (pwd == null || pwd.equals(""))
msg = "该用户未注册";
else if (pwd.equals(password))
msg = "密码正确";
else
msg = "密码错误";
return "index";
}
看一个实际使用的例子。在用户登录接口上加上@DuplicateSubmitToken(type = DuplicateSubmitToken.SESSION)注解,类型设为SESSION,意味着用户在同一个会话内只有一次提交机会。如果前端重复点击了,第二次请求进来,切面直接拦截并抛出重复提交异常。代码结构很清晰:注解、异常、常量、切面四件套,再加一个业务接口使用示例。
这个防重复提交方案的核心思路是:在Session里保存一个临时Token作为标记,通过AOP切面在校验点前置拦截,请求处理完后或异常发生时清理标记。开发者只需要在需要防重复提交的接口上添加一个@DuplicateSubmitToken注解,其余的都交给切面去处理。既不用侵入业务代码,又能灵活控制类型和作用域,算是一个轻量实用的解决方案。

售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
售后无忧
立即购买>office旗舰店
正版软件
正版软件
正版软件
正版软件
正版软件
1
2
3
7
8