SL4J 配置更改或正则表达式以在 Json 中屏蔽电子邮件(由 '@' 和 '.com' 识别)

SL4J configuration change or regex to mask email(recognized by '@' and '.com' ) in a Json

我想在作为安全协议的一部分登录时从 JSON 中屏蔽 Java 中的电子邮件地址。 我已经尝试过使用字符串操作替换的原始方法,但我无法搜索整个项目。 SL4J中有没有可能的配置或者正则表达式是唯一的出路

如果下面是log(slf4j)打印的响应:

{
  "any_key": {

    "salt": "salt_here",
    "sel": "sel_here",
    "rules": [
      {
        "variation": 0,
        "clauses": [
          {
            "attribute": "email",
            "op": "in",
            "values": [
              "jack.jones@gmail.com",
              "david.beckham@yahoo.com"
            ],
            "negate": false
          }
        ]
      }
    ]
  }
}

它应该在日志中打印 json 如下:

{
    "any_key": {
    "salt": "salt_here",
    "sel": "sel_here",
    "rules": [
      {
        "variation": 0,
        "clauses": [
          {
            "attribute": "email",
            "op": "in",
            "values": [
              "jac*******@******com",
              "dav**********@******com"
            ],
            "negate": false
          }
        ]
      }
    ]
  }
}
(?<=[\"|\']\p{L}{3}).*\@.*(?=com)

此正则表达式将检测 \"\p{L}{3}(“后跟三个字母)和 com 之间的任何内容。然后您可以迭代匹配并将每个字符替换为 *(@ 除外).

编辑:

要包含 'something@xyz.com' (som******@****com),请使用更新后的正则表达式,现在允许 \"\' 作为分隔符。

https://regex101.com/r/qX1MjR/2

我能够以编程方式解决它,如下所示:

public class Test {

    public static void main(String a[]){
    String result =mask("{\"id\":\"jack.sparrow@pirate.com\"}"); 

    System.out.println(result);

    }
    final static char ASTERICK = '*';
    final static char DOT = '.';
    final static char QUOTATION_MARK = '"';
    final static char ATMARK = '@';


    public static String mask(String jsonString) {
        try {
            if(false)
                return maskEmail(jsonString);

            Matcher m = Pattern.compile("(?=(@))").matcher(jsonString);
            List<Integer> positionList = new ArrayList<Integer>();
            while (m.find()) {
                positionList.add(m.start());
            }
            for (int index = 0; index < positionList.size(); index++) {
                String semiFinalString = maskEmailbeforeAtmark(jsonString,
                        positionList.get(index),ASTERICK);
                String finalString = maskEmailAfterAtmark(semiFinalString,
                        positionList.get(index),ASTERICK);
                jsonString = new String(finalString);
            }
            return jsonString;
        }catch(Exception e) {
            return jsonString;
        }
    }



    /**
     * This method will stark masking before the Atmark(`@`) identifier.
     *
     * @param str
     * @return masked json String.
     * 
     */
    private static String maskEmailbeforeAtmark(String str, int index,char replacementCharacter) throws Exception{
        StringBuffer buff = new StringBuffer(str);
        char[] chars = str.toCharArray();
        loop: for (int i = index - 1; i >= 0; i--) {

            if (chars[i] == QUOTATION_MARK) {
                buff.replace(i+1, i + 2, Character.toString(chars[i + 1]));
                buff.replace(i + 2, i + 3, Character.toString(chars[i + 2]));
                buff.replace(i + 3, i + 4, Character.toString(chars[i + 3]));

                break loop;
            } else {
                buff.replace(i, i + 1, Character.toString(replacementCharacter));
            }
        }
        return buff.toString();
    }

    /**
     * This method will stark masking after the Atmark(`@`) identifier.
     *
     * @param str
     * @return masked json String.
     * 
     */
    private static String maskEmailAfterAtmark(String str, int index,char replacementCharacter) throws Exception{
        StringBuffer buff = new StringBuffer(str);
        char[] chars = str.toCharArray();
        loop: for (int i = index + 1; i <= str.length(); i++) {
            if (chars[i] == DOT)
                break loop;
            else
                buff.replace(i, i + 1, Character.toString(replacementCharacter));
        }
        return buff.toString();
    }

    /**
     * This method mask a part of the email string.
     *
     * @param email
     * @return number
     * 
     */

    private static boolean isJSONValid(String target) {
        try {
            new JSONObject(target);
        } catch (JSONException ex) {
            try {
                new JSONArray(target);
            } catch (JSONException ex1) {
                return false;
            }
        }
        return true;
    }

    private static String maskEmail(String email) throws Exception {

            int start = 0;
            int end = email.length();
            if (start > end)
                return email;

            String semiMaskedString = maskEmailbeforeAtmark("\""+email,email.indexOf(ATMARK)+1,ASTERICK);
            String editedString= semiMaskedString.substring(1, semiMaskedString.length());
            String finalString = maskEmailAfterAtmark(editedString,editedString.indexOf(ATMARK),ASTERICK);
            return finalString;
    }

}