Browse Source

敏感词检测

seyason 4 years ago
parent
commit
f742901a80

+ 25 - 6
fhKeeper/formulahousekeeper/management-platform/src/main/java/com/management/platform/aop/SensitiveWordConfig.java

@@ -15,6 +15,7 @@ import javax.servlet.http.HttpServletRequest;
 import java.lang.reflect.Field;
 import java.lang.reflect.Method;
 import java.lang.reflect.Modifier;
+import java.time.LocalDate;
 
 /**
  * 敏感词过滤
@@ -27,7 +28,7 @@ public class SensitiveWordConfig {
     @Autowired
     WordFilter wordFilter;
 
-    @Around("@annotation(org.springframework.web.bind.annotation.PostMapping)||@annotation(org.springframework.web.bind.annotation.PutMapping)")
+    @Around("@annotation(org.springframework.web.bind.annotation.RequestMapping)||@annotation(org.springframework.web.bind.annotation.PutMapping)||@annotation(org.springframework.web.bind.annotation.PostMapping)")
     @SneakyThrows
     public Object doBefore(ProceedingJoinPoint point) {
         System.out.println("####################进入敏感词检测#######");
@@ -36,13 +37,30 @@ public class SensitiveWordConfig {
             return point.proceed();
         }
         //所有的参数对象
-        for (Object arg : point.getArgs()) {
+        boolean hasSentiveWords = false;
+        for (int i=0;i<point.getArgs().length; i++) {
+            Object arg = point.getArgs()[i];
             //参数对象,通过反射将String类型的值进行敏感词过滤
-            Class<?> aClass = arg.getClass();
-            //递归遍历,将所有String参数进行敏感词匹配
-            foundString(aClass,arg);
+            System.out.println("获取到参数:");
+            if (arg != null) {
+                Class<?> aClass = arg.getClass();
+                System.out.println("获取到aClass:"+aClass);
+                if(aClass == String.class) {
+                    //本身就是string类型
+                    String afterValue = wordFilter.replaceWords((String)(arg));
+                    System.out.println("替换后=" + afterValue);
+                    point.getArgs()[i] = afterValue;
+                    if (!afterValue.equals(arg)) {
+                        hasSentiveWords = true;
+                        break;
+                    }
+                } else if (aClass != Integer.class && aClass != LocalDate.class) {
+                    //递归遍历,将所有String参数进行敏感词匹配
+                    foundString(aClass,arg);
+                }
+            }
         }
-        return  point.proceed();
+        return  point.proceed(point.getArgs());
     }
 
     /**
@@ -60,6 +78,7 @@ public class SensitiveWordConfig {
                 //如果是String类型,进行关键词匹配 且要排除final修饰的字段
                 declaredField.setAccessible(true);
                 String value=(String)declaredField.get(arg);
+                System.out.println("参数值:"+value);
                 declaredField.set(arg, wordFilter.replaceWords(value));
             }else if (type.getPackage()!=null&&type.getPackage().getName().contains("com.bysk")){
                 Method[] methods = clazz.getMethods();

+ 1 - 1
fhKeeper/formulahousekeeper/management-platform/src/main/resources/application-prod.yml

@@ -24,7 +24,7 @@ spring:
     #######redis配置######
     # redis
     redis:
-      host: 118.190.47.230
+      host: 127.0.0.1
       port: 6379
       timeout: 3
       # password: