敏感字符过滤器

注意:
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;
  }
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

欧文晶
关注

0

0

0


javaWeb实现的过滤器敏感字过滤
03-17
javaWeb实现的过滤器敏感字过滤,建一个敏感字符的文件,发送时实现过滤为***
————————————————
版权声明:本文为CSDN博主「欧文晶」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42017951/article/details/88564102

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值