This commit is contained in:
+32
@@ -1,6 +1,7 @@
|
||||
package com.xiaowang.jayucloud.salon.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import com.xiaowang.jayucloud.salon.core.support.privacy.PhoneCrypto;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
@@ -27,6 +28,7 @@ public class MyMetaObjectHandler implements MetaObjectHandler {
|
||||
log.info("start insert fill ....");
|
||||
this.strictInsertFill(metaObject, "createTime", LocalDateTime.class, LocalDateTime.now());
|
||||
this.strictInsertFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
|
||||
fillPhoneIndexes(metaObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -37,5 +39,35 @@ public class MyMetaObjectHandler implements MetaObjectHandler {
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
log.info("start update fill ....");
|
||||
this.strictUpdateFill(metaObject, "updateTime", LocalDateTime.class, LocalDateTime.now());
|
||||
fillPhoneIndexes(metaObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Populate searchable HMAC indexes for encrypted phone fields while keeping
|
||||
* this class as the single MyBatis-Plus MetaObjectHandler bean.
|
||||
*/
|
||||
private void fillPhoneIndexes(MetaObject metaObject) {
|
||||
setAccountIndex(metaObject);
|
||||
setIndex(metaObject, "phone", "phoneHash");
|
||||
setIndex(metaObject, "contactPhone", "contactPhoneHash");
|
||||
setIndex(metaObject, "customerPhoneSnapshot", "customerPhoneSnapshotHash");
|
||||
}
|
||||
|
||||
private void setAccountIndex(MetaObject metaObject) {
|
||||
if (!metaObject.hasGetter("account") || !metaObject.hasSetter("accountHash")) {
|
||||
return;
|
||||
}
|
||||
Object value = metaObject.getValue("account");
|
||||
String account = value == null ? null : String.valueOf(value);
|
||||
metaObject.setValue("accountHash", account == null ? null : PhoneCrypto.current().index(account));
|
||||
}
|
||||
|
||||
private void setIndex(MetaObject metaObject, String source, String target) {
|
||||
if (!metaObject.hasGetter(source) || !metaObject.hasSetter(target)) {
|
||||
return;
|
||||
}
|
||||
Object value = metaObject.getValue(source);
|
||||
String hash = value == null ? null : PhoneCrypto.current().index(String.valueOf(value));
|
||||
metaObject.setValue(target, hash);
|
||||
}
|
||||
}
|
||||
|
||||
-39
@@ -1,39 +0,0 @@
|
||||
package com.xiaowang.jayucloud.salon.core.support.privacy;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.apache.ibatis.reflection.MetaObject;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SensitivePhoneMetaObjectHandler implements MetaObjectHandler {
|
||||
@Override
|
||||
public void insertFill(MetaObject metaObject) {
|
||||
fillIndexes(metaObject);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateFill(MetaObject metaObject) {
|
||||
fillIndexes(metaObject);
|
||||
}
|
||||
|
||||
private void fillIndexes(MetaObject metaObject) {
|
||||
setAccountIndex(metaObject);
|
||||
setIndex(metaObject, "phone", "phoneHash");
|
||||
setIndex(metaObject, "contactPhone", "contactPhoneHash");
|
||||
setIndex(metaObject, "customerPhoneSnapshot", "customerPhoneSnapshotHash");
|
||||
}
|
||||
|
||||
private void setAccountIndex(MetaObject metaObject) {
|
||||
if (!metaObject.hasGetter("account") || !metaObject.hasSetter("accountHash")) return;
|
||||
Object value = metaObject.getValue("account");
|
||||
String account = value == null ? null : String.valueOf(value);
|
||||
metaObject.setValue("accountHash", account == null ? null : PhoneCrypto.current().index(account));
|
||||
}
|
||||
|
||||
private void setIndex(MetaObject metaObject, String source, String target) {
|
||||
if (!metaObject.hasGetter(source) || !metaObject.hasSetter(target)) return;
|
||||
Object value = metaObject.getValue(source);
|
||||
String hash = value == null ? null : PhoneCrypto.current().index(String.valueOf(value));
|
||||
metaObject.setValue(target, hash);
|
||||
}
|
||||
}
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
package com.xiaowang.jayucloud.salon.core.config;
|
||||
|
||||
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||
import org.springframework.core.type.filter.AssignableTypeFilter;
|
||||
|
||||
import java.util.Set;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
class MetaObjectHandlerRegistrationTest {
|
||||
|
||||
@Test
|
||||
void onlyOneMetaObjectHandlerImplementationIsRegistered() {
|
||||
ClassPathScanningCandidateComponentProvider scanner = new ClassPathScanningCandidateComponentProvider(false);
|
||||
scanner.addIncludeFilter(new AssignableTypeFilter(MetaObjectHandler.class));
|
||||
|
||||
Set<String> handlerClasses = scanner
|
||||
.findCandidateComponents("com.xiaowang.jayucloud.salon.core")
|
||||
.stream()
|
||||
.map(beanDefinition -> beanDefinition.getBeanClassName())
|
||||
.collect(Collectors.toSet());
|
||||
|
||||
assertThat(handlerClasses)
|
||||
.containsExactly(MyMetaObjectHandler.class.getName());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user