使用Spring AOP注解方式实现表单重复提交验证功能

防重复提交常见解决方案:http://patrick002.iteye.com/blog/2197521

定义注解

 

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface FormRepeatSubmitValidation {
	
	String value() default "表单重复提交验证";
}

 

定义切面

 

 

@Aspect
@Component
public class FormRepeatSubmitAspect {
	/**
	 * 日志记录
	 */
	private static final Logger logger = LoggerFactory.getLogger(FormRepeatSubmitAspect.class);
	
	@Autowired
	private RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 定义切入点
	 */
	@Pointcut("@annotation(com.geekymv.FormRepeatSubmitValidation)")
	public void doAspect() {
	}
	
	@Before("doAspect()")
	public void doBefore(JoinPoint pjp) {
		Object[] args = pjp.getArgs();
		MethodSignature signature = (MethodSignature) pjp.getSignature();
		Method method = signature.getMethod();
		Object target = pjp.getTarget();
		String params = JSON.toJSONString(args);
		if(logger.isDebugEnabled()) {
			logger.debug("before methodName: {}", method.getName());
			logger.debug("before args: {}", params);
			logger.debug("before target: {}", target);
		}
		boolean flag = method.isAnnotationPresent(FormRepeatSubmitValidation.class) ;    
		if(flag) {
			FormRepeatSubmitValidation formRepeatSubmitValidation = method.getAnnotation(FormRepeatSubmitValidation.class);
			logger.info("FormRepeatSubmitValidation-->{}", formRepeatSubmitValidation.value());
			
			// 将入参作为key自增值存入redis
			ValueOperations<String, Object> value = redisTemplate.opsForValue();
			Long result = value.increment(params, 1);
			// 判断返回值是否大于1,如果是则重复提交
			if(result > 1) {
				throw new BaseException(ErrorMessage.FORM_REPEAT_SUBMIT);
			}
			redisTemplate.expire(params, 5, TimeUnit.SECONDS); // 设置过期时间5秒
		}
		
	}
	
	@After("doAspect()")
	public void doAfter(JoinPoint pjp) {
		Object[] args = pjp.getArgs();
		MethodSignature signature = (MethodSignature) pjp.getSignature();
		Method method = signature.getMethod();
		Object target = pjp.getTarget();
		String params = JSON.toJSONString(args);
		if(logger.isDebugEnabled()) {
			logger.debug("after methodName: {}", method.getName());
			logger.debug("after args: {}", params);
			logger.debug("after target: {}", target);
		}
		boolean flag = method.isAnnotationPresent(FormRepeatSubmitValidation.class) ;    
		if(flag) {
			// 清除redis中key为入参的数据,对于引用类型的变量有可能在业务代码中被修改
			redisTemplate.delete(params);
		}
	}
	
}

添加配置spring-aop.xml

 

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
		http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

	<!-- 启动@AspectJ支持 -->
	<aop:aspectj-autoproxy expose-proxy="true" proxy-target-class="true" />

</beans>

 

「更多精彩内容请关注公众号geekymv,喜欢请分享给更多的朋友哦」

 

 

 

 

 

Spring AOP(面向切面编程)是Spring框架中的一个模块,可以实现在代码执行期间的横切关注点的处理。表单验证是一个常见的需求,在接收到用户提交表单数据后,需要对这些数据进行验证,确保数据的合法性。使用Spring AOP可以将这个验证过程从业务逻辑代码中剥离出来,使得代码更加清晰和可维护。 首先,我们需要定义一个切点(Join Point),它表示在哪些方法执行之前或之后我们要进行表单验证。例如,我们可以定义一个切点在所有的Controller层的方法执行之前进行表单验证。 接下来,我们需要定义一个切面(Aspect),它包含了在切点上要执行的逻辑。在这个切面中,我们可以定义各种验证规则,比如必填项验证、长度验证、正则表达式验证等。 然后,我们需要配置Spring AOP,将切面和切点关联起来。通过配置,Spring框架会在切点匹配的方法执行之前或之后,自动调用切面中的验证逻辑。 最后,我们只需要在业务方法中使用相应的注解,将需要进行表单验证的方法标识出来。比如,我们可以在Controller类的方法上加上@Validated注解使用Spring AOP实现表单验证的好处是可以将验证逻辑独立出来,不会和业务逻辑代码混在一起,使得代码更加易读和易维护。同时,由于AOP的动态代理机制,我们可以在不修改原有代码的情况下,添加、修改或删除验证规则。这种灵活性使得我们能够方便地对表单验证进行扩展和修改。 综上所述,通过使用Spring AOP,我们可以实现表单验证功能,并将其与业务逻辑代码解耦,使代码更加清晰和可维护。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值