SpringBoot prevent repeat request, duplicate form submission super easy to achieve comment

1. Notes Interface 
 

/ **
* @description prevented Form to repeat annotation
* /
@Retention (RetentionPolicy.RUNTIME)
@Target (ElementType.METHOD)
@Documented
public @interface DuplicateSubmitToken {

// save duplicate commit marker needs to be saved as the default
Boolean Save () default to true;
}
2. custom exception classes

 

/ **
* repeatedly submit exception
* /
public class DuplicateSubmitException the extends a RuntimeException {
public DuplicateSubmitException (String MSG) {
Super (MSG);
}

public DuplicateSubmitException(String msg, Throwable cause){
super(msg,cause);
}
}
 

3. interceptor
 
/ **
* @description prevented Form to repeat blocker
* /
@Aspect
@Component
@ SLF4J
public class DuplicateSubmitAspect {
public static String DUPLICATE_TOKEN_KEY Final = "duplicate_token_key";

@Pointcut("execution(public * cn.test.core.controller..*(..))")

public void webLog() {
}

@Before("webLog() && @annotation(token)")
public void before(final JoinPoint joinPoint, DuplicateSubmitToken token){
if (token!=null){
Object[]args=joinPoint.getArgs();
HttpServletRequest request=null;
HttpServletResponse response=null;
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof HttpServletRequest){
request= (HttpServletRequest) args[i];
}
if (args[i] instanceof HttpServletResponse){
response= (HttpServletResponse) args[i];
}
}

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-->duplicate_token_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){
Object[]args=joinPoint.getArgs();
HttpServletRequest request=null;
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof HttpServletRequest){
request= (HttpServletRequest) args[i];
}
}
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 ( "Method removal request repeat flag is finished!");
}
}
}
}

/**
* 异常
* @param joinPoint
* @param e
*/
@AfterThrowing(pointcut = "webLog()&& @annotation(token)", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e, DuplicateSubmitToken token) {
if (null!=token
&& e instanceof DuplicateSubmitException==false){
//处理处理重复提交本身之外的异常
Object[]args=joinPoint.getArgs();
HttpServletRequest request=null;
for (int i = 0; i < args.length; i++) {
if (args[i] instanceof HttpServletRequest){
request= (HttpServletRequest) args[i];
}
}
boolean isSaveSession=token.save();
//获得方法名称
if (isSaveSession){
Key getDuplicateTokenKey = String (Joinpoint);
. T = Request.getSession Object () the getAttribute (Key);
IF (! T = null) {
// the method finishes removing duplicate request flag
request.getSession (false) .removeAttribute (key );
log.info ( "exceptions - removal request repeat flag!");
}
}
}
}
}
4. the controllers use: you want to avoid duplicate submission controller method to add annotations @DuplicateSubmitToken
/ **
* @description
* /
@RestController
public class TestController {
@RequestMapping (value = "/ Test", Method = RequestMethod.GET)
public the Map <String, Object> firstResp (the HttpServletRequest Request) {
the Map <String, Object> = Map the HashMap new new <> ();
Request.getSession () the setAttribute ( "Request the Url", Request..getRequestURL());
map.put("request Url", request.getRequestURL());
return map;
}

@DuplicateSubmitToken
@RequestMapping(value = "/test/d", method = RequestMethod.GET)
public Map<String, Object> test (HttpServletRequest request){

Random r=new Random();
int i = r.nextInt(3);
if (i==2){
throw new CustomException("有异常");
}

try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}

Map<String, Object> map = new HashMap<>();
request.getSession().setAttribute("request Url", request.getRequestURL());
map.put("request Url", request.getRequestURL());
return map;
}
}
5. 测试略过。。。

Guess you like

Origin www.cnblogs.com/bevis-byf/p/11491547.html