Location>code7788 >text

Working Standby: Custom Annotations for Data Desensitization

Popularity:689 ℃/2024-11-15 10:28:09

Today, we will explore how to customize annotations to achieve the desensitization of sensitive data processing. In today's data security more and more attention to the background, many enterprises have strict requirements for the protection of sensitive data, especially when dealing with personal privacy, financial information and other sensitive data, desensitization has become a critical link. Therefore, today's content will focus on practical operation, and will not involve too much theoretical analysis.

We will go through a specific example , step by step to show how to desensitize data through custom annotations , the entire process relies only on the Spring Framework , without the introduction of any third-party libraries or additional dependencies . Just follow the steps , you can complete the corresponding functionality . Next, let's start this practical part of the study .

Data desensitization

There are a wide variety of desensitized data types that we need to process, including but not limited to ID numbers, phone numbers, usernames, micro-signals, and more. Each data type has its own specific encryption or desensitization rules, so each data type must be handled separately.

Custom Annotations

Next, we will need to use custom annotations to implement specific features and behaviors. These annotations will act on the properties of each class, depending on their defined purpose, in order to provide the required identification, validation or processing logic in different contexts. Okay, let's compose it:

public class DesensitizeJsonSerializerByTelNo extends JsonSerializer<String> {
    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        // Data desensitization at serialization time
        (("(?<=^..).(?=.*..$)", "*"));
    }
}


public class DesensitizeJsonSerializerByCustNm extends JsonSerializer<String> {

    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        // Data desensitization at serialization time
        (("(?<=^.).*", "*"));
    }
}


public class DesensitizeJsonSerializerByEmail extends JsonSerializer<String> {
    @Override
    public void serialize(String value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
        // Data desensitization at serialization time
        (("(?<=^.|(?<=@.).)([^@])(.*)(?=@|$)", "*$3"));
    }
}

Instead of listing all possible annotations here, we have chosen to list three of the most common and representative types of annotations. These annotations cover the most common requirements we see in real-world development. Next, we'll start adding these annotations to specific classes and properties.

usage

We usually use entity classes to be passed as the return data type to the Spring Framework, Spring will automatically serialize these entity classes for data exchange between the client and the server. Next, we'll start implementing this process in concrete.

@JsonSerialize(using = )
private  String       custNm;
@JsonSerialize(using = )
private  String       ssnCrnNo;

Here, we only demonstrate basic desensitization usage. This approach basically ensures that when returned to the front-end, the data will be desensitized by serialization of the annotation markup, thus avoiding leakage of sensitive information. However, what if we need to desensitize the data during internal processing as well? By default, the desensitization is only triggered by annotations when the data is returned to the front-end, and the data is not automatically desensitized in the internal logic.

In this case, we can use theObjectMapper to manually desensitize the object to ensure that the same desensitization effect is achieved when used internally.

internal desensitization

Next, in this example, we will demonstrate how to protect the privacy of sensitive information through desensitization. Below is the specific code implementation:

List<ResultInfoVO> list = selectResultByCondition(searchVO);
ObjectMapper objectMapper = new ObjectMapper();
String s = (list);
list = (s, new <List<ResultInfoVO>>() {});

"In this way, data is automatically desensitized. In fact, the whole process is just a process of converting the object into a transferable format through serialization and then restoring it to the original object through deserialization.

summarize

In today's increasingly important data security, desensitization of sensitive data is especially critical, especially when it comes to personal privacy and financial information. In this paper, we explore how to use the Spring Framework for data desensitization without introducing third-party libraries through custom annotations. We show how to write custom annotations for common sensitive data types (e.g., phone numbers, ID cards, emails, etc.) and apply desensitization rules in the serialization process through concrete examples. With these annotations, sensitive information can be automatically desensitized when the data is returned to the front-end to avoid leakage.


I'm Rain, a Java server-side coder, studying the mysteries of AI technology. I love technical communication and sharing, and I am passionate about open source community. I am also a Tencent Cloud Creative Star, Ali Cloud Expert Blogger, Huawei Cloud Enjoyment Expert, and Nuggets Excellent Author.

💡 I won't be shy about sharing my personal explorations and experiences on the path of technology, in the hope that I can bring some inspiration and help to your learning and growth.

🌟 Welcome to the effortless drizzle! 🌟