logback身份证脱敏
学习了:https://shift-alt-ctrl.iteye.com/blog/2425469
https://blog.csdn.net/fywfengyanwei/article/details/78484590
java:
package com.util; import ch.qos.logback.classic.pattern.MessageConverter; import ch.qos.logback.classic.spi.ILoggingEvent; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ComplexMessageConverter extends MessageConverter { @Override public String convert(ILoggingEvent event) { String source = event.getFormattedMessage(); if (source == null || source.isEmpty()) { return source; } // 身份证号码脱敏 String card = "([1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx])|" + "(^[1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3})"; Pattern p = Pattern.compile(card); Matcher matcher = p.matcher(source); source = matcher.replaceAll("idCardNo"); return source; } }
logback.xml:
<conversionRule conversionWord="m" converterClass="com.util.ComplexMessageConverter"></conversionRule>
脱敏优化:
public static void main(String[] args) { // 身份证号码脱敏 // 原始的正则表达式 String card = "([1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx])|" + "([1-9]\d{5}\d{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)\d{3})"; card = "(([1-9]\d{5}(18|19|20)\d{2})(((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31))(\d{3}[0-9Xx]))"; // 18位身份证 String str = "身份证120105199808092345,进行脱密;身份证120105199808092345,进行脱敏2;"; // 两个也是可以的 String s = str.replaceAll(card, "$1*$2*$3*$4*$5*$6*$7*$8*$9*$10*$11"); // 中间过程 System.out.println(s); String s1 = str.replaceAll(card, "$2****$10"); System.out.println(s1); card = "(([1-9]\d{5}\d{2}((0[1-9])|(10|11|12)))((([0-2][1-9])|(10)|20|30|31)\d{2})(\d))"; //15位身份证 str = "身份证130105191208092,进行脱密"; s = str.replaceAll(card, "$1*$2*$3*$4*$5*$6*$7*$8*$9*$10"); // 中间过程 System.out.println(s); s1 = str.replaceAll(card, "$2****$10"); System.out.println(s1); // 汇总一下,汇总之后的写法不统一; card = "(([1-9]\d{5}(18|19|20)\d{2})(((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31))(\d{3}[0-9Xx]))|"+ "(([1-9]\d{5}\d{2}((0[1-9])|(10|11|12)))((([0-2][1-9])|(10)|20|30|31)\d{2})(\d))"; str = "身份证130105191208092,进行脱密"; s = str.replaceAll(card, "$1*$2*$3*$4*$5*$6*$7*$8*$9*$10*$11*$12*$13*$14"); // 中间过程 System.out.println(s); s1 = str.replaceAll(card, "$2****$10"); System.out.println(s1); }