41 lines
1.3 KiB
Java
41 lines
1.3 KiB
Java
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;
|
|
}
|
|
}
|