java filter脱敏手机号证件号等

filter springboot 配置  西门吹水_的文章:  java使用Filter过滤器对Response返回值进行修改

 @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
        throws IOException, ServletException
    {
        ResponseWrapper wrapperResponse = new ResponseWrapper((HttpServletResponse)response);//转换成代理类
        // 这里只拦截返回,直接让请求过去,如果在请求前有处理,可以在这里处理
        filterChain.doFilter(request, wrapperResponse);
        byte[] content = wrapperResponse.getContent();//获取返回值
	//判断是否有值
        if (content.length > 0)
        {
 
            String str = new String(content, "UTF-8");
            System.out.println("返回值:" + str);
            StringBuilder sb= new StringBuilder(str);
 
            try
            {
               for (int i = 0;i<str.length()-23;i++){
                        Long phone = null;
                        Long cardId = null;
                        //身份证脱敏 
                        String substring1 = str.substring(i, i+7);
                           //匹配json中身份证属性名称
                        if (substring1.equals("cardNo\":")) {
                            try {
                                String substring2 = str.substring(i + 8, i + 25);
                                cardId = Long.parseLong(substring2);

                                String s1 = cardId.toString();
                                String replace1 = s1.replace(s1.substring(3, 14), "***********");
                                sb.replace(i+8, i + 25, replace1);
                                i+=26;
                            } catch (NumberFormatException e) {
                                String substring2 = str.substring(i + 8, i + 22);
                                try {
                                    cardId = Long.parseLong(substring2);
                                    String s1 = cardId.toString();

                                    String replace1 = s1.replace(s1.substring(3, 11), "********");
                                    sb.replace(i+8, i + 22, replace1);
                                    i+=22;
                                } catch (NumberFormatException e1) {
                                    System.out.println("匹配身份证失败");
                                }
                            }
                        }
                        //手机号脱敏
                        String substring2 = str.substring(i, i+10);
                        if (substring2.equals("phoneNo\":")) {
                            try {
                                String phoneString = str.substring(i + 11, i + 22);
                                phone = Long.parseLong(phoneString);

                                String s1 = phone.toString();
                                String replace1 = s1.replace(s1.substring(3, 7), "****");
                                sb.replace(i+11, i + 22, replace1);
                                i+=22;
                            } catch (NumberFormatException e) {
                                System.out.println("不是手机号,不需要脱敏");
                            }
                        }


                    }
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
		//把返回值输出到客户端
            ServletOutputStream out = response.getOutputStream();
            out.write(sb.toString.getBytes());
            out.flush();
        }
 
    }

这种方法,返回值类型结构不确定时能较好的拦截敏感信息。但是循环会意向性能 不建议使用

猜你喜欢

转载自blog.csdn.net/weixin_42012335/article/details/85335079