当前位置 : 主页 > 编程语言 > java >

环绕解决防重复提交

来源:互联网 收集:自由互联 发布时间:2021-06-28
api防重复提交表单 package com.fengunion.common.aop;import java.lang.reflect.Method;import javax.servlet.http.HttpServletRequest;import org.aspectj.lang.ProceedingJoinPoint;import org.aspectj.lang.annotation.Around;import org.aspectj.
api防重复提交表单
package com.fengunion.common.aop;

import java.lang.reflect.Method;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fengunion.service.FormTokenService;
import com.fengunion.util.ExceptionCode;
import com.fengunion.util.ResultData;

@Aspect
@Component
public class ControllerInterceptor {

	private static final Logger logger = LoggerFactory.getLogger(ControllerInterceptor.class);

	@Autowired
	private FormTokenService formTokenService;

	@Autowired
	HttpServletRequest request;

	@Pointcut("execution(* com.fengunion.controller..*(..)) and @annotation(org.springframework.web.bind.annotation.RequestMapping)")
	public void controllerMethodPointcut() {

	}
	/**
	 * 记录耗时
	 * @param joinPoint
	 * @return
	 * @throws Throwable
	 */
	@Around("controllerMethodPointcut()")
	public 
 
   Object Interceptor(ProceedingJoinPoint joinPoint) throws Throwable {
		long start = System.currentTimeMillis();
		MethodSignature signature = (MethodSignature) joinPoint.getSignature();
		Method method = signature.getMethod(); // 获取被拦截的方法
		String methodName = method.getName(); // 获取被拦截的方法名
		try {

			Object obj = joinPoint.proceed();
			if (obj instanceof ResultData) {
				@SuppressWarnings("unchecked")
				ResultData
  
    result = (ResultData
   
    ) obj; long end = System.currentTimeMillis(); result.setTimes((end - start) + "ms"); obj = result; logger.info("接口" + methodName + ";耗時 : " + (end - start) + " ms!"); } return obj; } catch (Throwable e) { logger.error("接口异常" + methodName + ";耗時 : " + (System.currentTimeMillis() - start) + " ms!"); throw e; } } /** * api表单防重复提交 */ @Around("@annotation(formtoken)") public 
    
      Object execute(ProceedingJoinPoint joinPoint, FormToken formtoken) throws Throwable { // 类名 Object formToken = request.getParameter("formToken"); if (formToken != null) { // 校验本地缓存是否存在已 String isExist = formTokenService.get(formToken.toString()); if (isExist != null && isExist.equals(ExceptionCode.CODE_COMMON_SUCCESS)) { return ResultData.getIntance(ExceptionCode.FORM_SUBMISSION_REPEAT); } Object obj = joinPoint.proceed(); if (obj instanceof ResultData) { ResultData
      result = (ResultData
     ) obj; if (ExceptionCode.CODE_COMMON_SUCCESS.equals(result.getCode())) { formTokenService.put(formToken.toString()); } } return obj; } else { return ResultData.getIntance(ExceptionCode.REQUIRED_PARAM_NULL); } } } @Documented @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface FormToken { }
    
   
  
 
网友评论