Springboot敏感字段脫敏的實(shí)現(xiàn)思路
生產(chǎn)環(huán)境用戶的隱私數(shù)據(jù),比如手機(jī)號(hào)、身份證或者一些賬號(hào)配置等信息,入庫(kù)時(shí)都要進(jìn)行不落地脫敏,也就是在進(jìn)入我們系統(tǒng)時(shí)就要實(shí)時(shí)的脫敏處理。
用戶數(shù)據(jù)進(jìn)入系統(tǒng),脫敏處理后持久化到數(shù)據(jù)庫(kù),用戶查詢數(shù)據(jù)時(shí)還要進(jìn)行反向解密。這種場(chǎng)景一般需要全局處理,那么用AOP
切面來實(shí)現(xiàn)在適合不過了。
首先自定義兩個(gè)注解@EncryptField
、@EncryptMethod
分別用在字段屬性和方法上,實(shí)現(xiàn)思路很簡(jiǎn)單,只要方法上應(yīng)用到@EncryptMethod
注解,則檢查入?yún)⒆侄问欠駱?biāo)注@EncryptField
注解,有則將對(duì)應(yīng)字段內(nèi)容加密。
@Documented @Target({ElementType.FIELD,ElementType.PARAMETER}) @Retention(RetentionPolicy.RUNTIME) public @interface EncryptField { String[] value() default ""; }
@Documented @Target({ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) public @interface EncryptMethod { String type() default ENCRYPT; }
切面的實(shí)現(xiàn)也比較簡(jiǎn)單,對(duì)入?yún)⒓用埽祷亟Y(jié)果解密。
import com.xiaofu.annotation.EncryptField; import com.xiaofu.annotation.EncryptMethod; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.annotation.*; import org.aspectj.lang.reflect.MethodSignature; import org.jasypt.encryption.StringEncryptor; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.springframework.util.SerializationUtils; import java.lang.reflect.Field; import java.util.*; import static com.xiaofu.enums.EncryptConstant.DECRYPT; import static com.xiaofu.enums.EncryptConstant.ENCRYPT; @Slf4j @Aspect @Component public class EncryptHandler { @Autowired private StringEncryptor stringEncryptor; @Pointcut("@annotation(com.xiaofu.annotation.EncryptMethod)") public void pointCut() { } @Around("pointCut()") public Object around(ProceedingJoinPoint joinPoint) { /** * 加密 */ encrypt(joinPoint); /** * 解密 */ Object decrypt = decrypt(joinPoint); return decrypt; } public void encrypt(ProceedingJoinPoint joinPoint) { try { Object[] objects = joinPoint.getArgs(); if (objects.length != 0) { for (Object o : objects) { if (o instanceof String) { encryptValue(o); } else { handler(o, ENCRYPT); } //TODO 其余類型自己看實(shí)際情況加 } } } catch (IllegalAccessException e) { e.printStackTrace(); } } public Object decrypt(ProceedingJoinPoint joinPoint) { Object result = null; try { Object obj = joinPoint.proceed(); if (obj != null) { if (obj instanceof String) { decryptValue(obj); } else { result = handler(obj, DECRYPT); } //TODO 其余類型自己看實(shí)際情況加 } } catch (Throwable e) { e.printStackTrace(); } return result; } private Object handler(Object obj, String type) throws IllegalAccessException { if (Objects.isNull(obj)) { return null; } Field[] fields = obj.getClass().getDeclaredFields(); for (Field field : fields) { boolean hasSecureField = field.isAnnotationPresent(EncryptField.class); if (hasSecureField) { field.setAccessible(true); String realValue = (String) field.get(obj); String value; if (DECRYPT.equals(type)) { value = stringEncryptor.decrypt(realValue); } else { value = stringEncryptor.encrypt(realValue); } field.set(obj, value); } } return obj; } public String encryptValue(Object realValue) { String value = null; try { value = stringEncryptor.encrypt(String.valueOf(realValue)); } catch (Exception ex) { return value; } return value; } public String decryptValue(Object realValue) { String value = String.valueOf(realValue); try { value = stringEncryptor.decrypt(value); } catch (Exception ex) { return value; } return value; } }
緊接著測(cè)試一下切面注解的效果,我們對(duì)字段mobile
、address
加上注解@EncryptField
做脫敏處理。
@EncryptMethod @PostMapping(value = "test") @ResponseBody public Object testEncrypt(@RequestBody UserVo user, @EncryptField String name) { return insertUser(user, name); } private UserVo insertUser(UserVo user, String name) { System.out.println("加密后的數(shù)據(jù):user" + JSON.toJSONString(user)); return user; } @Data public class UserVo implements Serializable { private Long userId; @EncryptField private String mobile; @EncryptField private String address; private String age; }
請(qǐng)求這個(gè)接口,看到參數(shù)被成功加密,而返回給用戶的數(shù)據(jù)依然是脫敏前的數(shù)據(jù),符合我們的預(yù)期,那到這簡(jiǎn)單的脫敏實(shí)現(xiàn)就完事了。
到此這篇關(guān)于Springboot敏感字段脫敏的文章就介紹到這了,更多相關(guān)Springboot脫敏內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringBoot多環(huán)境打包與配置文件排除實(shí)踐記錄
本文介紹了SpringBoot項(xiàng)目多環(huán)境打包與配置文件排除實(shí)踐,包括多環(huán)境配置的實(shí)現(xiàn)方法、打包時(shí)排除配置文件的方法以及動(dòng)態(tài)加載外部配置文件的最佳實(shí)踐,感興趣的朋友跟隨小編一起看看吧2024-11-11Java棧之鏈?zhǔn)綏4鎯?chǔ)結(jié)構(gòu)的實(shí)現(xiàn)代碼
這篇文章主要介紹了Java棧之鏈?zhǔn)綏4鎯?chǔ)結(jié)構(gòu)的實(shí)現(xiàn)代碼的相關(guān)資料,需要的朋友可以參考下2017-04-04使用spring?security?BCryptPasswordEncoder接入系統(tǒng)
這篇文章主要介紹了使用spring?security?BCryptPasswordEncoder接入系統(tǒng)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08Java開發(fā)環(huán)境jdk 1.8安裝配置方法(Win7 64位系統(tǒng)/windows server 2008)
這篇文章主要介紹了Java開發(fā)環(huán)境配置方法(Win7 64位系統(tǒng)/windows server 2008),需要的朋友可以參考下2016-10-10SpringBoot自定義starter啟動(dòng)器的實(shí)現(xiàn)思路
這篇文章主要介紹了SpringBoot如何自定義starter啟動(dòng)器,通過starter的自定義過程,能夠加深大家對(duì)SpringBoot自動(dòng)配置原理的理解,需要的朋友可以參考下2022-10-10詳解如何開發(fā)一個(gè)MyBatis通用Mapper的輪子
因?yàn)橐恍┰?,例如:通用?shù)據(jù)權(quán)限控制、MyBatis-Plus好像不支持聯(lián)合主鍵等,我們不得不開發(fā)一個(gè)MyBatis通用Mapper的輪子。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-12-12