|
|
@@ -0,0 +1,55 @@
|
|
|
+package com.doc.framework.interceptor;
|
|
|
+
|
|
|
+import com.doc.common.utils.StringUtils;
|
|
|
+import com.doc.common.utils.ip.IpUtils;
|
|
|
+import com.doc.system.service.ISysConfigService;
|
|
|
+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;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.PrintWriter;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 白名单拦截
|
|
|
+ *
|
|
|
+ * @author wukai
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Component
|
|
|
+public class WhiteListInterceptor implements HandlerInterceptor {
|
|
|
+ @Resource
|
|
|
+ private ISysConfigService configService;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
|
|
|
+ String whiteList = configService.selectConfigByKey("access.whiteIPList");
|
|
|
+ String no = "false";
|
|
|
+ if (StringUtils.isEmpty(whiteList) || no.equals(whiteList)) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ if (IpUtils.isMatchedIp(whiteList, IpUtils.getIpAddr())) {
|
|
|
+ return true;
|
|
|
+ } else {
|
|
|
+ returnJson(response);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置请求拦截返回参数
|
|
|
+ *
|
|
|
+ * @param response 返回
|
|
|
+ */
|
|
|
+ private void returnJson(HttpServletResponse response) {
|
|
|
+ response.setCharacterEncoding("UTF-8");
|
|
|
+ response.setContentType("text/html; charset=utf-8");
|
|
|
+ try (PrintWriter writer = response.getWriter()) {
|
|
|
+ writer.print("{ \"msg\": \"非法IP访问\", \"code\": 417}");
|
|
|
+ } catch (IOException ignored) {
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|