Skip to content

Commit

Permalink
1 新增字段加密设置 ,只适用于 insert 和 find
Browse files Browse the repository at this point in the history
2  添加了 自动创建索引功能
3 部分代码测试
4  发布0.9.18
  • Loading branch information
HbnKing committed Mar 19, 2024
1 parent d747490 commit 847f565
Show file tree
Hide file tree
Showing 60 changed files with 1,012 additions and 435 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,22 @@ public MongoDatabase getDatabase() {


//所有扫描到的带有@Entity的类的集合
private Set<? extends Class<?>> initialEntitySet;
private Set<? extends Class<?>> initialEntitySet =new HashSet<>();

public void setInitialEntitySet(Set<? extends Class<?>> initialEntitySet) {
this.initialEntitySet = initialEntitySet;
// 非空才设置
if(initialEntitySet !=null){
this.initialEntitySet = initialEntitySet;
}

}


public Set<? extends Class<?>> getInitialEntitySet(){
return initialEntitySet;
}

//是否开启自动创建注解
//是否开启自动创建注解 ,默认值为false
private boolean autoIndexCreation = false;

public MongoMappingContext( MongoDatabase database ) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,6 @@ private void processPropertyAnnotations(final EntityModelBuilder<?> entityModelB
if(bsonType !=null){
propertyModelBuilder.bsonRepresentation(bsonType);
}

}

} else if(annotation instanceof Representation){
Expand All @@ -302,7 +301,7 @@ private void processPropertyAnnotations(final EntityModelBuilder<?> entityModelB

propertyModelBuilder.readName(null);

}else {
} else {
//todo
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ protected void decodeModel( BsonReader reader, DecoderContext decoderContext,
reader.readNull();
} else {
Object value = decoderContext.decodeWithChildContext(model.getCachedCodec(), reader);
instanceCreator.set(value, model);

instanceCreator.set(model.deserialize(value), model);
}
} catch (BsonInvalidOperationException e) {
mark.reset();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,10 @@ private void encodeValue( BsonWriter writer, EncoderContext encoderContext, Prop
writer.writeNull();
} else {
Codec<? super Object> cachedCodec = model.getCachedCodec();
encoderContext.encodeWithChildContext(cachedCodec, writer, propertyValue);
encoderContext.encodeWithChildContext(cachedCodec, writer, model.serialize(propertyValue));
}
}else{
// 既然不需要序列化 那么就直接丢弃
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@



import com.whaleal.mars.codecs.pojo.annotations.PropEncrypt;
import com.whaleal.mars.util.Assert;

import java.lang.annotation.Annotation;
Expand Down Expand Up @@ -175,7 +176,8 @@ static <T> void configureClassModelBuilder(final EntityModelBuilder<T> entityMod
}
}

reverse(annotations);

//reverse(annotations); 不需要翻转 ,翻转会出发顺序bug
entityModelBuilder.annotations(annotations);
entityModelBuilder.propertyNameToTypeParameterMap(propertyTypeParameterMap);

Expand Down Expand Up @@ -277,6 +279,19 @@ static <T> PropertyModelBuilder<T> createPropertyModelBuilder(final PropertyMeta
propertyMetadata.getTypeParameterMap()));
}

List< Annotation > writeAnnotations = propertyMetadata.getWriteAnnotations();

// 特殊注解 加密注解 及相关实现
for (Annotation annotation : writeAnnotations) {
if (PropEncrypt.class.equals(annotation.annotationType())) {
PropEncrypt propEncrypt = PropEncrypt.class.cast(annotation);

propertyModelBuilder.propertySerialization(new PropertyModelEncrptySerializationImpl(propEncrypt.value(),propEncrypt.enableDecrypt()));
}
}



return propertyModelBuilder;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ private void specializePropertyCodecs() {
Codec codec = getPropertyModelCodec(propertyModel);
if (codec != null) {
propertyModel.cachedCodec(codec);

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,14 @@ public boolean shouldSerialize(final T value) {
return propertySerialization.shouldSerialize(value);
}

public T serialize(final T value) {
return propertySerialization.serialize(value);
}

public T deserialize(final T value) {
return propertySerialization.deserialize(value);
}


public PropertyAccessor<T> getPropertyAccessor() {
return propertyAccessor;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Codec<T> getCodec() {
}


public PropertyModelBuilder<T> propertySerialization(final PropertySerialization<T> propertySerialization) {
public PropertyModelBuilder<T> propertySerialization(final PropertySerialization propertySerialization) {
this.propertySerialization = notNull(propertySerialization);
return this;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.whaleal.mars.codecs.pojo;

import com.whaleal.mars.util.AESUtil;

/**
* @author wh
*
* 针对字符串等格式 进行先关加密及解密的 实现类
*
* @see com.whaleal.mars.codecs.pojo.annotations.PropEncrypt ;
*
*/
class PropertyModelEncrptySerializationImpl implements PropertySerialization<String> {


private String sKey ;
private boolean enableDecrypt ;

PropertyModelEncrptySerializationImpl( String sKey ,boolean enableDecrypt) {
super();
this.sKey = sKey ;
this.enableDecrypt = enableDecrypt ;

}

@Override
public boolean shouldSerialize(final String value) {
return value != null;
}

@Override
public String serialize( String value ) {
try {
return AESUtil.encrypt(value , sKey);
}catch (Exception e){
e.printStackTrace();
return null ;
}

}

@Override
public String deserialize( String value ) {
if(enableDecrypt){
try {
return AESUtil.decrypt(value,sKey);
}catch (Exception e){
return null ;
}
}else {
return value ;
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,14 @@ class PropertyModelSerializationImpl<T> implements PropertySerialization<T> {
public boolean shouldSerialize(final T value) {
return value != null;
}

@Override
public T serialize( T value ) {
return value;
}

@Override
public T deserialize( T value ) {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,8 @@
public interface PropertySerialization<T> {

boolean shouldSerialize(T value);

T serialize(T value );

T deserialize(T value);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.whaleal.mars.codecs.pojo.annotations;

/**
* @author wh
*
* 主要用于字段级别的加密
*
* 当前使用 AES 加密解密
*
*
*
* 与 property 注解 冲突
*
*/
import java.lang.annotation.*;

@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface PropEncrypt {

/**
* 加密秘钥
* @return
*
*/
String value() default "0123456789abcdef";



/**
* 是否启用解密处理
*/
boolean enableDecrypt() default true;

}

Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ public long getPageFaults(){
return getOpLatenciesData("page_faults");
}

private Long getOpLatenciesData(String key) {
private Integer getOpLatenciesData(String key) {

return serverStatus.get("extra_info",Document.class).get(key,Long.class);
return serverStatus.get("extra_info",Document.class).get(key,Integer.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ public Document getReplication() {
MongoCollection<Document> opLog = db.getCollection("oplog.rs");
Document ol = db.runCommand(new Document("collStats", "oplog.rs"));

Long maxSize = ol.getLong("maxSize");
Integer maxSize = ol.getInteger("maxSize");
if (maxSize != null && maxSize > 0) {
//计算configured oplog size
Long logSizeMB = maxSize / (1024 * 1024);
Integer logSizeMB = maxSize / (1024 * 1024);
document.put("size", logSizeMB);

double size = ol.getInteger("size");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ protected DatastoreImpl( MongoClient mongoClient, String databaseName ) {
this.mongoClient = mongoClient;
this.defaultGridFSBucket = GridFSBuckets.create(super.database);
this.operations = new CollectionOperations();

automatic();
}

Expand All @@ -146,11 +147,20 @@ protected DatastoreImpl( MongoClient mongoClient, MongoMappingContext mapper ) {


private void automatic(){
for (Class< ? > entity : mapper.getInitialEntitySet()) {
for (Class< ? > clazz : mapper.getInitialEntitySet()) {
if (mapper.isAutoIndexCreation()) {
lock.lock();
// do CreateCollection First

try{
createCollection(clazz);
}catch (Exception e){
LOGGER.warn(" create collection error in the current database , Please ignore this message for other creations");
}


try {
ensureIndexes(entity);
ensureIndexes(clazz);
} finally {
lock.unlock();
}
Expand Down Expand Up @@ -2345,6 +2355,7 @@ private < T > T doTransaction( MarsSessionImpl marsSession, MarsTransaction< T >
if (marsSession == null) {
throw new IllegalStateException("No session could be found for the transaction.");
}

return marsSession.withTransaction(() -> body.execute(marsSession));
} catch (Exception e) {
LOGGER.error(e.getMessage());
Expand Down
Loading

0 comments on commit 847f565

Please sign in to comment.