java关键字敏感字实现过滤替换

package com.cbpk.util;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;

import com.cbpk.beans.KeyWord;
import com.cbpk.dao.KeyWordDao;

public class KeyWordFilter implements Filter {
 
 public KeyWordDao kwDao;
 public List list;

 public KeyWordDao getKwDao() {
  return kwDao;
 }
 @Resource(name="keyWordDaoImpl")
 public void setKwDao(KeyWordDao kwDao) {
  this.kwDao = kwDao;
 }

 public List getList() {
  return list;
 }

 public void setList(List list) {
  this.list = list;
 }

 public void destroy() {
  // TODO Auto-generated method stub

 }

 public void doFilter(ServletRequest request, ServletResponse response,
  FilterChain chain) throws IOException, ServletException {
  
  list=kwDao.getKeyWord();
  HttpServletRequest myRequest=(HttpServletRequest)request;
  
  chain.doFilter(new KeyWordRequestWrapper(myRequest,list), response);
 }

 public void init(FilterConfig filterConfig) throws ServletException {
  
 }

}

 

 

自定义HttpServletRequestWrapper类

 

 

package com.cbpk.util;

import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

import com.cbpk.beans.KeyWord;

public class KeyWordRequestWrapper extends HttpServletRequestWrapper {

 private List list;
 public KeyWordRequestWrapper(HttpServletRequest request,List list) {
  super(request);
  this.list=list;
 }

 @Override
 public Map getParameterMap() {
  
  Map map=super.getParameterMap();
  
  Iterator itorator=map.keySet().iterator();
  String[] value;
  while(itorator.hasNext())
  {
   value=map.get(itorator.next());
   for(KeyWord kword:list)
   {
    for(int i=0;i     {
     if(value[i].contains(kword.getValue()))
     {
      value[i]=value[i].replace(kword.getValue(), "");
     }
    }
   }
  }
  
  return map;
 }

 @Override
 public String getParameter(String name) {
  // TODO Auto-generated method stub
  return super.getParameter(name);
 }

 @Override
 public Enumeration getParameterNames() {
  // TODO Auto-generated method stub
  return super.getParameterNames();
 }
 
 
 
}

 

 

你可能感兴趣的:(java关键字敏感字实现过滤替换)