• 附加:信息脱敏;


    说明:

    (1)为什么写了本篇博客?:

              ● 在【26:第三章:开发通行证服务:9】中,我们新增AppUser数据的时候,设置用户昵称的时候,编写了一个信息脱敏工具DesensitizationUtil工具类;(可以把【用户+18888888888】转化为【用户+18******888】的形式);

              ● 本篇博客就来介绍下信息脱敏工具DesensitizationUtil;

    (2)其实,本篇博客主要是让自己明确两点:

              ● 对于项目中,一些需要在前台展示,但同时又不想泄密的情况,可以考虑信息脱敏;

              ● 然后,本篇博客,只是一个信息脱敏的案例而已;对于其他项目,虽然有信息脱敏需求,但是这个需求却各有不同(比如有的要求把18888888888转成18***888***;有的要求把18888888888转成18######888;有的要求把李二柱转成李*柱等)

    (3)以后,在其他项目中,遇到具体的信息脱敏需求时,再根据实际情况,编写具体的、适合的逻辑去解决就行了;

    目录

    一:在【26:第三章:开发通行证服务:9】中,DesensitizationUtil工具类的内容;


    一:在【26:第三章:开发通行证服务:9】中,DesensitizationUtil工具类的内容;

    1. package com.imooc.utils;
    2. /**
    3. * 通用脱敏工具类
    4. * 可用于:
    5. * 用户名
    6. * 手机号
    7. * 邮箱
    8. * 地址等
    9. */
    10. public class DesensitizationUtil {
    11. private static final int SIZE = 6;
    12. private static final String SYMBOL = "*";
    13. public static void main(String[] args) {
    14. String name = commonDisplay("慕课网");
    15. String mobile = commonDisplay("13900000000");
    16. String mail = commonDisplay("admin@imooc.com");
    17. String address = commonDisplay("北京大运河东路888号");
    18. System.out.println(name);
    19. System.out.println(mobile);
    20. System.out.println(mail);
    21. System.out.println(address);
    22. }
    23. /**
    24. * 通用脱敏方法
    25. * @param value
    26. * @return
    27. */
    28. public static String commonDisplay(String value) {
    29. if (null == value || "".equals(value)) {
    30. return value;
    31. }
    32. int len = value.length();
    33. int pamaone = len / 2;
    34. int pamatwo = pamaone - 1;
    35. int pamathree = len % 2;
    36. StringBuilder stringBuilder = new StringBuilder();
    37. if (len <= 2) {
    38. if (pamathree == 1) {
    39. return SYMBOL;
    40. }
    41. stringBuilder.append(SYMBOL);
    42. stringBuilder.append(value.charAt(len - 1));
    43. } else {
    44. if (pamatwo <= 0) {
    45. stringBuilder.append(value.substring(0, 1));
    46. stringBuilder.append(SYMBOL);
    47. stringBuilder.append(value.substring(len - 1, len));
    48. } else if (pamatwo >= SIZE / 2 && SIZE + 1 != len) {
    49. int pamafive = (len - SIZE) / 2;
    50. stringBuilder.append(value.substring(0, pamafive));
    51. for (int i = 0; i < SIZE; i++) {
    52. stringBuilder.append(SYMBOL);
    53. }
    54. if ((pamathree == 0 && SIZE / 2 == 0) || (pamathree != 0 && SIZE % 2 != 0)) {
    55. stringBuilder.append(value.substring(len - pamafive, len));
    56. } else {
    57. stringBuilder.append(value.substring(len - (pamafive + 1), len));
    58. }
    59. } else {
    60. int pamafour = len - 2;
    61. stringBuilder.append(value.substring(0, 1));
    62. for (int i = 0; i < pamafour; i++) {
    63. stringBuilder.append(SYMBOL);
    64. }
    65. stringBuilder.append(value.substring(len - 1, len));
    66. }
    67. }
    68. return stringBuilder.toString();
    69. }
    70. }

    说明:

    (1)这个代码,自己并没有细看;但稍微瞅了一眼,其基本思路就是:在考虑了所有非正常情况后,把18888888888转成18******888;

  • 相关阅读:
    Python调用ChatGPT API使用国内中转key 修改接口教程
    9.子数组统计问题
    识别一切模型RAM(Recognize Anything Model)及其前身 Tag2Text 论文解读
    2023/09/07 c++&qt day2
    C/C++语言100题练习计划 75——自然数拆分问题(DFS算法实现)
    C++之list的用法介绍
    阿里P8熬了一个月肝出这份32W字Java面试手册,在Github标星31K+
    主流商业智能(BI)工具的比较(一):Tableau与Domo
    安卓APP(1)——安卓工程构建和第一APP运行
    MySQL笔记:MySQL求最近一段时间
  • 原文地址:https://blog.csdn.net/csucsgoat/article/details/125564580