In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how Java through annotations to achieve interface output data desensitization, I believe that most people do not know much about it, so share this article for your reference, I hope you will learn a lot after reading this article, let's learn about it!
Desensitization of Interface output data by Java Annotation
In the background management, the data such as mobile phone number, ID card and name are not allowed to be viewed by everyone, so we have to desensitize the relative data.
Declared a note first.
By declaring the relevant interface functions and configuring the parameter type SecretTypeEnum that needs desensitization, the default desensitization mobile phone number
/ * * desensitization statement * / @ Documented@Target (ElementType.METHOD) @ Retention (RetentionPolicy.RUNTIME) public @ interface SecretManage {SecretTypeEnum [] value () default {SecretTypeEnum.MOBILE};} currently we only support mobile phone numbers.
ID card and user name are desensitized in three fields. The field name must conform to the enumeration value.
Package com.test.base.enums;import com.fasterxml.jackson.annotation.JsonValue;/** * @ author fyf * @ date 2021-2-26 * / public enum SecretTypeEnum implements BaseEnum {MOBILE (0, "mobile"), NAME (1, "name"), ID (2, "identity"); @ JsonValue private int code; private String desc; public String getDesc () {return desc } SecretTypeEnum (int code, String desc) {this.code = code; this.desc = desc;} @ Override public int code () {return code;} public static SecretTypeEnum checkParam (String paramName) {SecretTypeEnum [] values = values (); for (int I = 0; I
< values.length; i++) { if (paramName.equals(values[i].getDesc())) { return values[i]; } } return null; }}然后我们需要实现注解的拦截功能/** * 对字段进行脱敏管理 * @author: fyf * @date: 2021/02/23 */@Order(1)@Aspect@Componentpublic class SecretManageAspect { @Pointcut("@annotation(com.test.base.annotations.SecretManage)") public void pointCut() { } @Around("pointCut() && @annotation(secretManage)") public Object secretManageAround(ProceedingJoinPoint joinPoint, SecretManage secretManage) throws Throwable { Class returnType = ((MethodSignature) joinPoint.getSignature()).getReturnType(); Object invokeResult = joinPoint.proceed(); if (returnType.isInstance(invokeResult)) { returnType.cast(invokeResult); } Field[] fields = returnType.getDeclaredFields(); List annotationSecretTypeEnums = Arrays.asList(secretManage.value()); for (Field field : fields) { String fieldName = field.getName(); Class paramType = field.getType(); System.out.println("字段名称:" + fieldName); SecretTypeEnum secretTypeEnum = SecretTypeEnum.checkParam(fieldName); long count = annotationSecretTypeEnums.stream().filter(item ->Item.equals (secretTypeEnum). Count (); if (secretTypeEnum! = null & & count > 0) {fieldName = fieldName.substring (0,1). ToUpperCase () + fieldName.substring (1); / / get the setter method Method setMethod = returnType.getMethod ("set" + fieldName, new Class [] {paramType}) / / get getter method Method getMethod = returnType.getMethod ("get" + fieldName); / / execute getter method Object value = getMethod.invoke (invokeResult); this.invokeSetter (setMethod, invokeResult, secretTypeEnum, value);}} return invokeResult } / * encapsulates the execution of setter functions * / private void invokeSetter (Method setterMethod, Object invokeResult, SecretTypeEnum secretTypeEnum, Object value) throws InvocationTargetException, IllegalAccessException {switch (secretTypeEnum) {case NAME: setterMethod.invoke (invokeResult, SecretUtil.nameSecret (value.toString (); break Case MOBILE: setterMethod.invoke (invokeResult, SecretUtil.mobileSecret (value.toString ()); break; case ID: setterMethod.invoke (invokeResult, SecretUtil.idNoSecret (value.toString (); break;}
Above, we have realized the desensitization function, and now we can see the results through the mock interface.
I tested the default declaration and desensitization name and mobile phone number / * * curl localhost:9999/user/test-secret * / @ GetMapping ("/ test-secret") / / @ SecretManage (value = {SecretTypeEnum.NAME, SecretTypeEnum.MOBILE}) @ SecretManage public User getSecretUser () {User user = new User (); user.setId (1) User.setMobile ("13715166409"); user.setName ("Zhang Zhixin"); user.setIdentity ("370283790911703"); return user;}
Here are the test results
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.