Commit ac1e1caf by leejoker

提交敏感词相关代码:

1. 增加敏感词命中查询和保存
2. 修改sql文件
3. 在插入信息到es时使用异步方法
1 parent 89cb9da5
...@@ -7,10 +7,10 @@ import com.linkwechat.common.core.domain.AjaxResult; ...@@ -7,10 +7,10 @@ import com.linkwechat.common.core.domain.AjaxResult;
import com.linkwechat.common.core.page.TableDataInfo; import com.linkwechat.common.core.page.TableDataInfo;
import com.linkwechat.common.enums.BusinessType; import com.linkwechat.common.enums.BusinessType;
import com.linkwechat.wecom.domain.WeSensitive; import com.linkwechat.wecom.domain.WeSensitive;
import com.linkwechat.wecom.domain.query.WeSensitiveHitQuery;
import com.linkwechat.wecom.service.IWeSensitiveService; import com.linkwechat.wecom.service.IWeSensitiveService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.validation.Valid; import javax.validation.Valid;
...@@ -19,7 +19,7 @@ import java.util.List; ...@@ -19,7 +19,7 @@ import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/** /**
* 敏感词设置Controller * 敏感词Controller
* *
* @author ruoyi * @author ruoyi
* @date 2020-12-29 * @date 2020-12-29
...@@ -87,4 +87,13 @@ public class WeSensitiveController extends BaseController { ...@@ -87,4 +87,13 @@ public class WeSensitiveController extends BaseController {
Arrays.stream(id).map(Long::parseLong).collect(Collectors.toList()).toArray(idArray); Arrays.stream(id).map(Long::parseLong).collect(Collectors.toList()).toArray(idArray);
return toAjax(weSensitiveService.destroyWeSensitiveByIds(idArray)); return toAjax(weSensitiveService.destroyWeSensitiveByIds(idArray));
} }
/**
* 敏感词命中查询
*/
@PreAuthorize("@ss.hasPermi('wecom:sensitivehit:list')")
@GetMapping("/hit/list")
public TableDataInfo hitList(WeSensitiveHitQuery query) {
return getDataTable(weSensitiveService.getHitSensitiveList(query));
}
} }
package com.linkwechat.common.constant; package com.linkwechat.common.constant;
import java.io.IOException;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
...@@ -149,7 +148,7 @@ public class WeConstans { ...@@ -149,7 +148,7 @@ public class WeConstans {
/** /**
* 批量生成的单人码 活动场景 * 批量生成的单人码 活动场景
*/ */
public static final String ONE_PERSON_CODE_GENERATED_BATCH="批量生成的单人码"; public static final String ONE_PERSON_CODE_GENERATED_BATCH = "批量生成的单人码";
/** /**
* 微信接口相应端错误字段 * 微信接口相应端错误字段
...@@ -222,6 +221,11 @@ public class WeConstans { ...@@ -222,6 +221,11 @@ public class WeConstans {
public static final Integer ID_TYPE_MACHINE = 2; public static final Integer ID_TYPE_MACHINE = 2;
/** /**
* 一次拉取的消息条数,最大值1000条,超过1000条会返回错误
*/
public static final long LIMIT = 1_000L;
/**
* 企微回调事件类型路由 * 企微回调事件类型路由
*/ */
public final static Map<String, String> eventRoute = new HashMap<String, String>() { public final static Map<String, String> eventRoute = new HashMap<String, String>() {
...@@ -285,6 +289,10 @@ public class WeConstans { ...@@ -285,6 +289,10 @@ public class WeConstans {
public static final String WECOM_FINANCE_INDEX = "finance"; public static final String WECOM_FINANCE_INDEX = "finance";
/** 开启会话存档成员列表 **/ public static final String WECOM_SENSITIVE_HIT_INDEX = "sensitive";
/**
* 开启会话存档成员列表
**/
public static final String weMsgAuditKey = "wecom_msg_audit:user:ids"; public static final String weMsgAuditKey = "wecom_msg_audit:user:ids";
} }
...@@ -2,10 +2,11 @@ package com.linkwechat.common.core.elasticsearch; ...@@ -2,10 +2,11 @@ package com.linkwechat.common.core.elasticsearch;
import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo; import com.github.pagehelper.PageInfo;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity; import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteRequest;
...@@ -29,13 +30,14 @@ import org.elasticsearch.search.SearchHit; ...@@ -29,13 +30,14 @@ import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.io.IOException; import java.io.IOException;
import java.util.*; import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
/** /**
* @author sxw * @author sxw
...@@ -206,6 +208,24 @@ public class ElasticSearch { ...@@ -206,6 +208,24 @@ public class ElasticSearch {
} }
/** /**
* 异步批量插入,并执行回调方法
*
* @param idxName
* @param list
* @param consumers
*/
public void insertBatchAsync(String idxName, List<JSONObject> list, List<Consumer<List<JSONObject>>> consumers) {
BulkRequest request = new BulkRequest();
list.forEach(item -> request.add(new IndexRequest(idxName, "_doc").id(item.getString("msgid"))
.source(item, XContentType.JSON)));
try {
restHighLevelClient.bulkAsync(request, RequestOptions.DEFAULT, getActionListener(list, consumers));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 批量删除 * 批量删除
* *
* @param idxName index * @param idxName index
...@@ -260,7 +280,7 @@ public class ElasticSearch { ...@@ -260,7 +280,7 @@ public class ElasticSearch {
request.source(builder); request.source(builder);
try { try {
SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT); SearchResponse response = restHighLevelClient.search(request, RequestOptions.DEFAULT);
int totalHits =(int) response.getHits().getTotalHits().value; int totalHits = (int) response.getHits().getTotalHits().value;
SearchHit[] hits = response.getHits().getHits(); SearchHit[] hits = response.getHits().getHits();
List<T> res = new ArrayList<>(hits.length); List<T> res = new ArrayList<>(hits.length);
for (SearchHit hit : hits) { for (SearchHit hit : hits) {
...@@ -269,7 +289,7 @@ public class ElasticSearch { ...@@ -269,7 +289,7 @@ public class ElasticSearch {
Map<String, HighlightField> highlightFields = hit.getHighlightFields(); Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField hghlightContent = highlightFields.get("text.content"); HighlightField hghlightContent = highlightFields.get("text.content");
String newName = ""; String newName = "";
if (hghlightContent != null){ if (hghlightContent != null) {
//获取该高亮字段的高亮信息 //获取该高亮字段的高亮信息
Text[] fragments = hghlightContent.getFragments(); Text[] fragments = hghlightContent.getFragments();
//将前缀、关键词、后缀进行拼接 //将前缀、关键词、后缀进行拼接
...@@ -278,7 +298,7 @@ public class ElasticSearch { ...@@ -278,7 +298,7 @@ public class ElasticSearch {
} }
} }
Map<String, Object> sourceAsMap = hit.getSourceAsMap(); Map<String, Object> sourceAsMap = hit.getSourceAsMap();
sourceAsMap.put("content",newName); sourceAsMap.put("content", newName);
res.add(JSON.parseObject(JSONObject.toJSONString(sourceAsMap), c)); res.add(JSON.parseObject(JSONObject.toJSONString(sourceAsMap), c));
} }
// 封装分页 // 封装分页
...@@ -342,6 +362,20 @@ public class ElasticSearch { ...@@ -342,6 +362,20 @@ public class ElasticSearch {
} }
} }
public ActionListener getActionListener(List<JSONObject> list, List<Consumer<List<JSONObject>>> consumers) {
return new ActionListener() {
@Override
public void onResponse(Object o) {
consumers.forEach(consumer -> consumer.accept(list));
}
@Override
public void onFailure(Exception e) {
log.warn("work with es failed, exception={}", ExceptionUtils.getStackTrace(e));
}
};
}
public XContentBuilder getFinanceMapping() throws IOException { public XContentBuilder getFinanceMapping() throws IOException {
// 创建 会话文本Mapping // 创建 会话文本Mapping
XContentBuilder xContentBuilder = XContentFactory.jsonBuilder() XContentBuilder xContentBuilder = XContentFactory.jsonBuilder()
...@@ -372,4 +406,4 @@ public class ElasticSearch { ...@@ -372,4 +406,4 @@ public class ElasticSearch {
.endObject(); .endObject();
return xContentBuilder; return xContentBuilder;
} }
} }
\ No newline at end of file \ No newline at end of file
...@@ -35,10 +35,6 @@ public class FinanceUtils { ...@@ -35,10 +35,6 @@ public class FinanceUtils {
* 超时时间,单位秒 * 超时时间,单位秒
*/ */
private final static long timeout = 5 * 60; private final static long timeout = 5 * 60;
/**
* 一次拉取的消息条数,最大值1000条,超过1000条会返回错误
*/
private final static long LIMIT = 1000;
private static String downloadWeWorkPath = RuoYiConfig.getDownloadWeWorkPath(); private static String downloadWeWorkPath = RuoYiConfig.getDownloadWeWorkPath();
...@@ -67,7 +63,7 @@ public class FinanceUtils { ...@@ -67,7 +63,7 @@ public class FinanceUtils {
public static List<JSONObject> getChatData(long seq, String proxy, String passwd, RedisCache redisCache) { public static List<JSONObject> getChatData(long seq, String proxy, String passwd, RedisCache redisCache) {
List<JSONObject> resList = new ArrayList<>(); List<JSONObject> resList = new ArrayList<>();
long slice = Finance.NewSlice(); long slice = Finance.NewSlice();
int ret = Finance.GetChatData(sdk, seq, LIMIT, proxy, passwd, timeout, slice); int ret = Finance.GetChatData(sdk, seq, WeConstans.LIMIT, proxy, passwd, timeout, slice);
if (ret != 0) { if (ret != 0) {
log.info("getChatData ret " + ret); log.info("getChatData ret " + ret);
return null; return null;
...@@ -82,8 +78,8 @@ public class FinanceUtils { ...@@ -82,8 +78,8 @@ public class FinanceUtils {
LocalSEQ.set(data.getLong("seq")); LocalSEQ.set(data.getLong("seq"));
JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"), JSONObject jsonObject = decryptChatRecord(sdk, data.getString("encrypt_random_key"),
data.getString("encrypt_chat_msg"), privateKey); data.getString("encrypt_chat_msg"), privateKey);
if (jsonObject ==null){ if (jsonObject == null) {
return; return;
} }
jsonObject.put("seq", LocalSEQ.get()); jsonObject.put("seq", LocalSEQ.get());
resList.add(jsonObject); resList.add(jsonObject);
...@@ -94,7 +90,7 @@ public class FinanceUtils { ...@@ -94,7 +90,7 @@ public class FinanceUtils {
log.info("数据解析完成:------------"); log.info("数据解析完成:------------");
} }
Finance.FreeSlice(slice); Finance.FreeSlice(slice);
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,LocalSEQ.get()); redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY, LocalSEQ.get());
return resList; return resList;
} }
...@@ -123,7 +119,7 @@ public class FinanceUtils { ...@@ -123,7 +119,7 @@ public class FinanceUtils {
if (StringUtils.isNotEmpty(msgType)) { if (StringUtils.isNotEmpty(msgType)) {
getSwitchType(realJsonData, msgType); getSwitchType(realJsonData, msgType);
} }
log.info("数据解析:------------"+ realJsonData.toJSONString()); log.info("数据解析:------------" + realJsonData.toJSONString());
return realJsonData; return realJsonData;
} catch (Exception e) { } catch (Exception e) {
log.error("解析密文失败"); log.error("解析密文失败");
...@@ -278,5 +274,4 @@ public class FinanceUtils { ...@@ -278,5 +274,4 @@ public class FinanceUtils {
} }
} }
} }
} }
...@@ -2,12 +2,14 @@ package com.linkwechat.quartz.task; ...@@ -2,12 +2,14 @@ package com.linkwechat.quartz.task;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.WeConstans; import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity; import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.core.elasticsearch.ElasticSearch; import com.linkwechat.common.core.elasticsearch.ElasticSearch;
import com.linkwechat.common.core.redis.RedisCache; import com.linkwechat.common.core.redis.RedisCache;
import com.linkwechat.common.utils.StringUtils; import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.service.IWeChatContactMappingService; import com.linkwechat.wecom.service.IWeChatContactMappingService;
import com.linkwechat.wecom.service.IWeSensitiveService;
import com.tencent.wework.FinanceUtils; import com.tencent.wework.FinanceUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.formula.functions.T; import org.apache.poi.ss.formula.functions.T;
...@@ -38,6 +40,8 @@ public class RyTask { ...@@ -38,6 +40,8 @@ public class RyTask {
private RedisCache redisCache; private RedisCache redisCache;
@Autowired @Autowired
private IWeChatContactMappingService weChatContactMappingService; private IWeChatContactMappingService weChatContactMappingService;
@Autowired
private IWeSensitiveService weSensitiveService;
public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) { public void ryMultipleParams(String s, Boolean b, Long l, Double d, Integer i) {
System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i)); System.out.println(StringUtils.format("执行多参方法: 字符串类型{},布尔类型{},长整型{},浮点型{},整形{}", s, b, l, d, i));
...@@ -72,8 +76,10 @@ public class RyTask { ...@@ -72,8 +76,10 @@ public class RyTask {
"", redisCache); "", redisCache);
if (CollectionUtil.isNotEmpty(chatDataList)){ if (CollectionUtil.isNotEmpty(chatDataList)){
try { try {
List<ElasticSearchEntity> elasticSearchEntities = weChatContactMappingService.saveWeChatContactMapping(chatDataList); List<Consumer<List<JSONObject>>> consumerList = Lists.newArrayList();
elasticSearch.insertBatch(WeConstans.WECOM_FINANCE_INDEX, elasticSearchEntities); consumerList.add(weChatContactMappingService::saveWeChatContactMapping);
consumerList.add(weSensitiveService::hitSensitive);
elasticSearch.insertBatchAsync(WeConstans.WECOM_FINANCE_INDEX, chatDataList, consumerList);
} catch (Exception e) { } catch (Exception e) {
log.error("消息处理异常:ex:{}", e); log.error("消息处理异常:ex:{}", e);
e.printStackTrace(); e.printStackTrace();
...@@ -88,7 +94,7 @@ public class RyTask { ...@@ -88,7 +94,7 @@ public class RyTask {
searchSourceBuilder.size(1); searchSourceBuilder.size(1);
List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class); List<JSONObject> searchResultList = elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, searchSourceBuilder, JSONObject.class);
searchResultList.stream().findFirst().ifPresent(result ->{ searchResultList.stream().findFirst().ifPresent(result ->{
index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY)); index.set(result.getLong(WeConstans.CONTACT_SEQ_KEY) + 1);
}); });
redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,index); redisCache.setCacheObject(WeConstans.CONTACT_SEQ_KEY,index);
} }
......
...@@ -14,7 +14,6 @@ import org.apache.commons.lang3.builder.ToStringStyle; ...@@ -14,7 +14,6 @@ import org.apache.commons.lang3.builder.ToStringStyle;
import javax.validation.constraints.NotBlank; import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotEmpty; import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.List; import java.util.List;
/** /**
...@@ -66,8 +65,8 @@ public class WeSensitive extends BaseEntity { ...@@ -66,8 +65,8 @@ public class WeSensitive extends BaseEntity {
*/ */
@TableField(value = "audit_user_id") @TableField(value = "audit_user_id")
@ApiModelProperty(value = "审计人id") @ApiModelProperty(value = "审计人id")
@NotNull(message = "审计人id不能为空") @NotBlank(message = "审计人id不能为空")
private Long auditUserId; private String auditUserId;
/** /**
* 审计人姓名 * 审计人姓名
......
...@@ -42,7 +42,7 @@ public class WeSensitiveAuditScope implements Serializable { ...@@ -42,7 +42,7 @@ public class WeSensitiveAuditScope implements Serializable {
*/ */
@TableField(value = "audit_scope_id") @TableField(value = "audit_scope_id")
@ApiModelProperty(value = "审计对象id") @ApiModelProperty(value = "审计对象id")
private Long auditScopeId; private String auditScopeId;
/** /**
* 审计对象名称 * 审计对象名称
......
package com.linkwechat.wecom.domain.query;
import com.linkwechat.common.core.domain.BaseEntity;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
/**
* @author leejoker <1056650571@qq.com>
* @version 1.0
* @date 2021/1/4 21:56
*/
@EqualsAndHashCode(callSuper = true)
@Data
public class WeSensitiveHitQuery extends BaseEntity {
/**
* 1:组织机构id,2:成员id
*/
@ApiModelProperty(value = "审计范围类型, 1 组织机构 2 成员")
private Integer scopeType;
/**
* 审计对象id
*/
@ApiModelProperty(value = "审计范围id")
private String auditScopeId;
/**
* 关键词
*/
@ApiModelProperty(value = "关键词")
private String keyword;
}
package com.linkwechat.wecom.service; package com.linkwechat.wecom.service;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.linkwechat.wecom.domain.WeSensitive; import com.linkwechat.wecom.domain.WeSensitive;
import com.linkwechat.wecom.domain.query.WeSensitiveHitQuery;
import java.util.List; import java.util.List;
...@@ -58,4 +61,13 @@ public interface IWeSensitiveService { ...@@ -58,4 +61,13 @@ public interface IWeSensitiveService {
* @return 结果 * @return 结果
*/ */
public int destroyWeSensitiveByIds(Long[] ids); public int destroyWeSensitiveByIds(Long[] ids);
/**
* 敏感词命中
*
* @param entityList
*/
public void hitSensitive(List<JSONObject> entityList);
public PageInfo<JSONObject> getHitSensitiveList(WeSensitiveHitQuery weSensitiveHitQuery);
} }
package com.linkwechat.wecom.service.impl; package com.linkwechat.wecom.service.impl;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Lists;
import com.linkwechat.common.constant.WeConstans;
import com.linkwechat.common.core.domain.elastic.ElasticSearchEntity;
import com.linkwechat.common.core.elasticsearch.ElasticSearch;
import com.linkwechat.common.core.page.PageDomain;
import com.linkwechat.common.core.page.TableSupport;
import com.linkwechat.common.utils.DateUtils; import com.linkwechat.common.utils.DateUtils;
import com.linkwechat.common.utils.SecurityUtils; import com.linkwechat.common.utils.SecurityUtils;
import com.linkwechat.common.utils.StringUtils;
import com.linkwechat.wecom.domain.WeSensitive; import com.linkwechat.wecom.domain.WeSensitive;
import com.linkwechat.wecom.domain.WeSensitiveAuditScope;
import com.linkwechat.wecom.domain.WeUser;
import com.linkwechat.wecom.domain.query.WeSensitiveHitQuery;
import com.linkwechat.wecom.mapper.WeSensitiveMapper; import com.linkwechat.wecom.mapper.WeSensitiveMapper;
import com.linkwechat.wecom.service.IWeSensitiveAuditScopeService; import com.linkwechat.wecom.service.IWeSensitiveAuditScopeService;
import com.linkwechat.wecom.service.IWeSensitiveService; import com.linkwechat.wecom.service.IWeSensitiveService;
import com.linkwechat.wecom.service.IWeUserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.io.IOException;
import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
/** /**
* 敏感词设置Service业务层处理 * 敏感词设置Service业务层处理
...@@ -20,6 +43,7 @@ import java.util.List; ...@@ -20,6 +43,7 @@ import java.util.List;
* @date 2020-12-29 * @date 2020-12-29
*/ */
@Service @Service
@Slf4j
public class WeSensitiveServiceImpl implements IWeSensitiveService { public class WeSensitiveServiceImpl implements IWeSensitiveService {
@Autowired @Autowired
private WeSensitiveMapper weSensitiveMapper; private WeSensitiveMapper weSensitiveMapper;
...@@ -27,6 +51,12 @@ public class WeSensitiveServiceImpl implements IWeSensitiveService { ...@@ -27,6 +51,12 @@ public class WeSensitiveServiceImpl implements IWeSensitiveService {
@Autowired @Autowired
private IWeSensitiveAuditScopeService sensitiveAuditScopeService; private IWeSensitiveAuditScopeService sensitiveAuditScopeService;
@Autowired
private ElasticSearch elasticSearch;
@Autowired
private IWeUserService weUserService;
/** /**
* 查询敏感词设置 * 查询敏感词设置
* *
...@@ -132,4 +162,112 @@ public class WeSensitiveServiceImpl implements IWeSensitiveService { ...@@ -132,4 +162,112 @@ public class WeSensitiveServiceImpl implements IWeSensitiveService {
} }
return deleteResult; return deleteResult;
} }
@Override
public void hitSensitive(List<JSONObject> entityList) {
//获取所有的敏感词规则
List<WeSensitive> allSensitiveRules = weSensitiveMapper.selectWeSensitiveList(new WeSensitive());
//根据规则过滤命中
if (CollectionUtils.isNotEmpty(allSensitiveRules)) {
allSensitiveRules.parallelStream().forEach(weSensitive -> {
List<JSONObject> jsonList = Lists.newArrayList();
List<String> patternWords = Arrays.asList(weSensitive.getPatternWords().split(","));
List<String> users = getScopeUsers(weSensitive.getAuditUserScope());
jsonList.addAll(hitSensitiveInES(patternWords, users));
//将命中结果插入es
try {
addHitSensitiveList(jsonList, weSensitive);
} catch (IOException e) {
log.warn("添加敏感词命中信息失败, jsonList={}, auditUserId={}, exception={}",
jsonList, weSensitive.getAuditUserId(), ExceptionUtils.getStackTrace(e));
}
});
}
}
@Override
public PageInfo<JSONObject> getHitSensitiveList(WeSensitiveHitQuery weSensitiveHitQuery) {
List<String> userIds = Lists.newArrayList();
if (weSensitiveHitQuery.getScopeType().equals(WeConstans.USE_SCOP_BUSINESSID_TYPE_USER)) {
userIds.add(weSensitiveHitQuery.getAuditScopeId());
} else {
List<String> userIdList = weUserService.selectWeUserList(WeUser.builder().department(new String[]{weSensitiveHitQuery.getAuditScopeId()}).build())
.stream().filter(Objects::nonNull).map(WeUser::getUserId).collect(Collectors.toList());
userIds.addAll(userIdList);
}
PageDomain pageDomain = TableSupport.buildPageRequest();
Integer pageNum = pageDomain.getPageNum() == null ? 1 : pageDomain.getPageNum();
Integer pageSize = pageDomain.getPageSize() == null ? 10 : pageDomain.getPageSize();
SearchSourceBuilder builder = new SearchSourceBuilder();
int from = (pageNum - 1) * pageSize;
builder.size(pageSize);
builder.from(from);
builder.sort("msgtime", SortOrder.DESC);
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
BoolQueryBuilder userBuilder = QueryBuilders.boolQuery();
userIds.forEach(user -> {
userBuilder.should(QueryBuilders.termQuery("from.keyword", user));
});
userBuilder.minimumShouldMatch(1);
boolQueryBuilder.must(userBuilder);
if (StringUtils.isNotBlank(weSensitiveHitQuery.getKeyword())) {
BoolQueryBuilder keywordBuilder = QueryBuilders.boolQuery().should(QueryBuilders.matchPhraseQuery("text.content", weSensitiveHitQuery.getKeyword()));
boolQueryBuilder.must(keywordBuilder);
}
builder.query(boolQueryBuilder);
return elasticSearch.searchPage(WeConstans.WECOM_SENSITIVE_HIT_INDEX, builder, pageNum, pageSize, JSONObject.class);
}
private void addHitSensitiveList(List<JSONObject> json, WeSensitive weSensitive) throws IOException {
//创建索引
elasticSearch.createIndex2(WeConstans.WECOM_SENSITIVE_HIT_INDEX, elasticSearch.getFinanceMapping());
if (weSensitive.getAlertFlag().equals(1)) {
//针对每一条命中信息发送消息通知给相应的审计人 TODO
}
//批量提交插入记录
if (CollectionUtils.isNotEmpty(json)) {
List<ElasticSearchEntity> list = json.stream().filter(Objects::nonNull).map(j -> {
ElasticSearchEntity ese = new ElasticSearchEntity();
ese.setData(j);
ese.setId(j.getString("msgid"));
return ese;
}).collect(Collectors.toList());
elasticSearch.insertBatch(WeConstans.WECOM_SENSITIVE_HIT_INDEX, list);
}
}
private List<String> getScopeUsers(List<WeSensitiveAuditScope> scopeList) {
List<String> users = Lists.newArrayList();
scopeList.forEach(scope -> {
if (scope.getScopeType().equals(WeConstans.USE_SCOP_BUSINESSID_TYPE_USER)) {
users.add(scope.getAuditScopeId());
} else {
List<String> userIdList = weUserService.selectWeUserList(WeUser.builder().department(new String[]{scope.getAuditScopeId()}).build())
.stream().filter(Objects::nonNull).map(WeUser::getUserId).collect(Collectors.toList());
users.addAll(userIdList);
}
});
return users;
}
private List<JSONObject> hitSensitiveInES(List<String> patternWords, List<String> users) {
//TODO user过大时进行分组处理
SearchSourceBuilder builder = new SearchSourceBuilder();
builder.sort("msgtime", SortOrder.DESC);
BoolQueryBuilder patterWordsBuilder = QueryBuilders.boolQuery();
patternWords.forEach(word -> {
patterWordsBuilder.should(QueryBuilders.matchPhraseQuery("text.content", word));
});
patterWordsBuilder.minimumShouldMatch(1);
BoolQueryBuilder userBuilder = QueryBuilders.boolQuery();
users.forEach(user -> {
userBuilder.should(QueryBuilders.termQuery("from.keyword", user));
});
userBuilder.minimumShouldMatch(1);
BoolQueryBuilder searchBuilder = QueryBuilders.boolQuery().must(patterWordsBuilder).must(userBuilder);
builder.query(searchBuilder);
return elasticSearch.search(WeConstans.WECOM_FINANCE_INDEX, builder, JSONObject.class);
}
} }
...@@ -42,11 +42,12 @@ ...@@ -42,11 +42,12 @@
<if test="patternWords != null and patternWords != ''">and a.pattern_words like concat('%', <if test="patternWords != null and patternWords != ''">and a.pattern_words like concat('%',
#{patternWords}, '%') #{patternWords}, '%')
</if> </if>
<if test="auditUserId != null ">and a.audit_user_id = #{auditUserId}</if> <if test="auditUserId != null and auditUserId != ''">and a.audit_user_id = #{auditUserId}</if>
<if test="auditUserName != null and auditUserName != ''">and a.audit_user_name like concat('%', <if test="auditUserName != null and auditUserName != ''">and a.audit_user_name like concat('%',
#{auditUserName}, '%') #{auditUserName}, '%')
</if> </if>
</where> </where>
order by a.update_time desc, a.create_time desc
</select> </select>
<select id="selectWeSensitiveById" parameterType="Long" resultMap="WeSensitiveResult"> <select id="selectWeSensitiveById" parameterType="Long" resultMap="WeSensitiveResult">
...@@ -60,6 +61,7 @@ ...@@ -60,6 +61,7 @@
<foreach item="id" collection="array" open="(" separator="," close=")"> <foreach item="id" collection="array" open="(" separator="," close=")">
#{id} #{id}
</foreach> </foreach>
order by a.update_time desc, a.create_time desc
</select> </select>
<insert id="insertWeSensitive" parameterType="WeSensitive" useGeneratedKeys="true" keyProperty="id"> <insert id="insertWeSensitive" parameterType="WeSensitive" useGeneratedKeys="true" keyProperty="id">
...@@ -67,7 +69,7 @@ ...@@ -67,7 +69,7 @@
<trim prefix="(" suffix=")" suffixOverrides=","> <trim prefix="(" suffix=")" suffixOverrides=",">
<if test="strategyName != null and strategyName != ''">strategy_name,</if> <if test="strategyName != null and strategyName != ''">strategy_name,</if>
<if test="patternWords != null and patternWords != ''">pattern_words,</if> <if test="patternWords != null and patternWords != ''">pattern_words,</if>
<if test="auditUserId != null">audit_user_id,</if> <if test="auditUserId != null and auditUserId != ''">audit_user_id,</if>
<if test="auditUserName != null and auditUserName != ''">audit_user_name,</if> <if test="auditUserName != null and auditUserName != ''">audit_user_name,</if>
<if test="alertFlag != null">alert_flag,</if> <if test="alertFlag != null">alert_flag,</if>
<if test="delFlag != null">del_flag,</if> <if test="delFlag != null">del_flag,</if>
...@@ -79,7 +81,7 @@ ...@@ -79,7 +81,7 @@
<trim prefix="values (" suffix=")" suffixOverrides=","> <trim prefix="values (" suffix=")" suffixOverrides=",">
<if test="strategyName != null and strategyName != ''">#{strategyName},</if> <if test="strategyName != null and strategyName != ''">#{strategyName},</if>
<if test="patternWords != null and patternWords != ''">#{patternWords},</if> <if test="patternWords != null and patternWords != ''">#{patternWords},</if>
<if test="auditUserId != null">#{auditUserId},</if> <if test="auditUserId != null and auditUserId != ''">#{auditUserId},</if>
<if test="auditUserName != null and auditUserName != ''">#{auditUserName},</if> <if test="auditUserName != null and auditUserName != ''">#{auditUserName},</if>
<if test="alertFlag != null">#{alertFlag},</if> <if test="alertFlag != null">#{alertFlag},</if>
<if test="delFlag != null">#{delFlag},</if> <if test="delFlag != null">#{delFlag},</if>
...@@ -95,7 +97,7 @@ ...@@ -95,7 +97,7 @@
<trim prefix="SET" suffixOverrides=","> <trim prefix="SET" suffixOverrides=",">
<if test="strategyName != null and strategyName != ''">strategy_name = #{strategyName},</if> <if test="strategyName != null and strategyName != ''">strategy_name = #{strategyName},</if>
<if test="patternWords != null and patternWords != ''">pattern_words = #{patternWords},</if> <if test="patternWords != null and patternWords != ''">pattern_words = #{patternWords},</if>
<if test="auditUserId != null">audit_user_id = #{auditUserId},</if> <if test="auditUserId != null and auditUserId != ''">audit_user_id = #{auditUserId},</if>
<if test="auditUserName != null">audit_user_name = #{auditUserName},</if> <if test="auditUserName != null">audit_user_name = #{auditUserName},</if>
<if test="alertFlag != null">alert_flag = #{alertFlag},</if> <if test="alertFlag != null">alert_flag = #{alertFlag},</if>
<if test="delFlag != null">del_flag = #{delFlag},</if> <if test="delFlag != null">del_flag = #{delFlag},</if>
...@@ -126,7 +128,7 @@ ...@@ -126,7 +128,7 @@
</trim> </trim>
<trim prefix="audit_user_id = case" suffix="end,"> <trim prefix="audit_user_id = case" suffix="end,">
<foreach collection="list" item="item" index="index"> <foreach collection="list" item="item" index="index">
<if test="item.auditUserId != null"> <if test="item.auditUserId != null and auditUserId != ''">
when id = #{item.id} then #{item.auditUserId} when id = #{item.id} then #{item.auditUserId}
</if> </if>
</foreach> </foreach>
......
...@@ -6311,3 +6311,35 @@ CREATE TABLE `we_user` ( ...@@ -6311,3 +6311,35 @@ CREATE TABLE `we_user` (
-- ---------------------------- -- ----------------------------
-- Records of we_user -- Records of we_user
-- ---------------------------- -- ----------------------------
-- ----------------------------
-- Table structure for we_sensitive
-- ----------------------------
DROP TABLE IF EXISTS `we_sensitive`;
CREATE TABLE `we_sensitive` (
id BIGINT auto_increment NOT NULL COMMENT '主键',
strategy_name varchar(100) NOT NULL COMMENT '策略名称',
pattern_words TEXT NOT NULL COMMENT '匹配词',
audit_user_id varchar(64) NOT NULL COMMENT '审计人id',
audit_user_name varchar(30) NOT NULL COMMENT '审计人',
alert_flag TINYINT DEFAULT 1 NOT NULL COMMENT '消息通知,1 开启 0 关闭',
del_flag TINYINT DEFAULT 0 NOT NULL COMMENT '删除标识,1 已删除 0 未删除',
create_by varchar(64) NULL COMMENT '创建人',
create_time DATETIME NULL COMMENT '创建时间',
update_by varchar(64) NULL COMMENT '更新人',
update_time DATETIME NULL COMMENT '更新时间',
CONSTRAINT we_sensitive_pk PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='敏感词设置表';
-- ----------------------------
-- Table structure for we_sensitive_audit_scope
-- ----------------------------
DROP TABLE IF EXISTS `we_sensitive_audit_scope`;
CREATE TABLE `we_sensitive_audit_scope` (
id BIGINT auto_increment NOT NULL COMMENT '主键',
sensitive_id BIGINT NOT NULL COMMENT '敏感词表主键',
scope_type TINYINT NOT NULL COMMENT '审计范围类型, 1 组织机构 2 成员',
audit_scope_id varchar(64) NOT NULL COMMENT '审计对象id',
audit_scope_name varchar(64) NOT NULL COMMENT '审计对象名称',
CONSTRAINT we_sensitive_audit_scope_pk PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=DYNAMIC COMMENT='敏感词审计范围';
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!