From e90cbff1461e833d222d728f6e2da213ac99ed58 Mon Sep 17 00:00:00 2001 From: Administrator Date: Wed, 8 Feb 2023 19:39:17 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=86=E9=A2=9170=EF=BC=88=E6=95=8F=E6=84=9F?= =?UTF-8?q?=E8=AF=8D=E6=A0=A1=E9=AA=8C=E4=BA=A4=E9=9B=86=E6=A0=A1=E9=AA=8C?= =?UTF-8?q?=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../cache/controller/CacheController.java | 19 +++++++ beacon-strategy/pom.xml | 6 +++ .../strategy/client/BeaconCacheClient.java | 6 +++ .../filter/impl/DirtyWordStrategyFilter.java | 49 +++++++++++++++++-- 4 files changed, 77 insertions(+), 3 deletions(-) diff --git a/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java b/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java index 5e10bb7..e163bbb 100644 --- a/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java +++ b/beacon-cache/src/main/java/com/mashibing/cache/controller/CacheController.java @@ -3,8 +3,11 @@ package com.mashibing.cache.controller; import com.msb.framework.redis.RedisClient; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.web.bind.annotation.*; +import javax.annotation.Resource; +import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; @@ -20,6 +23,9 @@ public class CacheController { @Autowired private RedisClient redisClient; + @Resource + private RedisTemplate redisTemplate; + @PostMapping(value = "/cache/hmset/{key}") public void hmset(@PathVariable(value = "key") String key, @RequestBody Map map) { log.info("【缓存模块】 hmset方法,存储key = {},存储value = {}", key, map); @@ -44,6 +50,19 @@ public class CacheController { redisClient.sAdd(key,value); } + @PostMapping(value = "/cache/sinterstr/{key}/{sinterKey}") + public Set sinterStr(@PathVariable(value = "key")String key, @PathVariable String sinterKey,@RequestBody String... value){ + log.info("【缓存模块】 sinterStr的交集方法,存储key = {},sinterKey = {},存储value = {}", key, sinterKey,value); + //1、 存储数据到set集合 + redisClient.sAdd(key,value); + //2、 需要将key和sinterKey做交集操作,并拿到返回的set + Set result = redisTemplate.opsForSet().intersect(key, sinterKey); + //3、 将key删除 + redisClient.delete(key); + //4、 返回交集结果 + return result; + } + @GetMapping("/cache/hgetall/{key}") public Map hGetAll(@PathVariable(value = "key")String key){ diff --git a/beacon-strategy/pom.xml b/beacon-strategy/pom.xml index 82ffa8f..f6da17d 100644 --- a/beacon-strategy/pom.xml +++ b/beacon-strategy/pom.xml @@ -44,6 +44,12 @@ org.springframework.cloud spring-cloud-starter-openfeign + + + com.janeluo + ikanalyzer + 2012_u6 + diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java b/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java index da295a4..7c107c9 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/client/BeaconCacheClient.java @@ -4,6 +4,9 @@ import org.springframework.cloud.openfeign.FeignClient; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; + +import java.util.Set; /** * @author zjw @@ -17,4 +20,7 @@ public interface BeaconCacheClient { @GetMapping(value = "/cache/get/{key}") String getString(@PathVariable(value = "key") String key); + + @PostMapping(value = "/cache/sinterstr/{key}/{sinterKey}") + Set sinterStr(@PathVariable(value = "key")String key, @PathVariable String sinterKey, @RequestBody String... value); } diff --git a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java index cfc2bf2..54b7bb5 100644 --- a/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java +++ b/beacon-strategy/src/main/java/com/mashibing/strategy/filter/impl/DirtyWordStrategyFilter.java @@ -1,20 +1,63 @@ package com.mashibing.strategy.filter.impl; +import com.mashibing.common.constant.CacheConstant; import com.mashibing.common.model.StandardSubmit; +import com.mashibing.strategy.client.BeaconCacheClient; import com.mashibing.strategy.filter.StrategyFilter; import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.wltea.analyzer.core.IKSegmenter; +import org.wltea.analyzer.core.Lexeme; + +import java.io.IOException; +import java.io.StringReader; +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; /** * 敏感词校验 * @author zjw * @description */ -@Service(value = "dirtyword") -@Slf4j -public class DirtyWordStrategyFilter implements StrategyFilter { + @Service(value = "dirtyword") + @Slf4j + public class DirtyWordStrategyFilter implements StrategyFilter { + + @Autowired + private BeaconCacheClient cacheClient; + @Override public void strategy(StandardSubmit submit) { log.info("【策略模块-敏感词校验】 校验ing…………"); + //1、 获取短信内容 + String text = submit.getText(); + + //2、 对短信内容进行分词,并且将分析内容存储到集合中 + Set contents = new HashSet<>(); + StringReader reader = new StringReader(text); + IKSegmenter ik = new IKSegmenter(reader,false); + Lexeme lex = null; + while(true){ + try { + if ((lex = ik.next()) == null) break; + } catch (IOException e) { + log.info("【策略模块-敏感词校验】 IK分词器在处理短信内容时,出现异常 e = {}" ,e.getMessage()); + } + contents.add(lex.getLexemeText()); + } + + + //3、 调用Cache缓存模块的交集方法,拿到结果 + + Set dirtyWords = cacheClient.sinterStr(UUID.randomUUID().toString(), CacheConstant.DIRTY_WORD, contents.toArray(new String[]{})); + + //4、 根据返回的set集合,判断是否包含敏感词 + if(dirtyWords != null && dirtyWords.size() > 0){ + //5、 如果有敏感词,抛出异常 / 其他操作。。 + log.info("【策略模块-敏感词校验】 短信内容包含敏感词信息, dirtyWords = {}",dirtyWords); + // 还需要做其他处理 + } } }