diff --git a/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java b/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java index 400b8d48..6edb49b0 100644 --- a/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java +++ b/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java @@ -1,6 +1,7 @@ package cn.stylefeng.guns.config.web; import cn.stylefeng.guns.core.error.CustomErrorAttributes; +import cn.stylefeng.guns.core.security.BlackWhiteInterceptor; import cn.stylefeng.guns.core.security.TokenAndPermissionInterceptor; import cn.stylefeng.roses.kernel.wrapper.field.jackson.CustomJacksonIntrospector; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; @@ -28,6 +29,9 @@ public class SpringMvcConfiguration implements WebMvcConfigurer { @Resource private TokenAndPermissionInterceptor tokenAndPermissionInterceptor; + @Resource + private BlackWhiteInterceptor blackWhiteInterceptor; + /** * 重写系统的默认错误提示 * @@ -62,7 +66,8 @@ public class SpringMvcConfiguration implements WebMvcConfigurer { */ @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(tokenAndPermissionInterceptor); + registry.addInterceptor(blackWhiteInterceptor).order(100); + registry.addInterceptor(tokenAndPermissionInterceptor).order(200); } /** diff --git a/src/main/java/cn/stylefeng/guns/core/security/BlackWhiteInterceptor.java b/src/main/java/cn/stylefeng/guns/core/security/BlackWhiteInterceptor.java new file mode 100644 index 00000000..c57508b7 --- /dev/null +++ b/src/main/java/cn/stylefeng/guns/core/security/BlackWhiteInterceptor.java @@ -0,0 +1,35 @@ +package cn.stylefeng.guns.core.security; + +import cn.stylefeng.roses.kernel.rule.util.HttpServletUtil; +import cn.stylefeng.roses.kernel.security.blackwhite.BlackWhiteValidateService; +import lombok.extern.slf4j.Slf4j; +import org.springframework.stereotype.Component; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.annotation.Resource; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +/** + * 黑白名单的web拦截器· + * + * @author fengshuonan + * @since 2024/7/11 10:11 + */ +@Component +@Slf4j +public class BlackWhiteInterceptor implements HandlerInterceptor { + + @Resource + private BlackWhiteValidateService blackWhiteValidateService; + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) { + + // 校验黑白名单 + blackWhiteValidateService.totalValidate(HttpServletUtil.getRequestClientIp(request)); + + return true; + } + +}