72 lines
1.5 KiB
Java
72 lines
1.5 KiB
Java
package com.imyeyu.spring.annotation;
|
|
|
|
import com.imyeyu.spring.bean.CaptchaData;
|
|
import org.aspectj.lang.JoinPoint;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.aspectj.lang.annotation.Before;
|
|
import org.aspectj.lang.annotation.Pointcut;
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
/**
|
|
* 图形验证码校验注解处理器
|
|
*
|
|
* @author 夜雨
|
|
* @since 2023-07-15 10:01
|
|
*/
|
|
@Aspect
|
|
public abstract class CaptchaValidAbstractInterceptor {
|
|
|
|
private boolean enable = true;
|
|
|
|
/**
|
|
* 创建验证码校验拦截器
|
|
*/
|
|
protected CaptchaValidAbstractInterceptor() {
|
|
}
|
|
|
|
/** 注入注解 */
|
|
@Pointcut("@annotation(com.imyeyu.spring.annotation.CaptchaValid)")
|
|
public void captchaPointCut() {
|
|
}
|
|
|
|
/**
|
|
* 执行前
|
|
*
|
|
* @param joinPoint 切入点
|
|
*/
|
|
@Before("captchaPointCut()")
|
|
public void doBefore(JoinPoint joinPoint) {
|
|
if (!enable) {
|
|
return;
|
|
}
|
|
if (joinPoint.getSignature() instanceof MethodSignature ms) {
|
|
Object[] args = joinPoint.getArgs();
|
|
for (int i = 0; i < args.length; i++) {
|
|
if (args[i] instanceof CaptchaData<?> captchaData) {
|
|
// 校验请求参数的验证码
|
|
verify(captchaData.getCaptchaId(), captchaData.getCaptcha());
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 校验验证码
|
|
*
|
|
* @param captchaId 验证码 ID
|
|
* @param captcha 验证码
|
|
*/
|
|
protected abstract void verify(String captchaId, String captcha);
|
|
|
|
/** 启用校验 */
|
|
public void enable() {
|
|
enable = true;
|
|
}
|
|
|
|
/** 禁用校验 */
|
|
public void disable() {
|
|
enable = false;
|
|
}
|
|
}
|