diff --git a/pom.xml b/pom.xml index e75fd58..12a8d2a 100644 --- a/pom.xml +++ b/pom.xml @@ -7,7 +7,7 @@ spring-data-mongodb-encrypt jar spring-data-mongodb-encrypt - 2.2 + 2.3 High performance, per-field encryption for spring-data-mongodb https://github.com/agoston/spring-data-mongodb-encrypt diff --git a/src/main/java/com/bol/reflection/Node.java b/src/main/java/com/bol/reflection/Node.java index ec0be50..c9e9d2d 100644 --- a/src/main/java/com/bol/reflection/Node.java +++ b/src/main/java/com/bol/reflection/Node.java @@ -25,4 +25,6 @@ public enum Type { /** field is a sub-document, descend */ DOCUMENT } + + public static final Node EMPTY = new Node(null, null, null); } diff --git a/src/main/java/com/bol/secure/CachedEncryptionEventListener.java b/src/main/java/com/bol/secure/CachedEncryptionEventListener.java index 5d80bda..ba129dd 100644 --- a/src/main/java/com/bol/secure/CachedEncryptionEventListener.java +++ b/src/main/java/com/bol/secure/CachedEncryptionEventListener.java @@ -7,11 +7,11 @@ import org.bson.Document; import org.bson.types.ObjectId; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.mongodb.core.mapping.BasicMongoPersistentEntity; import org.springframework.data.mongodb.core.mapping.MongoMappingContext; import org.springframework.data.mongodb.core.mapping.event.AfterLoadEvent; import org.springframework.data.mongodb.core.mapping.event.BeforeSaveEvent; -import javax.annotation.PostConstruct; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -22,19 +22,20 @@ public class CachedEncryptionEventListener extends AbstractEncryptionEventListener { @Autowired MongoMappingContext mappingContext; - Map encrypted; + HashMap encrypted = new HashMap<>(); public CachedEncryptionEventListener(CryptVault cryptVault) { super(cryptVault); } - @PostConstruct - public void initReflection() { - encrypted = new HashMap<>(); + Node node(Class clazz) { + return encrypted.computeIfAbsent(clazz, c -> { + BasicMongoPersistentEntity entity = mappingContext.getPersistentEntity(c); + if (entity == null) return Node.EMPTY; - mappingContext.getPersistentEntities().forEach(entity -> { List children = processDocument(entity.getType()); - if (!children.isEmpty()) encrypted.put(entity.getType(), new Node("", children, Node.Type.ROOT)); + if (!children.isEmpty()) return new Node("", children, Node.Type.ROOT); + return Node.EMPTY; }); } @@ -42,8 +43,8 @@ public void initReflection() { public void onAfterLoad(AfterLoadEvent event) { Document document = event.getDocument(); - Node node = encrypted.get(event.getType()); - if (node == null) return; + Node node = node(event.getType()); + if (node == Node.EMPTY) return; try { cryptFields(document, node, new Decoder()); @@ -57,8 +58,8 @@ public void onAfterLoad(AfterLoadEvent event) { public void onBeforeSave(BeforeSaveEvent event) { Document document = event.getDocument(); - Node node = encrypted.get(event.getSource().getClass()); - if (node == null) return; + Node node = node(event.getSource().getClass()); + if (node == Node.EMPTY) return; try { cryptFields(document, node, new Encoder());