Springboot-Logback: 遮盖社保号(SSN)的最后四位数字

huangapple go评论119阅读模式
英文:

Springboot-Logback: Mask SSN last four Digits

问题

我正在尝试在我的Spring Boot应用程序中使用Logback.xml来屏蔽敏感信息,如社保号和信用卡信息。我在网上搜索了各种链接,但找不到好的解决方案。是否有一种简单的方法或任何库可以在日志中屏蔽敏感信息?

输入

{"firstName":"John","lastName":"Doe","SSN":123456789}

输出:

  {"firstName":"John","lastName":"Doe","SSN":12345****}

并在Stack Overflow上找到了这个问题,但难以理解正则表达式。任何帮助将不胜感激。

https://stackoverflow.com/questions/25277930/mask-sensitive-data-in-logs-with-logback

英文:

I am trying to mask sensitive information like SSN and Credit card in my Spring boot application using Logback.xml. I searched various links in web, but could not find good solution.Is there a simple-way or any library to mask the sensitive information in logs?

Input

{"firstName":"John","lastName":"Doe","SSN":123456789}

output:

  {"firstName":"John","lastName":"Doe","SSN":12345****}

And found this on stack overflow but trouble figuring out regex.Any help would be greatly appreciated

https://stackoverflow.com/questions/25277930/mask-sensitive-data-in-logs-with-logback

答案1

得分: 1

你可以尝试使用 String.ReplaceAll(String regex, String replacement)。正则表达式应该只匹配 SSN 的前5位数字,保留它们并替换其他内容。由于我们知道每个 SSN 只有9位数字,所以可以这样捕获前5位:

String rgx = "([0-9]{5})[0-9]*";

我们将前5位数字捕获到一个组中,然后可以在 ReplaceAll() 中使用 $1 引用该组。我们不关心后面有多少位数字,所以只需使用 [0-9]* 来匹配剩余部分。在我们使用 $1 引用了前5位数字后,只需用 **** 替换其他内容。

结果:

String baseSSN = "123456789";    
String rgx = "([0-9]{5})[0-9]*";
String modifiedSSN = baseSSN.ReplaceAll(rgx, "$1****"); //modifiedSSN = 12345****

你可以在 这里 尝试修改正则表达式。

英文:

You could try using String.ReplaceAll(String regex, String replacement). The regex would want to just match the first 5 digits of the SSN, keep them, and replace everything else. Since we know that every SSN is only 9 digits, just capture the first 5 like so:

String rgx = "([0-9]{5})[0-9]*";

We capture the first 5 in a group, we can then reference that group in ReplaceAll() with $1. We don't care how many digits are after it, so just use [0-9]* to match the rest. After we reference the first 5 digits with $1, just replace everything else with ****.

The result:

String baseSSN = "123456789";    
String rgx = "([0-9]{5})[0-9]*";
String modifiedSSN = baseSSN.ReplaceAll(rgx, "$1****"); //modifiedSSN = 12345****

You can mess with the regex here.

huangapple
  • 本文由 发表于 2020年8月10日 02:43:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/63330044.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定