{"id":10006346,"date":"2022-08-30T14:59:03","date_gmt":"2022-08-30T06:59:03","guid":{"rendered":"https:\/\/ityet.com\/?p=10006346"},"modified":"2022-08-30T14:59:03","modified_gmt":"2022-08-30T06:59:03","slug":"%e4%b8%80%e4%b8%aa%e6%b3%a8%e8%a7%a3%e6%90%9e%e5%ae%9a%e6%8e%a5%e5%8f%a3%e6%95%b0%e6%8d%ae%e8%84%b1%e6%95%8f%ef%bc%8c%e5%a4%aa%e5%bc%ba%e4%ba%86%ef%bc%81","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10006346","title":{"rendered":"\u4e00\u4e2a\u6ce8\u89e3\u641e\u5b9a\u63a5\u53e3\u6570\u636e\u8131\u654f\uff0c\u592a\u5f3a\u4e86\uff01"},"content":{"rendered":"<p>\u6765\u6e90\uff1ajuejin.cn\/post\/7110110794188062727<\/p>\n<p>\u4e0b\u5348\u60ec\u610f\u65f6\u5149\uff0c\u7a81\u7136\u4ea7\u54c1\u5c0f\u59d0\u59d0\u8d70\u5230\u6211\u9762\u524d\uff0c\u6253\u65ad\u6211\u77ed\u6682\u7684\u6478\u9c7ctime\uff0c\u4f01\u56fe\u4e0e\u6211\u8fdb\u884c\u6df1\u5165\u4ea4\u6d41\uff0c\u8fd8\u597d\u6211\u65e9\u6709\u9632\u5907\u6ca1\u6709\u95ea\uff0c\u6253\u5f00\u745estar\u7684\u70b9\u5355\u9875\u9762\uff0c\u6697\u793a\u6ca1\u6709\u4e00\u676fcoffee\u89e3\u51b3\u4e0d\u4e86\u7684\u9700\u6c42\uff0c\u9700\u6c42\u662f\u67d0\u4e9b\u63a5\u53e3\u8fd4\u56de\u7684\u4fe1\u606f\uff0c\u6d89\u53ca\u5230\u654f\u611f\u6570\u636e\u7684\u5fc5\u987b\u8fdb\u884c\u8131\u654f\u64cd\u4f5c\uff0c\u6211\u601d\u8003\u4e00\u53cd\uff0c\u8868\u793a\u67d0\u95ee\u9898\uff0c\u9a6c\u4e0a\u5b89\u6392\u3002<\/p>\n<p><img decoding=\"async\" referrerpolicy=\"no-referrer\" src=\"https:\/\/segmentfault.com\/img\/bVc15gH\" alt=\"\" title=\"\" loading=\"lazy\"><\/p>\n<h3>\u601d\u8def<\/h3>\n<p>1.\u8981\u505a\u6210\u53ef\u914d\u7f6e\u591a\u7b56\u7565\u7684\u8131\u654f\u64cd\u4f5c\uff0c\u8981\u4e0d\u7136\u4e00\u4e2a\u4e2a\u63a5\u53e3\u8fdb\u884c\u8131\u654f\u64cd\u4f5c\uff0c\u91cd\u590d\u7684\u5de5\u4f5c\u91cf\u592a\u591a\uff0c\u5f88\u663e\u7136\u8fdd\u80cc\u4e86\u201c\u591a\u5199\u4e00\u884c\u7b97\u6211\u8f93\u201d\u7684\u7a0b\u5e8f\u5458\u89c4\u8303\uff0c\u601d\u6765\u60f3\u53bb\uff0c\u5b9a\u4e49\u6570\u636e\u8131\u654f\u6ce8\u89e3\u548c\u6570\u636e\u8131\u654f\u903b\u8f91\u7684\u63a5\u53e3\uff0c \u5728\u8fd4\u56de\u7c7b\u4e0a\uff0c\u5bf9\u9700\u8981\u8fdb\u884c\u8131\u654f\u7684\u5c5e\u6027\u52a0\u4e0a\uff0c\u5e76\u6307\u5b9a\u5bf9\u5e94\u7684\u8131\u654f\u7b56\u7565\u64cd\u4f5c\u3002<\/p>\n<p>2.\u63a5\u4e0b\u6765\u6211\u53ea\u9700\u8981\u62e6\u622a\u63a7\u5236\u5668\u8fd4\u56de\u7684\u6570\u636e\uff0c\u627e\u5230\u5e26\u6709\u8131\u654f\u6ce8\u89e3\u7684\u5c5e\u6027\u64cd\u4f5c\u5373\u53ef\uff0c\u4e00\u5f00\u59cb\u6253\u7b97\u7528@ControllerAdvice\u53bb\u5b9e\u73b0\uff0c\u4f46\u53d1\u73b0\u9700\u8981\u81ea\u5df1\u53bb\u53cd\u5c04\u7c7b\u83b7\u53d6\u6ce8\u89e3\uff0c\u5f53\u8fd4\u56de\u5bf9\u8c61\u6bd4\u8f83\u590d\u6742\uff0c\u9700\u8981\u9012\u5f52\u53bb\u53cd\u5c04\uff0c\u6027\u80fd\u4e00\u4e0b\u5b50\u5c31\u4f1a\u964d\u4f4e\uff0c\u4e8e\u662f\u6362\u79cd\u601d\u8def\uff0c\u6211\u60f3\u5230\u5e73\u65f6\u4f7f\u7528\u7684@JsonFormat\uff0c\u8ddf\u6211\u73b0\u5728\u7684\u573a\u666f\u5f88\u7c7b\u4f3c\uff0c\u901a\u8fc7\u81ea\u5b9a\u4e49\u6ce8\u89e3\u8ddf\u5b57\u6bb5\u89e3\u6790\u5668\uff0c\u5bf9\u5b57\u6bb5\u8fdb\u884c\u81ea\u5b9a\u4e49\u89e3\u6790\uff0ctql<\/p>\n<h3>\u4ee3\u7801<\/h3>\n<p>Spring Boot \u57fa\u7840\u5c31\u4e0d\u4ecb\u7ecd\u4e86\uff0c\u63a8\u8350\u4e0b\u8fd9\u4e2a\u5b9e\u6218\u6559\u7a0b\uff1a<\/p>\n<blockquote><p><a href=\"https:\/\/link.segmentfault.com\/?enc=0mY7b1k5LBT9ep%2FeOBp6JQ%3D%3D.6l1H6HwDOiAeXgBo7TTNHOoZN9PFUHU2OE%2BgpCEg9Yd4okg7gbETSy1n3HQEdVrGUdf2xiTFPWgj3dfu1y9DMQ%3D%3D\" rel=\"nofollow\">https:\/\/github.com\/javastacks&#8230;<\/a><\/p><\/blockquote>\n<h4>1. \u81ea\u5b9a\u4e49\u6570\u636e\u6ce8\u89e3\uff0c\u5e76\u53ef\u4ee5\u914d\u7f6e\u6570\u636e\u8131\u654f\u7b56\u7565<\/h4>\n<pre><code>@Target({ElementType.FIELD, ElementType.TYPE})\n@Retention(RetentionPolicy.RUNTIME)\n@Documented\npublic @interface DataMasking {\n\n    DataMaskingFunc maskFunc() default DataMaskingFunc.NO_MASK;\n\n}<\/code><\/pre>\n<h4>2. \u81ea\u5b9a\u4e49Serializer\uff0c\u53c2\u8003jackson\u7684StringSerializer\uff0c\u4e0b\u9762\u7684\u793a\u4f8b\u53ea\u9488\u5bf9String\u7c7b\u578b\u8fdb\u884c\u8131\u654f<\/h4>\n<pre><code>public interface DataMaskingOperation {\n\n    String MASK_CHAR = &quot;*&quot;;\n\n    String mask(String content, String maskChar);\n\n}\n\npublic enum DataMaskingFunc {\n\n     \/**\n     *  \u8131\u654f\u8f6c\u6362\u5668\n     *\/\n     NO_MASK((str, maskChar) -&gt; {\n        return str;\n     }),\n     ALL_MASK((str, maskChar) -&gt; {\n        if (StringUtils.hasLength(str)) {\n            StringBuilder sb = new StringBuilder();\n            for (int i = 0; i &lt; str.length(); i++) {\n                sb.append(StringUtils.hasLength(maskChar) ? maskChar : DataMaskingOperation.MASK_CHAR);\n            }\n            return sb.toString();\n        } else {\n            return str;\n        }\n    });\n\n    private final DataMaskingOperation operation;\n\n    private DataMaskingFunc(DataMaskingOperation operation) {\n        this.operation = operation;\n    }\n\n    public DataMaskingOperation operation() {\n        return this.operation;\n    }\n\n}\n\npublic final class DataMaskingSerializer extends StdScalarSerializer&lt;Object&gt; {\n    private final DataMaskingOperation operation;\n\n    public DataMaskingSerializer() {\n        super(String.class, false);\n        this.operation = null;\n    }\n\n    public DataMaskingSerializer(DataMaskingOperation operation) {\n        super(String.class, false);\n        this.operation = operation;\n    }\n\n    public boolean isEmpty(SerializerProvider prov, Object value) {\n        String str = (String)value;\n        return str.isEmpty();\n    }\n\n    public void serialize(Object value, JsonGenerator gen, SerializerProvider provider) throws IOException {\n        if (Objects.isNull(operation)) {\n            String content = DataMaskingFunc.ALL_MASK.operation().mask((String) value, null);\n            gen.writeString(content);\n        } else {\n            String content = operation.mask((String) value, null);\n            gen.writeString(content);\n        }\n    }\n\n    public final void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider, TypeSerializer typeSer) throws IOException {\n        this.serialize(value, gen, provider);\n    }\n\n    public JsonNode getSchema(SerializerProvider provider, Type typeHint) {\n        return this.createSchemaNode(&quot;string&quot;, true);\n    }\n\n    public void acceptJsonFormatVisitor(JsonFormatVisitorWrapper visitor, JavaType typeHint) throws JsonMappingException {\n        this.visitStringFormat(visitor, typeHint);\n    }\n}<\/code><\/pre>\n<h4>3. \u81ea\u5b9a\u4e49AnnotationIntrospector\uff0c\u9002\u914d\u6211\u4eec\u81ea\u5b9a\u4e49\u6ce8\u89e3\u8fd4\u56de\u76f8\u5e94\u7684Serializer<\/h4>\n<pre><code>@Slf4j\npublic class DataMaskingAnnotationIntrospector extends NopAnnotationIntrospector {\n\n    @Override\n    public Object findSerializer(Annotated am) {\n        DataMasking annotation = am.getAnnotation(DataMasking.class);\n        if (annotation != null) {\n            return new DataMaskingSerializer(annotation.maskFunc().operation());\n        }\n        return null;\n    }\n\n}<\/code><\/pre>\n<h4>4. \u8986\u76d6ObjectMapper<\/h4>\n<pre><code>@Configuration(\n        proxyBeanMethods = false\n)\npublic class DataMaskConfiguration {\n\n    @Configuration(\n            proxyBeanMethods = false\n    )\n    @ConditionalOnClass({Jackson2ObjectMapperBuilder.class})\n    static class JacksonObjectMapperConfiguration {\n        JacksonObjectMapperConfiguration() {\n        }\n\n        @Bean\n        @Primary\n        ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {\n            ObjectMapper objectMapper = builder.createXmlMapper(false).build();\n            AnnotationIntrospector ai = objectMapper.getSerializationConfig().getAnnotationIntrospector();\n            AnnotationIntrospector newAi = AnnotationIntrospectorPair.pair(ai, new DataMaskingAnnotationIntrospector());\n            objectMapper.setAnnotationIntrospector(newAi);\n            return objectMapper;\n        }\n    }\n\n}<\/code><\/pre>\n<h4>5. \u8fd4\u56de\u5bf9\u8c61\u52a0\u4e0a\u6ce8\u89e3<\/h4>\n<pre><code>public class User implements Serializable {\n    \/**\n     * \u4e3b\u952eID\n     *\/\n    private Long id;\n\n    \/**\n     * \u59d3\u540d\n     *\/\n    @DataMasking(maskFunc = DataMaskingFunc.ALL_MASK)\n    private String name;\n\n    \/**\n     * \u5e74\u9f84\n     *\/\n    private Integer age;\n\n    \/**\n     * \u90ae\u7bb1\n     *\/\n    @DataMasking(maskFunc = DataMaskingFunc.ALL_MASK)\n    private String email;\n\n}<\/code><\/pre>\n<p><strong>\u8fd1\u671f\u70ed\u6587\u63a8\u8350\uff1a<\/strong><\/p>\n<p>1.<a href=\"https:\/\/link.segmentfault.com\/?enc=lcK3hwzus3SwzaHa0tNATg%3D%3D.we7Nkfv%2FicCdGSoC4mJJa%2BazNimiZBSNN88p%2B%2B0d5%2Fg%3D\" rel=\"nofollow\">1,000+ \u9053 Java\u9762\u8bd5\u9898\u53ca\u7b54\u6848\u6574\u7406(2022\u6700\u65b0\u7248)<\/a><\/p>\n<p>2.<a href=\"https:\/\/link.segmentfault.com\/?enc=8jakaIKJkzyyt8jJgTwR4g%3D%3D.zRYg7PoHu5IH4o0JPKKVNi%2B%2BMMbZgusvOVy1yeREuBjxQjA8TjPuv03BCPuVzVoRboW0T%2BwXqhRITh%2BTtcnuWg%3D%3D\" rel=\"nofollow\">\u52b2\u7206\uff01Java \u534f\u7a0b\u8981\u6765\u4e86\u3002\u3002\u3002<\/a><\/p>\n<p>3.<a href=\"https:\/\/link.segmentfault.com\/?enc=zrVst18n2uWjpW5k9Kf63g%3D%3D.Oh6OSYObPPJhCJLMc9rnvWuEtCSm%2Fl3JSHYawGpaUCU0twzpZpZLHEXLca%2F8z8gQ\" rel=\"nofollow\">Spring Boot 2.x \u6559\u7a0b\uff0c\u592a\u5168\u4e86\uff01<\/a><\/p>\n<p>4.<a href=\"https:\/\/link.segmentfault.com\/?enc=hTgQX%2FZQQK5T27jL41U4zA%3D%3D.CmkAwLvLt96SxW3jKVA%2Fu3AfrZAU8xptOTePY0anJS9C4mS1UF2Gll8KW1%2FPw4PTI4tgz71Jeztf5kmHU6rtjA%3D%3D\" rel=\"nofollow\">\u522b\u518d\u5199\u6ee1\u5c4f\u7684\u7206\u7206\u7206\u70b8\u7c7b\u4e86\uff0c\u8bd5\u8bd5\u88c5\u9970\u5668\u6a21\u5f0f\uff0c\u8fd9\u624d\u662f\u4f18\u96c5\u7684\u65b9\u5f0f\uff01\uff01<\/a><\/p>\n<p>5.<a href=\"https:\/\/link.segmentfault.com\/?enc=VR6nOTlO4UY4DGeHNRIb0g%3D%3D.RBR8DAaybbWmvj0SRN6ktOYrJXmUWk7g1nFZDLUqVLs4iiegaj%2F7krMttgaRCP4FPp23Kpc7cdSVyMXNLaMM6%2FdSaxuwgd7R0Pyaj3EAJDw%3D\" rel=\"nofollow\">\u300aJava\u5f00\u53d1\u624b\u518c\uff08\u5d69\u5c71\u7248\uff09\u300b\u6700\u65b0\u53d1\u5e03\uff0c\u901f\u901f\u4e0b\u8f7d\uff01<\/a><\/p>\n<p>\u89c9\u5f97\u4e0d\u9519\uff0c\u522b\u5fd8\u4e86\u968f\u624b\u70b9\u8d5e+\u8f6c\u53d1\u54e6\uff01<\/p>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000042403570\">https:\/\/segmentfault.com\/a\/1190000042403570<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u6765\u6e90\uff1ajuejin.cn\/post\/7110110794188062727 \u4e0b\u5348\u60ec\u610f\u65f6\u5149\uff0c\u7a81\u7136\u4ea7\u54c1\u5c0f\u59d0\u59d0\u8d70\u5230\u6211 [&hellip;]<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[],"tags":[],"class_list":["post-10006346","post","type-post","status-publish","format-standard","hentry"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10006346"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10006346\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10006346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10006346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10006346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}