From 7830c7a42c2cb8e9cdd1380d61c259d2028cda4d Mon Sep 17 00:00:00 2001 From: stylefeng Date: Thu, 11 Jul 2024 11:15:46 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E6=8B=A6=E6=88=AA=E5=99=A8=E3=80=91?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=BB=91=E7=99=BD=E5=90=8D=E5=8D=95=E6=A0=A1?= =?UTF-8?q?=E9=AA=8C=E6=8B=A6=E6=88=AA=E5=99=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../config/web/SpringMvcConfiguration.java | 7 +++- .../core/security/BlackWhiteInterceptor.java | 35 +++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 src/main/java/cn/stylefeng/guns/core/security/BlackWhiteInterceptor.java 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; + } + +}