56 lines
1.8 KiB
Java
56 lines
1.8 KiB
Java
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 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;
|
|
|
|
/**
|
|
* 启用配置注解处理器
|
|
*
|
|
* @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) throws InvocationTargetException, IllegalAccessException {
|
|
if (handler instanceof HandlerMethod handlerMethod) {
|
|
EnableSetting annotation = handlerMethod.getMethodAnnotation(EnableSetting.class);
|
|
if (annotation == null) {
|
|
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);
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
}
|