1.项目 后台接口
1.1 Controller
@Controller
@RequestMapping(value = "/admin/word")
public class SensitiveWordController {
public static Log log = LogFactory.getLog(SensitiveWordController.class);
@Autowired
private ISensitiveWordService sensitiveWordService;
/**
* 敏感词过滤
* @param text
* @return
*/
@RequestMapping(value = "/filter")
@ResponseBody
public ResResult sensitiveWordFiltering(String text){
ResResult res = new ResResult();
try {
SensitiveWordVO vo = sensitiveWordService.sensitiveWordFiltering(text);
if(vo.getWordSize() == 0){
res.setCode(1);
res.setMsg("无敏感词");
res.setRe(new ArrayList());
}else{
res.setCode(-1);
res.setMsg("发现敏感词");
res.setRe(vo);
}
} catch (Exception e) {
//throw new RoxException("过滤敏感词出错,请联系维护人员");
res.setCode(-1);
res.setMsg("敏感词过滤失敗");
res.setRe(new ArrayList());
}
return res;
}
/**
* 敏感词:跳转到列表页面
*
*/
@RequestMapping(value="/toDataWord")
public String toDataNews(NewsSearchForm form) throws Exception{
return "/backstage/word/word";
}
/**
* 敏感词:分页查询
* @param form
* @return
* @throws Exception
*/
@RequestMapping(value="/dataWord")
@ResponseBody
public Map dataWord(SensitiveWordSearchForm form) throws Exception{
@SuppressWarnings("unchecked")
Page page= (Page) sensitiveWordService.dataWord(form);
@SuppressWarnings("unchecked")
List rows = (List) page.getData();
long total = page.getTotalCount();
Map map = new HashMap();
map.put("rows", rows);
map.put("total", total);
return map;
}
/**
* 敏感词:跳转到新增页面
*
*/
@RequestMapping(value="/toAddWord")
public String toAddWord() throws Exception{
return "/backstage/word/word_add";
}
/**
* 敏感词:新增
* @param Word
* @return
*/
@RequestMapping("/addWord")
@ResponseBody
public Message addWord(SensitiveWord sensitiveWord){
ResultBean rb = sensitiveWordService.addWord(sensitiveWord);
return new Message(rb.getCode()==1?true:false,rb.getMsg());
}
/**
* 敏感词:跳转到详情页面
*
*/
@RequestMapping(value="/toDetailsWord/{id}")
public ModelAndView toDetailsWord(@PathVariable(value="id") Long id) throws Exception{
ModelAndView view= new ModelAndView();
SensitiveWord sensitiveWord = sensitiveWordService.selectByKeyId(id);
view.addObject("vo", sensitiveWord);
view.setViewName("/backstage/word/word_edit");
return view;
}
/**
* 敏感词:修改
* @param news
* @return
*/
@RequestMapping("/editWord")
@ResponseBody
public Message editWord(SensitiveWord sensitiveWord){
ResultBean rb = sensitiveWordService.editWord(sensitiveWord);
return new Message(rb.getCode()==1?true:false,rb.getMsg());
}
/**
* 敏感词:删除
* @param news
* @return
*/
@RequestMapping("/delWord")
@ResponseBody
public Message delWord(IdsForm form){
ResultBean rb = sensitiveWordService.delWord(form);
return new Message(rb.getCode()==1?true:false,rb.getMsg());
}
}
1.2 Service Impl 实现类
@Service
public class SensitiveWordServiceImpl implements ISensitiveWordService {
@Autowired
private ISensitiveWordDao sensitiveWordDao;
@Override
public SensitiveWordVO sensitiveWordFiltering(String text) {
SensitiveWordVO vo = new SensitiveWordVO();
// TODO Auto-generated method stub
// 初始化敏感词库对象
SensitiveWordInit sensitiveWordInit = new SensitiveWordInit();
// 从数据库中获取敏感词对象集合(调用的方法来自Dao层,此方法是service层的实现类)
List sensitiveWords = sensitiveWordDao.getSensitiveWordListAll();
// 构建敏感词库
Map sensitiveWordMap = sensitiveWordInit.initKeyWord(sensitiveWords);
// 传入SensitivewordEngine类中的敏感词库
SensitivewordEngine.sensitiveWordMap = sensitiveWordMap;
// 得到敏感词有哪些,传入2表示获取所有敏感词
vo.setSet(SensitivewordEngine.getSensitiveWord(text, 2));
vo.setWordSize(SensitivewordEngine.checkSensitiveWord(text,0,text.length()));
return vo;
}
@Override
public Page dataWord(SensitiveWordSearchForm form) {
// TODO Auto-generated method stub
ResResult res = new ResResult();
SqlBean sql = new SqlBean();
StringBuffer condition = new StringBuffer();
if(form != null){
if(form.getWord() != null){
condition.append(" and word like '%" + form.getWord()+"%'");
}
}
sql.setCondition(condition.toString());
sql.setOrderby(" create_time desc ");
Page vo= sensitiveWordDao.dataWord(sql, form.getPageNO(), form.getPageSize());
return vo;
}
@Override
public ResultBean addWord(SensitiveWord sensitiveWord) {
// TODO Auto-generated method stub
if(sensitiveWord != null){
sensitiveWord.setCreateTime(new Date());
}
return sensitiveWordDao.insert(sensitiveWord);
}
@Override
public SensitiveWord selectByKeyId(Long id) {
// TODO Auto-generated method stub
return sensitiveWordDao.selectByKey(id);
}
@Override
public ResultBean editWord(SensitiveWord sensitiveWord) {
// TODO Auto-generated method stub
if(sensitiveWord != null){
sensitiveWord.setUpdateTime(new Date());
}
return sensitiveWordDao.updateByPrimaryKeySelective(sensitiveWord);
}
@Override
public ResultBean delWord(IdsForm form) {
// TODO Auto-generated method stub
ResultBean rb = new ResultBean();
if(form != null && form.getIdsArr().length>0){
for(int i = 0;i
1.3 SensitiveWordInit 敏感词库初始化
public class SensitiveWordInit {
// 敏感词库
@SuppressWarnings("rawtypes")
public HashMap sensitiveWordMap;
/**
* 初始化敏感词
*
* @param sensitiveWords
* @return
*/
@SuppressWarnings("rawtypes")
public Map initKeyWord(List sensitiveWords) {
try {
// 从敏感词集合对象中取出敏感词并封装到Set集合中
Set keyWordSet = new HashSet();
for (SensitiveWord s : sensitiveWords) {
keyWordSet.add(s.getWord().trim()); // s.getContent() 修改为 s.getWord()
}
// 将敏感词库加入到HashMap中
addSensitiveWordToHashMap(keyWordSet);
} catch (Exception e) {
e.printStackTrace();
}
return sensitiveWordMap;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private void addSensitiveWordToHashMap(Set keyWordSet) {
// 初始化HashMap对象并控制容器的大小
sensitiveWordMap = new HashMap(keyWordSet.size());
// 敏感词
String key = null;
// 用来按照相应的格式保存敏感词库数据
Map nowMap = null;
// 用来辅助构建敏感词库
Map newWorMap = null;
// 使用一个迭代器来循环敏感词集合
Iterator iterator = keyWordSet.iterator();
while (iterator.hasNext()) {
key = iterator.next();
// 等于敏感词库,HashMap对象在内存中占用的是同一个地址,所以此nowMap对象的变化,sensitiveWordMap对象也会跟着改变
nowMap = sensitiveWordMap;
for (int i = 0; i < key.length(); i++) {
// 截取敏感词当中的字,在敏感词库中字为HashMap对象的Key键值
char keyChar = key.charAt(i);
// 判断这个字是否存在于敏感词库中
Object wordMap = nowMap.get(keyChar);
if (wordMap != null) {
nowMap = (Map) wordMap;
} else {
newWorMap = new HashMap();
newWorMap.put("isEnd", "0");
nowMap.put(keyChar, newWorMap);
nowMap = newWorMap;
}
// 如果该字是当前敏感词的最后一个字,则标识为结尾字
if (i == key.length() - 1) {
nowMap.put("isEnd", "1");
}
System.out.println("封装敏感词库过程:" + sensitiveWordMap);
}
System.out.println("查看敏感词库数据:" + sensitiveWordMap);
}
}
}
1.3 SensitivewordEngine 敏感词过滤工具类
public class SensitivewordEngine {
// 敏感词库
@SuppressWarnings("rawtypes")
public static Map sensitiveWordMap = null;
// 只过滤最小敏感词
public static int minMatchTYpe = 1;
// 过滤所有敏感词
public static int maxMatchType = 2;
/**
* 敏感词库敏感词数量
*
* @return
*/
public static int getWordSize() {
if (SensitivewordEngine.sensitiveWordMap == null) {
return 0;
}
return SensitivewordEngine.sensitiveWordMap.size();
}
/**
* 是否包含敏感词
*
* @param txt
* @param matchType
* @return
*/
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) {
flag = true;
}
}
return flag;
}
/**
* 获取敏感词内容
*
* @param txt
* @param matchType
* @return
*/
public static Set getSensitiveWord(String txt, int matchType) {
Set sensitiveWordList = new HashSet();
for (int i = 0; i < txt.length(); i++) {
int length = checkSensitiveWord(txt, i, matchType);
if (length > 0) {
// 将检测出的敏感词保存到集合中
sensitiveWordList.add(txt.substring(i, i + length));
i = i + length - 1;
}
}
return sensitiveWordList;
}
/**
* 替换敏感词
*
* @param txt
* @param matchType
* @param replaceChar
* @return
*/
public static String replaceSensitiveWord(String txt, int matchType, String replaceChar) {
String resultTxt = txt;
Set set = getSensitiveWord(txt, matchType);
Iterator iterator = set.iterator();
String word = null;
String replaceString = null;
while (iterator.hasNext()) {
word = iterator.next();
replaceString = getReplaceChars(replaceChar, word.length());
resultTxt = resultTxt.replaceAll(word, replaceString);
}
return resultTxt;
}
/**
* 替换敏感词内容
*
* @param replaceChar
* @param length
* @return
*/
private static String getReplaceChars(String replaceChar, int length) {
String resultReplace = replaceChar;
for (int i = 1; i < length; i++) {
resultReplace += replaceChar;
}
return resultReplace;
}
/**
* 检查敏感词数量
*
* @param txt
* @param beginIndex
* @param matchType
* @return
*/
public static int checkSensitiveWord(String txt, int beginIndex, int matchType) {
boolean flag = false;
// 记录敏感词数量
int matchFlag = 0;
char word = 0;
Map nowMap = SensitivewordEngine.sensitiveWordMap;
for (int i = beginIndex; i < txt.length(); i++) {
word = txt.charAt(i);
// 判断该字是否存在于敏感词库中
nowMap = (Map) nowMap.get(word);
if (nowMap != null) {
matchFlag++;
// 判断是否是敏感词的结尾字,如果是结尾字则判断是否继续检测
if ("1".equals(nowMap.get("isEnd"))) {
flag = true;
// 判断过滤类型,如果是小过滤则跳出循环,否则继续循环
if (SensitivewordEngine.minMatchTYpe == matchType) {
break;
}
}
} else {
break;
}
}
if (!flag) {
matchFlag = 0;
}
return matchFlag;
}
}
1.4 测试结果
{
"code" : -1,
"msg" : "发现敏感词",
"re" : {
"set" : [//敏感词集
"王八蛋"
],
"wordSize" : 3 //敏感词数量(字数)
}
}