Springboot使用token防止重复提交表单问题
更新时间:2026年05月20日 10:52:17 作者:学习不易
文章主要描述了一个关于用户登录测试的过程,涉及到了DuplicateSubmitToken、DuplicateSubmitExceptionTextConstants和DuplicateSubmitAspect等术语,这些可能是在测试过程中用于防止重复提交的功能或注解
DuplicateSubmitToken
package jyuxuan.openpose.config;
import java.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 save() default true;
// 防止重复提交类型,默认:一次请求完成之前防止重复提交
int type() default REQUEST;
}
DuplicateSubmitException
package jyuxuan.openpose.config;
/**
* 自定义异常
*/
public class DuplicateSubmitException extends Exception {
public DuplicateSubmitException(String msg){
super(msg);
}
}
TextConstants
package jyuxuan.openpose.config;
public class TextConstants {
public static final String REQUEST_REPEAT = "========this is a duplicate submit exception=====";
}
DuplicateSubmitAspect
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 javax.servlet.http.HttpServletRequest;
import java.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 isSaveSession = token.save();
if (isSaveSession) {
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 isSaveSession = token.save();
if (isSaveSession) {
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 isSaveSession = token.save();
//获得方法名称
if (isSaveSession) {
String key = getDuplicateTokenKey(joinPoint);
Object t = request.getSession().getAttribute(key);
if (null != t) {
//方法执行完毕移除请求重复标记
request.getSession(false).removeAttribute(key);
log.info("异常情况--移除标记!");
}
}
}
}
}
用户登录测试
/**
* 用户登录
*
* @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";
}
总结
以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。
相关文章
SpringBoot2.x过后static下的静态资源无法访问的问题
这篇文章主要介绍了SpringBoot2.x过后static下的静态资源无法访问的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教2023-01-01
Spring Boot与Spring Security的跨域问题解决方案
跨域问题是指在Web开发中,浏览器出于安全考虑,限制了不同域名之间的资源访问,本文重点给大家介绍Spring Boot与Spring Security的跨域问题解决方案,感兴趣的朋友一起看看吧2023-09-09
Java子线程无法获取Attributes的解决方法(最新推荐)
在Java多线程编程中,子线程无法直接获取主线程设置的Attributes是一个常见问题,本文探讨了这一问题的原因,并提供了两种解决方案,对Java子线程无法获取Attributes的解决方案感兴趣的朋友一起看看吧2025-01-01


最新评论