|
@@ -0,0 +1,86 @@
|
|
|
+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.core.Ordered;
|
|
|
+import org.springframework.core.annotation.Order;
|
|
|
+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;
|
|
|
+ String requestUrl = request.getRequestURI();
|
|
|
+
|
|
|
+ PathMatcher matcher = new AntPathMatcher();
|
|
|
+ boolean match = false;
|
|
|
+
|
|
|
+ filterChain.doFilter(servletRequest, servletResponse);
|
|
|
+ for(String url : FILTER_URL){
|
|
|
+ match = matcher.match(url, requestUrl);
|
|
|
+ if (match){
|
|
|
+ String ip = NetUtils.getIpAddress(request);
|
|
|
+ String key = "browsing:" + ip + "#" + DateUtil.today();
|
|
|
+ //判断redis中是否存在key,存在则不记录
|
|
|
+ if(validRedisKey(key)){
|
|
|
+ filterChain.doFilter(servletRequest, servletResponse);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ //TODO 记录浏览记录入库
|
|
|
+
|
|
|
+
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @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;
|
|
|
+ }
|
|
|
+ //不存在则记录
|
|
|
+ redisCache.setCacheObject(key, key, 1, TimeUnit.DAYS);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+}
|