add CaptchaValid
This commit is contained in:
17
src/main/java/com/imyeyu/spring/annotation/CaptchaValid.java
Normal file
17
src/main/java/com/imyeyu/spring/annotation/CaptchaValid.java
Normal file
@ -0,0 +1,17 @@
|
|||||||
|
package com.imyeyu.spring.annotation;
|
||||||
|
|
||||||
|
import java.lang.annotation.ElementType;
|
||||||
|
import java.lang.annotation.Retention;
|
||||||
|
import java.lang.annotation.RetentionPolicy;
|
||||||
|
import java.lang.annotation.Target;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 图形验证码校验注解
|
||||||
|
*
|
||||||
|
* @author 夜雨
|
||||||
|
* @since 2023-07-15 10:09
|
||||||
|
*/
|
||||||
|
@Target(ElementType.METHOD)
|
||||||
|
@Retention(RetentionPolicy.RUNTIME)
|
||||||
|
public @interface CaptchaValid {
|
||||||
|
}
|
||||||
@ -0,0 +1,57 @@
|
|||||||
|
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;
|
||||||
|
|
||||||
|
/** 注入注解 */
|
||||||
|
@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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void verify(String captchaId, String captcha);
|
||||||
|
|
||||||
|
public void enable() {
|
||||||
|
enable = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void disable() {
|
||||||
|
enable = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,8 +1,6 @@
|
|||||||
package com.imyeyu.spring.bean;
|
package com.imyeyu.spring.bean;
|
||||||
|
|
||||||
import jakarta.validation.Valid;
|
|
||||||
import jakarta.validation.constraints.NotBlank;
|
import jakarta.validation.constraints.NotBlank;
|
||||||
import jakarta.validation.constraints.NotNull;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 含验证码数据实体
|
* 含验证码数据实体
|
||||||
@ -14,23 +12,21 @@ public class CaptchaData<T> {
|
|||||||
|
|
||||||
/** 来源 */
|
/** 来源 */
|
||||||
@NotBlank(message = "timijava.code.request_bad")
|
@NotBlank(message = "timijava.code.request_bad")
|
||||||
protected String from;
|
protected String captchaId;
|
||||||
|
|
||||||
/** 验证码 */
|
/** 验证码 */
|
||||||
@NotBlank(message = "captcha.miss")
|
@NotBlank(message = "captcha.miss")
|
||||||
protected String captcha;
|
protected String captcha;
|
||||||
|
|
||||||
/** 数据体 */
|
/** 数据体 */
|
||||||
@Valid
|
|
||||||
@NotNull
|
|
||||||
protected T data;
|
protected T data;
|
||||||
|
|
||||||
public String getFrom() {
|
public String getCaptchaId() {
|
||||||
return from;
|
return captchaId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setFrom(String from) {
|
public void setCaptchaId(String captchaId) {
|
||||||
this.from = from;
|
this.captchaId = captchaId;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getCaptcha() {
|
public String getCaptcha() {
|
||||||
|
|||||||
Reference in New Issue
Block a user