refactor all
This commit is contained in:
@@ -1,22 +0,0 @@
|
||||
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();
|
||||
}
|
||||
@@ -1,20 +1,13 @@
|
||||
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 com.imyeyu.spring.annotation.CaptchaValidAbstractInterceptor;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
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;
|
||||
|
||||
/**
|
||||
* 图形验证码校验注解处理器
|
||||
*
|
||||
@@ -24,47 +17,19 @@ import java.lang.reflect.Method;
|
||||
@Slf4j
|
||||
@Aspect
|
||||
@Component
|
||||
public class CaptchaValidInterceptor {
|
||||
@RequiredArgsConstructor
|
||||
public class CaptchaValidInterceptor extends CaptchaValidAbstractInterceptor {
|
||||
|
||||
@Value("${spring.profiles.active}")
|
||||
private String env;
|
||||
|
||||
@Autowired
|
||||
private CaptchaManager captchaManager;
|
||||
private final 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");
|
||||
@Override
|
||||
protected void verify(String captchaId, String captcha) {
|
||||
if (!env.equals("prod")) {
|
||||
return;
|
||||
}
|
||||
captchaManager.verify(captchaId, captcha);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
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;
|
||||
@@ -17,8 +15,20 @@ import java.lang.annotation.Target;
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface EnableSetting {
|
||||
|
||||
/** @return 配置键 */
|
||||
SettingKey value();
|
||||
/**
|
||||
*
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-04-27 14:45
|
||||
*/
|
||||
enum Logic {
|
||||
|
||||
AND,
|
||||
|
||||
OR
|
||||
}
|
||||
|
||||
Logic logic() default Logic.AND;
|
||||
|
||||
/** @return 未启用配置时响应消息语言映射键 */
|
||||
String message() default "service.offline";
|
||||
|
||||
@@ -1,17 +1,20 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import com.imyeyu.api.modules.common.service.SettingService;
|
||||
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;
|
||||
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 启用配置注解处理器
|
||||
*
|
||||
@@ -24,16 +27,28 @@ public class EnableSettingInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final SettingService service;
|
||||
|
||||
public boolean preHandle(@NonNull HttpServletRequest req, @NonNull HttpServletResponse resp, @NonNull Object handler) {
|
||||
public boolean preHandle(@NonNull HttpServletRequest req, @NonNull HttpServletResponse resp, @NonNull Object handler) throws InvocationTargetException, IllegalAccessException {
|
||||
if (handler instanceof HandlerMethod handlerMethod) {
|
||||
EnableSetting annotation = handlerMethod.getMethodAnnotation(EnableSetting.class);
|
||||
if (annotation == null) {
|
||||
return true;
|
||||
}
|
||||
if (service.is(annotation.value())) {
|
||||
return true;
|
||||
EnableSetting.Logic logic = annotation.logic();
|
||||
List<Boolean> valueList = new ArrayList<>();
|
||||
for (Method method : annotation.getClass().getMethods()) {
|
||||
if (method.getName().equals("message")) {
|
||||
continue;
|
||||
}
|
||||
Enum<?>[] enums = (Enum<?>[]) method.invoke(annotation);
|
||||
for (Enum<?> anEnum : enums) {
|
||||
valueList.add(service.getSystem(anEnum).isTrue());
|
||||
}
|
||||
}
|
||||
if (logic == EnableSetting.Logic.AND) {
|
||||
return valueList.stream().allMatch(Boolean::booleanValue);
|
||||
} else {
|
||||
return valueList.stream().anyMatch(Boolean::booleanValue);
|
||||
}
|
||||
throw new TimiException(TimiCode.ERROR_SERVICE_OFF, annotation.message());
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
/**
|
||||
* 标记需要在响应阶段自动回填的多语言字段
|
||||
*
|
||||
* @author 夜雨
|
||||
* @since 2026-07-17 00:00
|
||||
*/
|
||||
@Target({ElementType.FIELD})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface MultilingualField {
|
||||
|
||||
/**
|
||||
* 多语言 ID 来源字段名
|
||||
*
|
||||
* @return 来源字段名
|
||||
*/
|
||||
String value();
|
||||
|
||||
/**
|
||||
* 模板参数来源字段名
|
||||
*
|
||||
* @return 同类中的 Map 字段名
|
||||
*/
|
||||
String argMap() default "";
|
||||
}
|
||||
@@ -23,19 +23,21 @@ import org.springframework.stereotype.Component;
|
||||
@Component
|
||||
public class RequestRateLimitInterceptor extends RequestRateLimitAbstractInterceptor {
|
||||
|
||||
private static final String KEY_PREFIX = "REQUEST:RATE_LIMIT:";
|
||||
|
||||
@Autowired
|
||||
@Qualifier("redisRateLimit")
|
||||
private Redis<String, Integer> redisRequestRateLimit;
|
||||
private Redis<String, Integer> redis;
|
||||
|
||||
@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);
|
||||
String key = getKey(id);
|
||||
if (redis.has(key)) {
|
||||
Integer count = redis.get(key);
|
||||
if (count != null) {
|
||||
if (count < limit) {
|
||||
redisRequestRateLimit.setAndKeepTTL(key, ++count);
|
||||
redis.setAndKeepTTL(key, ++count);
|
||||
} else {
|
||||
log.warn("请求频率过高:[" + key + "].C" + count + "L" + limit);
|
||||
throw new TimiException(TimiCode.REQUEST_BAD).msgKey("request_rate_limit");
|
||||
@@ -43,7 +45,11 @@ public class RequestRateLimitInterceptor extends RequestRateLimitAbstractInterce
|
||||
}
|
||||
return true;
|
||||
}
|
||||
redisRequestRateLimit.set(key, 0, Time.S * cycle);
|
||||
redis.set(key, 0, Time.S * cycle);
|
||||
return true;
|
||||
}
|
||||
|
||||
private String getKey(String id) {
|
||||
return "%sTIMISERVERAPI:%s:%s".formatted(KEY_PREFIX, TimiSpring.getRequestIP(), id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import com.imyeyu.api.bean.PermissionMatchMode;
|
||||
import com.imyeyu.api.bean.CorePermissionCode;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
///
|
||||
/// 要求 CORE 模块权限
|
||||
///
|
||||
/// @author Codex
|
||||
/// @since 2026-07-26
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface RequireCorePermission {
|
||||
|
||||
/// 所需权限
|
||||
///
|
||||
/// @return 权限列表
|
||||
CorePermissionCode[] value();
|
||||
|
||||
/// 匹配模式
|
||||
///
|
||||
/// @return 匹配模式
|
||||
PermissionMatchMode mode() default PermissionMatchMode.ALL;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import com.imyeyu.api.bean.PermissionMatchMode;
|
||||
import com.imyeyu.api.bean.CorePermissionCode;
|
||||
import com.imyeyu.api.bean.ModuleCode;
|
||||
import com.imyeyu.api.modules.user.service.PermissionChecker;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
///
|
||||
/// CORE 权限注解处理器
|
||||
///
|
||||
/// @author Codex
|
||||
/// @since 2026-07-27
|
||||
@Component
|
||||
public class RequireCorePermissionInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final PermissionChecker permissionChecker;
|
||||
|
||||
public RequireCorePermissionInterceptor(PermissionChecker permissionChecker) {
|
||||
this.permissionChecker = permissionChecker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preHandle(@NonNull HttpServletRequest req, @NonNull HttpServletResponse resp, @NonNull Object handler) {
|
||||
if (!(handler instanceof HandlerMethod handlerMethod)) {
|
||||
return true;
|
||||
}
|
||||
RequireCorePermission annotation = handlerMethod.getMethodAnnotation(RequireCorePermission.class);
|
||||
if (annotation == null || TimiJava.isEmpty(annotation.value())) {
|
||||
return true;
|
||||
}
|
||||
String[] permissionCodeList = java.util.Arrays.stream(annotation.value()).map(CorePermissionCode::getValue).toArray(String[]::new);
|
||||
if (annotation.mode() == PermissionMatchMode.ANY) {
|
||||
permissionChecker.checkAny(ModuleCode.CORE, permissionCodeList);
|
||||
} else {
|
||||
permissionChecker.checkAll(ModuleCode.CORE, permissionCodeList);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import com.imyeyu.api.bean.RoleMatchMode;
|
||||
import com.imyeyu.api.bean.CoreRoleCode;
|
||||
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
import java.lang.annotation.RetentionPolicy;
|
||||
import java.lang.annotation.Target;
|
||||
|
||||
///
|
||||
/// 要求 CORE 模块角色
|
||||
///
|
||||
/// @author Codex
|
||||
/// @since 2026-07-26
|
||||
@Target({ElementType.METHOD, ElementType.TYPE})
|
||||
@Retention(RetentionPolicy.RUNTIME)
|
||||
public @interface RequireCoreRole {
|
||||
|
||||
/// 所需角色
|
||||
///
|
||||
/// @return 角色列表
|
||||
CoreRoleCode[] value();
|
||||
|
||||
/// 匹配模式
|
||||
///
|
||||
/// @return 匹配模式
|
||||
RoleMatchMode mode() default RoleMatchMode.ALL;
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.imyeyu.api.annotation;
|
||||
|
||||
import com.imyeyu.api.bean.ModuleCode;
|
||||
import com.imyeyu.api.bean.RoleMatchMode;
|
||||
import com.imyeyu.api.bean.CoreRoleCode;
|
||||
import com.imyeyu.api.modules.user.service.RoleChecker;
|
||||
import com.imyeyu.java.TimiJava;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import org.springframework.lang.NonNull;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.method.HandlerMethod;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
///
|
||||
/// CORE 角色注解处理器
|
||||
///
|
||||
/// @author Codex
|
||||
/// @since 2026-07-27
|
||||
@Component
|
||||
public class RequireCoreRoleInterceptor implements HandlerInterceptor {
|
||||
|
||||
private final RoleChecker roleChecker;
|
||||
|
||||
public RequireCoreRoleInterceptor(RoleChecker roleChecker) {
|
||||
this.roleChecker = roleChecker;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean preHandle(@NonNull HttpServletRequest req, @NonNull HttpServletResponse resp, @NonNull Object handler) {
|
||||
if (!(handler instanceof HandlerMethod handlerMethod)) {
|
||||
return true;
|
||||
}
|
||||
RequireCoreRole annotation = handlerMethod.getMethodAnnotation(RequireCoreRole.class);
|
||||
if (annotation == null || TimiJava.isEmpty(annotation.value())) {
|
||||
return true;
|
||||
}
|
||||
String[] roleCodeList = java.util.Arrays.stream(annotation.value()).map(CoreRoleCode::name).toArray(String[]::new);
|
||||
if (annotation.mode() == RoleMatchMode.ANY) {
|
||||
roleChecker.checkAny(ModuleCode.CORE, roleCodeList);
|
||||
} else {
|
||||
roleChecker.checkAll(ModuleCode.CORE, roleCodeList);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -1,39 +0,0 @@
|
||||
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