1. 程式人生 > >java filter 返回值固定利用Gson解析脫敏手機號、證件號等

java filter 返回值固定利用Gson解析脫敏手機號、證件號等

filter 配置看上一篇 返回值格式不固定情況脫敏手機號等

 @Override
    public void doFilter(ServletRequest req, ServletResponse response, FilterChain filterChain)
            throws IOException, ServletException
    {
        HttpServletRequest request = (HttpServletRequest)req;
        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");
            StringBuilder sb = new StringBuilder(str);
            boolean b = false;remoteAuthService.checkDataAccess("", "", "");
            try
            {
                if (!b) {
                    Gson gson = new Gson();
                    Message message = gson.fromJson(str, Message.class);
                    Map data = (Map)message.getData();
                    Map pageInfo = (Map) data.get("pageInfo");
                    List list = (List) pageInfo.get("list");
                    //對實體進行脫敏
                    desensitizationObject(list);
                    str = gson.toJson(message);
                    
                }


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

    }

//簡單的脫敏方法 gson轉換的都為map實體 想要修改哪個屬性直接map.get(屬性名字就好)

 public void desensitization(List<Map> list) {

        for (Map map:list) {
            Field f1 = null;
            try {
                //f1 = o.getClass().getDeclaredField("idCardNo");
               // f1.setAccessible(true);
                String str ="";
                String idCardNo = (String) map.get("idCardNo");
                if (idCardNo.length()>=10){
                    String pre = idCardNo.substring(0, 3);
                    str+=pre;
                    String tail = idCardNo.substring(idCardNo.length() - 4, idCardNo.length());
                    for (int i = 3;i<idCardNo.length()-4;i++){
                        str+="*";
                    }
                    str+=tail;
                }else{
                    String pre = idCardNo.substring(0, 1);
                    str+=pre;
                    String tail = idCardNo.substring(idCardNo.length() - 1, idCardNo.length());
                    for (int i = 1;i<idCardNo.length()-1;i++){
                        str+="*";
                    }
                    str+=tail;
                }
                map.put("idCardNo",str);
                //修改這個欄位的值
                //f1.set(o,str);
            } catch (Exception e) {

            }
            try {

               // Field f2 = o.getClass().getDeclaredField("contactInfo");
                //嘗試獲取欄位中的值

               // f2.setAccessible(true);
                String contactInfo = (String) map.get("contactInfo");
                String newContactInfo = contactInfo.substring(0, 3);
                for (int j=3;j<contactInfo.length();j++){
                    newContactInfo+="*";
                }
                //f2.set(o,newContactInfo);
                map.put("contactInfo",newContactInfo);
                //檢視這個欄位的值

            } catch (Exception e) {
                //e.printStackTrace();
            }

        }

    }

用Gson拿到需要修改的那個map實體 修改後返回前端