過濾敏感詞匯類
java/* * SiteAction.java * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Library General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Author: Winter Lau * http://dlog4j.sourceforge.net */ package com.liusoft.dlog4j; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import javax.servlet.ServletContext; import org.apache.commons.lang.StringUtils; /** * DLOG在安全方面的一些處理方法 * 敏感詞匯表:/WEB-INF/conf/illegal_glossary.dat * * @author Winter Lau */ public class DLOGSecurityManager { /** * 初始化 * @param sc * @throws IOException * * @see com.liusoft.dlog4j.servlet.DLOG_ActionServlet#init() */ public static void init(ServletContext sc) throws IOException { IllegalGlossary.init(sc); } public static void destroy(){ IllegalGlossary.destroy(); } /** * 敏感字匯 * @author Winter Lau */ public static class IllegalGlossary { private final static String file_glossary = "/WEB-INF/conf/illegal_glossary.dat"; private static List glossary = null; public static void init(ServletContext sc) throws IOException { glossary = new ArrayList(1000); if(sc!=null) loadIllegalGlossary(sc); } public static void destroy(){ if(glossary!=null) glossary.clear(); } /** * 加載敏感詞匯表 * @param sc * @throws IOException */ private synchronized static void loadIllegalGlossary(ServletContext sc) throws IOException { InputStream in = sc.getResourceAsStream(file_glossary); BufferedReader reader = null; try{ reader = new BufferedReader(new InputStreamReader(in)); do{ String line = reader.readLine(); if(line==null) break; glossary.add(line.trim()); }while(true); }finally{ in.close(); } } /** * 自動將敏感詞匯用XXX替換 * * @param content * @return */ public static String autoGlossaryFiltrate(String content) { if(StringUtils.isEmpty(content)) return content; for (int i = 0; i < glossary.size(); i++) { String word = (String)glossary.get(i); content = StringUtils.replace(content, word, StringUtils .repeat("X", word.length())); } return content; } /** * 判斷是否存在非法內容 * @param content * @return */ public static boolean existIllegalWord(String content){ if(StringUtils.isEmpty(content)) return false; for (int i = 0; i < glossary.size(); i++) { String word = (String) glossary.get(i); if(content.indexOf(word)>=0) return true; } return false; } /** * 刪除內容中存在的關鍵字 * @param content * @return */ public static String deleteIllegalWord(String content){ if(StringUtils.isEmpty(content)) return content; for (int i = 0; i < glossary.size(); i++) { String word = (String) glossary.get(i); content = StringUtils.remove(content, word); } return content; } } public static void main(String[] args) throws IOException{ init(null); String text = "中華人民共和國國家主席毛澤東,我們叫他毛主席"; System.out.println(IllegalGlossary.autoGlossaryFiltrate(text)); } }
這個類,是從DLOG4J上學到的。
posted on 2011-03-31 08:16 jack zhai 閱讀(839) 評論(3) 編輯 收藏 所屬分類: java web