javaweb敏感字符过滤

注意:
1、过滤器慎用日志、中文乱码、敏感字符
2、web对过滤器、监听和servlet的调用顺序
先用监听-----过滤器—再调用servlet
敏感字符过滤的两种方法:
1、把敏感字符放入到一个文件,从文件中读取这些字符,与请求对象中字符进行比较,如果包含有铭感字符,一般用某种符号替换
2、把敏感字符放入到数据库中,从数据库的表中读取这些字符。
过滤器的作用:拦截request对象,将请求传递到request的包装类中
效果图:在这里插入图片描述

首先创建一个txt文本,然后在filter过滤器中进行过滤。
提示:只放了重要代码,自动生成的代码就没有放入了。

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
 throws IOException, ServletException {
  HttpServletRequest sp=(HttpServletRequest)request;
  HttpServletResponse sResponse=(HttpServletResponse)response;
  //请求的包装,过滤器的作用:拦截request对象,将请求传递到request的包装
  sp.setCharacterEncoding("UTF-8");
  noderequest no=new noderequest(sp);
  chain.doFilter(no, response);
 }


 class noderequest extends HttpServletRequestWrapper{
  HttpServletRequest request;
  public noderequest(HttpServletRequest request) {
   super(request);
   this.request=request;
   // TODO Auto-generated constructor stub
  }
  @Override
  public String getParameter(String name) {
  //获得留言内容
   String nodestr=request.getParameter(name);
   //获得敏感词库
   List<String> lists=wordutil.getWords();
   //进行比对
   //取每一个
   //当读取到敏感字符用*号代替
   for(String str:lists) {
    nodestr=nodestr.replaceAll(str, "*");
   }
   return nodestr;
  }
  }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值