Parcourir la source

浏览记录拦截器

1018653686@qq.com il y a 1 an
Parent
commit
159851f142

+ 85 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/filter/BrowsingFilter.java

@@ -0,0 +1,85 @@
+package com.fjhx.xmhjc.filter;
+
+import cn.hutool.core.date.DateUtil;
+import cn.hutool.core.util.StrUtil;
+import cn.hutool.extra.spring.SpringUtil;
+import com.fjhx.xmhjc.utils.NetUtils;
+import com.ruoyi.common.core.redis.RedisCache;
+import org.springframework.stereotype.Component;
+import org.springframework.util.AntPathMatcher;
+import org.springframework.util.PathMatcher;
+
+import javax.servlet.*;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * 浏览记录过滤器
+ * @author hj
+ * @date 2024年03月12日 9:47
+ */
+@Component
+public class BrowsingFilter implements Filter {
+    private static final RedisCache redisCache = SpringUtil.getBean(RedisCache.class);
+
+    private static final String[] FILTER_URL = {"/open/**"};
+
+
+
+
+    @Override
+    public void init(FilterConfig filterConfig) throws ServletException {
+        Filter.super.init(filterConfig);
+    }
+
+    @Override
+    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
+        HttpServletRequest request = (HttpServletRequest) servletRequest;
+        HttpServletResponse response = (HttpServletResponse) servletResponse;
+        String requestUrl = request.getRequestURI();
+
+        PathMatcher matcher = new AntPathMatcher();
+        boolean match = false;
+        for(String url : FILTER_URL){
+            match = matcher.match(url, requestUrl);
+            if (match){
+                break;
+            }
+        }
+        //在过滤范围内记录浏览记录
+        if (match){
+            String ip = NetUtils.getIpAddress(request);
+            String key = "browsing:" + ip + ":" + DateUtil.today();
+            //判断redis中是否存在key,存在则不记录
+            if(validRedisKey(key)){
+                filterChain.doFilter(servletRequest, servletResponse);
+                return;
+            }
+            //不存在则记录
+            redisCache.setCacheObject(key, key, 1, TimeUnit.DAYS);
+            //TODO 记录浏览记录入库
+
+        }
+    }
+
+    @Override
+    public void destroy() {
+        Filter.super.destroy();
+    }
+
+
+    /**
+     * 判断redis中是否存在key
+     * @param key
+     * @return true 存在 false 不存在
+     */
+    private synchronized boolean validRedisKey(String key){
+        String cacheObject = redisCache.getCacheObject(key);
+        if(StrUtil.isNotBlank(cacheObject)){
+            return true;
+        }
+        return false;
+    }
+}

+ 85 - 0
hx-xmhjc/src/main/java/com/fjhx/xmhjc/utils/NetUtils.java

@@ -0,0 +1,85 @@
+package com.fjhx.xmhjc.utils;
+
+import javax.servlet.http.HttpServletRequest;
+import java.net.InetAddress;
+import java.net.NetworkInterface;
+import java.net.UnknownHostException;
+
+/**
+ * 网络工具类
+ *
+ */
+public class NetUtils {
+
+    /**
+     * 获取客户端 IP 地址
+     *
+     * @param request
+     * @return
+     */
+    public static String getIpAddress(HttpServletRequest request) {
+        String ip = request.getHeader("x-forwarded-for");
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getHeader("Proxy-Client-IP");
+        }
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getHeader("WL-Proxy-Client-IP");
+        }
+        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
+            ip = request.getRemoteAddr();
+            if (ip.equals("127.0.0.1")) {
+                // 根据网卡取本机配置的 IP
+                InetAddress inet = null;
+                try {
+                    inet = InetAddress.getLocalHost();
+                } catch (Exception e) {
+                    e.printStackTrace();
+                }
+                if (inet != null) {
+                    ip = inet.getHostAddress();
+                }
+            }
+        }
+        // 多个代理的情况,第一个IP为客户端真实IP,多个IP按照','分割
+        if (ip != null && ip.length() > 15) {
+            if (ip.indexOf(",") > 0) {
+                ip = ip.substring(0, ip.indexOf(","));
+            }
+        }
+        // 本机访问
+        if ("localhost".equalsIgnoreCase(ip) || "127.0.0.1".equalsIgnoreCase(ip) || "0:0:0:0:0:0:0:1".equalsIgnoreCase(ip)){
+            // 根据网卡取本机配置的IP
+            InetAddress inet;
+            try {
+                inet = InetAddress.getLocalHost();
+                ip = inet.getHostAddress();
+            } catch (UnknownHostException e) {
+                e.printStackTrace();
+            }
+        }
+        // 如果查找不到 IP,可以返回 127.0.0.1,可以做一定的处理,但是这里不考虑
+        // if (ip == null) {
+        //     return "127.0.0.1";
+        // }
+        return ip;
+    }
+    
+  /**
+     * 获取mac地址
+     */
+    public static String getMacAddress() throws Exception {
+        // 取mac地址
+        byte[] macAddressBytes = NetworkInterface.getByInetAddress(InetAddress.getLocalHost()).getHardwareAddress();
+        // 下面代码是把mac地址拼装成String
+        StringBuilder sb = new StringBuilder();
+        for (int i = 0; i < macAddressBytes.length; i++) {
+            if (i != 0) {
+                sb.append("-");
+            }
+            // mac[i] & 0xFF 是为了把byte转化为正整数
+            String s = Integer.toHexString(macAddressBytes[i] & 0xFF);
+            sb.append(s.length() == 1 ? 0 + s : s);
+        }
+        return sb.toString().trim().toUpperCase();
+    }
+}