Java实现地址脱敏至第二级

22

Java实现地址脱敏至第二级(解决地级行政区不一定是市的问题)

Author: histonevon@zohomail.com

Date: 2023/07/09

Source: https://github.com/HistoneVon/addressDesensitization

Blog: https://histonevon.top/archives/java-address-desensitization

[TOC]

说明

  • 将中华人民共和国的地址脱敏到第二级(不一定是地区级)
  • 中华人民共和国的地区级行政区划分为四种:地级市、地区、盟、自治州
  • 地址脱敏思路如下
    • 找到【盟】
    • 找到【自治州】
    • 找到【地区】
    • 找到【市】
      • 如果是【地级市】即可结束
      • 如果是【直辖市】则找到下一级的【区】或【县】
    • 如果均不符合则脱敏2/3的地址(包括英文)
  • 使用到了cn.hutool.core.util.StrUtil

实现

  • Utils.java类

    package org.example;
    
    /**
     * @author Histone Von histonevon@zohomail.com
     * @createdBy Histone Von
     * @date 2023/7/9 14:15
     */
    
    public class Utils {
        /**
         * 返回子串在母串中最后一个字符的索引
         * @param str 母串
         * @param subStr 子串
         * @return int 子串最后一个字符的索引,错误返回-2,未找到返回-1
         */
        public static int strStr(String str, String subStr) {
            int index = -2;
            if (!StrUtil.isBlank(str) && !StrUtil.isBlank(subStr)) {
                index = str.indexOf(subStr);
                if (index != -1) {
                    index += subStr.length() - 1;
                }
            }
            return index;
        }
    }
    
    
  • AddressDesensitization.java类:传入地址,返回脱敏后地址

    package org.example;
    
    import cn.hutool.core.util.StrUtil;
    
    /**
     * @author Histone Von histonevon@zohomail.com
     * @createdBy Histone Von
     * @date 2023/7/9 14:28
     */
    
    public class AddressDesensitization {
        public static String desensitize(String address) {
            if (StrUtil.isBlank(address)) {
                return "";
            } else {
                String addressDesensitized;
                if (Utils.strStr(address, "盟") >= 0) { // 1.盟
                    addressDesensitized = StrUtil.hide(address, Utils.strStr(address, "盟") + 1, address.length());
                } else if (Utils.strStr(address, "自治州") >= 0) { // 2.自治州
                    addressDesensitized = StrUtil.hide(address, Utils.strStr(address, "自治州") + 1, address.length());
                } else if (Utils.strStr(address, "地区") >= 0) { // 3.地区
                    addressDesensitized = StrUtil.hide(address, Utils.strStr(address, "地区") + 1, address.length());
                } else if (Utils.strStr(address, "市") >= 0) { // 4.市
                    boolean isBeijing = address.contains("北京"); // 4-1.排除直辖市
                    boolean isShanghai = address.contains("上海");
                    boolean isTianjin = address.contains("天津");
                    boolean isChongqing = address.contains("重庆");
                    if (!isBeijing && !isShanghai && !isTianjin && !isChongqing) { // 4-2.地级市
                        addressDesensitized = StrUtil.hide(address, Utils.strStr(address, "市") + 1, address.length());
                    } else { // 4-3.直辖市查区县
                        int indexDistrictOrCounty; // 直辖市的区或者县
                        if (Utils.strStr(address, "区") >= 0) {
                            indexDistrictOrCounty = Utils.strStr(address, "区");
                        } else if (Utils.strStr(address, "县") >= 0) {
                            indexDistrictOrCounty = Utils.strStr(address, "县");
                        } else {
                            indexDistrictOrCounty = 3; // 从下标3的字符开始脱敏,直辖市都是三个字的
                        }
                        addressDesensitized = StrUtil.hide(address, indexDistrictOrCounty + 1, address.length());
                    }
                } else { // 5.其余的脱敏2/3
                    addressDesensitized = StrUtil.hide(address, address.length() / 3, address.length());
                }
                return addressDesensitized;
            }
        }
    }
    
    

运行示例

package org.example;

/**
 * @author Histone Von histonevon@zohomail.com
 * @createdBy Histone Von
 * @date 2023/7/9 14:05
 */

public class App {
    public static void main(String[] args) {
        System.out.println(AddressDesensitization.desensitize("浙江省宁波市江北区同济路280弄155号"));
        System.out.println(AddressDesensitization.desensitize(""));
        System.out.println(AddressDesensitization.desensitize("内蒙古自治区锡林郭勒盟二连浩特市格日勒敖都苏木"));
        System.out.println(AddressDesensitization.desensitize("吉林省延边朝鲜族自治州延吉市依兰镇"));
        System.out.println(AddressDesensitization.desensitize("新疆维吾尔自治区阿勒泰地区阿勒泰市解放路街道"));
        System.out.println(AddressDesensitization.desensitize("北京市海淀区中关村街道"));
        System.out.println(AddressDesensitization.desensitize("上海市浦东新区陆家嘴街道"));
        System.out.println(AddressDesensitization.desensitize("重庆市酉阳土家族苗族自治县中和街道"));
        System.out.println(AddressDesensitization.desensitize("香港特别行政区湾仔区铜锣湾"));
        System.out.println(AddressDesensitization.desensitize("No. 7, Section 5, Xinyi Road, Xinyi District, Taipei"));
    }
}

image-20230709180333322