通过rpc调用 敏感字检测
parent
cd8777c449
commit
ce1db905a9
|
@ -261,4 +261,14 @@ public class CoreService implements RPCRequestIFace.Iface {
|
|||
}
|
||||
return arenaOfHero;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isContaintSensitiveWord(String txt, int matchType) throws TException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<String> getSensitiveWord(String txt, int matchType) throws TException {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,7 +107,7 @@ public class GameLogicService implements IService {
|
|||
|
||||
MapLogic.getInstance().init();
|
||||
MongoKeys.initmongoKey();
|
||||
SensitivewordFilter.init();
|
||||
// SensitivewordFilter.init();
|
||||
|
||||
KeyGenUtils.setMachineNum(GameApplication.serverProperties.getNum());
|
||||
ActivityLogic.getInstance().checkActiviyStatus();
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,8 +1,15 @@
|
|||
package com.ljsd.jieling.util;
|
||||
|
||||
import com.ljsd.jieling.db.redis.RedisUtil;
|
||||
import com.ljsd.jieling.thrift.idl.RPCRequestIFace;
|
||||
import com.ljsd.jieling.thrift.pool.ClientAdapterPo;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import rpc.thrift.ServiceKey;
|
||||
import util.StringUtil;
|
||||
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
|
@ -10,19 +17,11 @@ import java.util.Set;
|
|||
* @Description: 敏感词过滤
|
||||
*/
|
||||
public class SensitivewordFilter {
|
||||
@SuppressWarnings("rawtypes")
|
||||
private static Map sensitiveWordMap = null;
|
||||
public static final int minMatchTYpe = 1; //最小匹配规则
|
||||
public static final int maxMatchType = 2; //最大匹配规则
|
||||
|
||||
/**
|
||||
* 构造函数,初始化敏感词库
|
||||
*/
|
||||
public SensitivewordFilter() {}
|
||||
// 敏感字检测服地址
|
||||
public static final String SENSIT_WORD_FILTER_KEY = "SENSIT_WORD_FILTER_KEY";
|
||||
|
||||
public static void init(){
|
||||
sensitiveWordMap = SensitiveWordInit.initKeyWord();
|
||||
}
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SensitivewordFilter.class);
|
||||
|
||||
|
||||
/**
|
||||
|
@ -34,14 +33,8 @@ public class SensitivewordFilter {
|
|||
* @version 1.0
|
||||
*/
|
||||
public static boolean isContaintSensitiveWord(String txt, int matchType) {
|
||||
boolean flag = false;
|
||||
for (int i = 0; i < txt.length(); i++) {
|
||||
int matchFlag = CheckSensitiveWord(txt, i, matchType); //判断是否包含敏感字符
|
||||
if (matchFlag > 0) { //大于0存在,返回true
|
||||
flag = true;
|
||||
}
|
||||
}
|
||||
return flag;
|
||||
boolean hasDirtyWords = isContaintSensitiveWordThrift(txt, matchType);
|
||||
return hasDirtyWords;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,18 +45,32 @@ public class SensitivewordFilter {
|
|||
* @return
|
||||
* @version 1.0
|
||||
*/
|
||||
public static Set<String> getSensitiveWord(String txt, int matchType) {
|
||||
Set<String> sensitiveWordList = new HashSet<>();
|
||||
|
||||
for (int i = 0; i < txt.length(); i++) {
|
||||
int length = CheckSensitiveWord(txt, i, matchType); //判断是否包含敏感字符
|
||||
if (length > 0) { //存在,加入list中
|
||||
sensitiveWordList.add(txt.substring(i, i + length));
|
||||
i = i + length - 1; //减1的原因,是因为for会自增
|
||||
private static Set<String> getSensitiveWord(String txt, int matchType) {
|
||||
Object o = RedisUtil.getInstence().get(SENSIT_WORD_FILTER_KEY);
|
||||
if (o == null) {
|
||||
return new HashSet<>(0);
|
||||
}
|
||||
String address = (String) o;
|
||||
String[] split = address.split("\\:");
|
||||
String serviceKey = StringUtil.getServiceKey(ServiceKey.RPCCore, split[0], split[1]);
|
||||
ClientAdapterPo<RPCRequestIFace.Client> rPCClient = null;
|
||||
try {
|
||||
rPCClient = ClientAdapterPo.getClientAdapterPo(serviceKey);
|
||||
if (rPCClient == null) {
|
||||
return null;
|
||||
}
|
||||
Set<String> sensitiveWord = rPCClient.getClient().getSensitiveWord(txt, matchType);
|
||||
return sensitiveWord;
|
||||
} catch (Exception e) {
|
||||
LOGGER.info("getSensitiveWord callback=>",e);
|
||||
return null;
|
||||
}finally {
|
||||
if (rPCClient != null){
|
||||
rPCClient.returnObject(serviceKey);
|
||||
}else{
|
||||
LOGGER.info("getSensitiveWord => rPCClient is null ");
|
||||
}
|
||||
}
|
||||
|
||||
return sensitiveWordList;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -108,52 +115,34 @@ public class SensitivewordFilter {
|
|||
|
||||
/**
|
||||
* 检查文字中是否包含敏感字符,检查规则如下:<br>
|
||||
*
|
||||
* @param txt
|
||||
* @param beginIndex
|
||||
* @param matchType
|
||||
* @return,如果存在,则返回敏感词字符的长度,不存在返回0
|
||||
* @version 1.0
|
||||
*/
|
||||
@SuppressWarnings({"rawtypes"})
|
||||
public static int CheckSensitiveWord(String txt, int beginIndex, int matchType) {
|
||||
boolean flag = false; //敏感词结束标识位:用于敏感词只有1位的情况
|
||||
int matchFlag = 0; //匹配标识数默认为0
|
||||
char word = 0;
|
||||
Map nowMap = sensitiveWordMap;
|
||||
for (int i = beginIndex; i < txt.length(); i++) {
|
||||
word = txt.charAt(i);
|
||||
nowMap = (Map) nowMap.get(word); //获取指定key
|
||||
if (nowMap != null) { //存在,则判断是否为最后一个
|
||||
matchFlag++; //找到相应key,匹配标识+1
|
||||
if ("1".equals(nowMap.get("isEnd"))) { //如果为最后一个匹配规则,结束循环,返回匹配标识数
|
||||
flag = true; //结束标志位为true
|
||||
if (SensitivewordFilter.minMatchTYpe == matchType) { //最小规则,直接返回,最大规则还需继续查找
|
||||
break;
|
||||
}
|
||||
}
|
||||
} else { //不存在,直接返回
|
||||
break;
|
||||
private static boolean isContaintSensitiveWordThrift(String userName, int type){
|
||||
Object o = RedisUtil.getInstence().get(SENSIT_WORD_FILTER_KEY);
|
||||
if (o == null) {
|
||||
return false;
|
||||
}
|
||||
String address = (String) o;
|
||||
String[] split = address.split("\\:");
|
||||
String serviceKey = StringUtil.getServiceKey(ServiceKey.RPCCore, split[0], split[1]);
|
||||
ClientAdapterPo<RPCRequestIFace.Client> rPCClient = null;
|
||||
try {
|
||||
rPCClient = ClientAdapterPo.getClientAdapterPo(serviceKey);
|
||||
if (rPCClient == null) {
|
||||
return false;
|
||||
}
|
||||
boolean result = rPCClient.getClient().isContaintSensitiveWord(userName, type);
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
LOGGER.info("getSensitiveWord callback=>",e);
|
||||
return false;
|
||||
}finally {
|
||||
if (rPCClient != null){
|
||||
rPCClient.returnObject(serviceKey);
|
||||
}else{
|
||||
LOGGER.info("getSensitiveWord => rPCClient is null ");
|
||||
}
|
||||
}
|
||||
if (matchFlag < 1 || !flag) { //长度大于等于1,为词
|
||||
matchFlag = 0;
|
||||
}
|
||||
return matchFlag;
|
||||
}
|
||||
|
||||
// public static void main(String[] args) {
|
||||
// SensitivewordFilter filter = new SensitivewordFilter();
|
||||
// filter.init();
|
||||
// LOGGER.info("敏感词的数量:" + filter.sensitiveWordMap.size());
|
||||
// String string = "太多的伤感情怀也许只局限于饲养基地 荧幕中的情节,主人公尝试着去用妈了个逼某种方式渐渐的很潇洒地释自杀指南怀那些自己经历的伤感。"
|
||||
// + "然后法轮功 我们的扮演的角色就是跟随着主人公的喜红客联盟 怒哀乐而过于牵强的把自己的情感也附加于银幕情节中,然后感动就流泪,"
|
||||
// + "难过就躺在某一个人的怀里76习近平56尽情狗日的的阐述心扉或者手机卡复制器一个人一杯红酒一部电影在夜三级片 深人静的晚上,关上电话静静的发呆着。";
|
||||
// LOGGER.info("待检测语句字数:" + string.length());
|
||||
// long beginTime = System.currentTimeMillis();
|
||||
// Set<String> set = filter.getSensitiveWord(string, 1);
|
||||
// long endTime = System.currentTimeMillis();
|
||||
// LOGGER.info("语句中包含敏感词的个数为:" + set.size() + "。包含:" + set);
|
||||
// LOGGER.info("总共消耗时间为:" + (endTime - beginTime));
|
||||
// }
|
||||
}
|
||||
|
|
|
@ -98,5 +98,10 @@ service RPCRequestIFace{
|
|||
RechargeResult getRecharge(1:i32 uid);
|
||||
void mathcRoomAdressInfo(1:i32 uid,2:i32 type,3:string address);
|
||||
CrossArenaManager getHeroManagerInfo(1:i32 uid);
|
||||
|
||||
// 判断文字是否包含敏感字符
|
||||
bool isContaintSensitiveWord(1:string txt, 2:i32 matchType);
|
||||
// 获取文字中的敏感词
|
||||
set<string> getSensitiveWord(1:string txt, 2:i32 matchType);
|
||||
}
|
||||
|
||||
|
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue