rename com.imyeyu.server to com.imyeyu.api
This commit is contained in:
22
src/main/java/com/imyeyu/api/annotation/CaptchaValid.java
Normal file
22
src/main/java/com/imyeyu/api/annotation/CaptchaValid.java
Normal file
@@ -0,0 +1,22 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import com.imyeyu.api.bean.CaptchaFrom;
|
||||
|
||||
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 {
|
||||
|
||||
/** @return 验证码来源 */
|
||||
CaptchaFrom value();
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.imyeyu.api.bean.CaptchaFrom;
|
||||
import com.imyeyu.api.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.api.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");
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/main/java/com/imyeyu/api/annotation/EnableSetting.java
Normal file
25
src/main/java/com/imyeyu/api/annotation/EnableSetting.java
Normal file
@@ -0,0 +1,25 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import com.imyeyu.api.modules.common.bean.SettingKey;
|
||||
|
||||
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:00
|
||||
*/
|
||||
@Target(ElementType.METHOD)
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface EnableSetting {
|
||||
|
||||
/** @return 配置键 */
|
||||
SettingKey value();
|
||||
|
||||
/** @return 未启用配置时响应消息语言映射键 */
|
||||
String message() default "service.offline";
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.api.modules.common.service.SettingService;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
/**
|
||||
* 启用配置注解处理器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2023-07-15 10:01
|
||||
*/
|
||||
@Component
|
||||
@RequiredArgsConstructor(onConstructor_ = {@Lazy})
|
||||
public class EnableSettingInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final SettingService service;
|
||||
|
||||
public boolean preHandle(@NonNull HttpServletRequest req, @NonNull HttpServletResponse resp, @NonNull Object handler) {
|
||||
if (handler instanceof HandlerMethod handlerMethod) {
|
||||
EnableSetting annotation = handlerMethod.getMethodAnnotation(EnableSetting.class);
|
||||
if (annotation == null) {
|
||||
return true;
|
||||
}
|
||||
if (service.is(annotation.value())) {
|
||||
return true;
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR_SERVICE_OFF, annotation.message());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,49 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.spring.TimiSpring;
|
||||
import com.imyeyu.spring.annotation.RequestRateLimitAbstractInterceptor;
|
||||
import com.imyeyu.spring.util.Redis;
|
||||
import com.imyeyu.utils.Time;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 请求频率限制处理器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-08-16 18:07
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RequestRateLimitInterceptor extends RequestRateLimitAbstractInterceptor {
|
||||
|
||||
@Autowired
|
||||
@Qualifier("redisRateLimit")
|
||||
private Redis<String, Integer> redisRequestRateLimit;
|
||||
|
||||
@Override
|
||||
public boolean beforeRun(HttpServletRequest req, HttpServletResponse resp, String id, int cycle, int limit) {
|
||||
// 键
|
||||
String key = "TimiServerAPI." + TimiSpring.getRequestIP() + "." + id;
|
||||
if (redisRequestRateLimit.has(key)) {
|
||||
Integer count = redisRequestRateLimit.get(key);
|
||||
if (count != null) {
|
||||
if (count < limit) {
|
||||
redisRequestRateLimit.setAndKeepTTL(key, ++count);
|
||||
} else {
|
||||
log.warn("请求频率过高:[" + key + "].C" + count + "L" + limit);
|
||||
throw new TimiException(TimiCode.REQUEST_BAD).msgKey("request_rate_limit");
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
redisRequestRateLimit.set(key, 0, Time.S * cycle);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
import com.imyeyu.api.modules.blog.util.UserToken;
|
||||
import com.imyeyu.spring.TimiSpring;
|
||||
import com.imyeyu.spring.annotation.RequiredToken;
|
||||
import com.imyeyu.spring.annotation.RequiredTokenAbstractInterceptor;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
/**
|
||||
* 令牌验证注解处理器
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2021-08-16 18:07
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RequiredTokenInterceptor extends RequiredTokenAbstractInterceptor<RequiredToken> {
|
||||
|
||||
@Autowired
|
||||
private UserToken userToken;
|
||||
|
||||
public RequiredTokenInterceptor() {
|
||||
super(RequiredToken.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected boolean beforeRun(HttpServletRequest req, HttpServletResponse resp) {
|
||||
if (userToken.isInvalid(TimiSpring.getToken())) {
|
||||
throw new TimiException(TimiCode.ARG_MISS).msgKey("token.illegal");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user