Initial project
This commit is contained in:
@ -0,0 +1,70 @@
|
||||
package com.imyeyu.server.annotation;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.imyeyu.server.bean.CaptchaFrom;
|
||||
import com.imyeyu.server.util.CaptchaManager;
|
||||
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;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
/**
|
||||
* 图形验证码校验注解处理器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2023-07-15 10:01
|
||||
*/
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
public class CaptchaValidInterceptor {
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String env;
|
||||
|
||||
@Autowired
|
||||
private CaptchaManager captchaManager;
|
||||
|
||||
/** 注入注解 */
|
||||
@Pointcut("@annotation(com.imyeyu.server.annotation.CaptchaValid)")
|
||||
public void captchaPointCut() {
|
||||
}
|
||||
|
||||
/**
|
||||
* 执行前
|
||||
*
|
||||
* @param joinPoint 切入点
|
||||
*/
|
||||
@Before("captchaPointCut()")
|
||||
public void doBefore(JoinPoint joinPoint) {
|
||||
try {
|
||||
if (env.startsWith("dev")) {
|
||||
// 开发环境不校验
|
||||
return;
|
||||
}
|
||||
if (joinPoint.getSignature() instanceof MethodSignature ms) {
|
||||
Method method = joinPoint.getTarget().getClass().getMethod(ms.getName(), ms.getParameterTypes());
|
||||
CaptchaValid annotation = method.getAnnotation(CaptchaValid.class);
|
||||
CaptchaFrom from = annotation.value();
|
||||
|
||||
Object[] args = joinPoint.getArgs();
|
||||
for (int i = 0; i < args.length; i++) {
|
||||
if (args[i] instanceof CaptchaData<?> captchaData) {
|
||||
// 校验请求参数的验证码
|
||||
captchaManager.test(captchaData.getCaptcha(), from.toString());
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (NoSuchMethodException e) {
|
||||
throw new RuntimeException("TODO CaptchaValidInterceptor error");
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user