Search in sources :

Example 1 with FieldSecureConfig

use of org.sagacity.sqltoy.config.model.FieldSecureConfig in project sagacity-sqltoy by chenrenfei.

the class DialectUtils method getSecureReflectHandler.

/**
 * @TODO 对字段值进行加密
 * @param preHandler
 * @param fieldsSecureProvider
 * @param desensitizeProvider
 * @param secureFields
 * @return
 */
public static ReflectPropsHandler getSecureReflectHandler(final ReflectPropsHandler preHandler, final FieldsSecureProvider fieldsSecureProvider, final DesensitizeProvider desensitizeProvider, List<FieldSecureConfig> secureFields) {
    if (fieldsSecureProvider == null || secureFields == null || secureFields.isEmpty()) {
        return preHandler;
    }
    ReflectPropsHandler handler = new ReflectPropsHandler() {

        @Override
        public void process() {
            if (preHandler != null) {
                preHandler.setPropertyIndexMap(this.getPropertyIndexMap());
                preHandler.setRowIndex(this.getRowIndex());
                preHandler.setRowData(this.getRowData());
                preHandler.process();
            }
            Object value;
            String contents;
            String field;
            String sourceField;
            // 加密操作
            for (FieldSecureConfig config : secureFields) {
                field = config.getField();
                sourceField = config.getSourceField();
                // 安全脱敏便于检索的字段,优先依据其来源加密字段
                if (StringUtil.isNotBlank(sourceField)) {
                    value = this.getValue(sourceField);
                } else {
                    value = this.getValue(field);
                }
                if (value != null) {
                    contents = value.toString();
                    if (!contents.equals("")) {
                        // 加密
                        if (config.getSecureType().equals(SecureType.ENCRYPT)) {
                            this.setValue(field, fieldsSecureProvider.encrypt(contents));
                        } else // 脱敏
                        {
                            this.setValue(field, desensitizeProvider.desensitize(contents, config.getMask()));
                        }
                    }
                }
            }
        }
    };
    return handler;
}
Also used : FieldSecureConfig(org.sagacity.sqltoy.config.model.FieldSecureConfig) ReflectPropsHandler(org.sagacity.sqltoy.callback.ReflectPropsHandler)

Example 2 with FieldSecureConfig

use of org.sagacity.sqltoy.config.model.FieldSecureConfig in project sagacity-sqltoy by chenrenfei.

the class EntityManager method parseSecureConfig.

/**
 * @todo 解析加解密配置
 * @param entityMeta
 * @param entityClass
 */
private void parseSecureConfig(EntityMeta entityMeta, Class entityClass) {
    Class classType = entityClass;
    SecureConfig secureConfig = null;
    // 增加递归对父类检测
    while (classType != null && !classType.equals(Object.class)) {
        secureConfig = (SecureConfig) classType.getAnnotation(SecureConfig.class);
        if (secureConfig != null) {
            break;
        }
        classType = classType.getSuperclass();
    }
    // 不存在加解密配置
    if (secureConfig == null) {
        return;
    }
    Secure[] secures = secureConfig.secures();
    if (secures != null && secures.length > 0) {
        IgnoreCaseSet secureColumns = new IgnoreCaseSet();
        String field;
        FieldMeta fieldMeta;
        for (Secure secure : secures) {
            field = secure.field();
            fieldMeta = entityMeta.getFieldMeta(field);
            if (fieldMeta != null) {
                // 加密
                if (secure.secureType().equals(SecureType.ENCRYPT)) {
                    secureColumns.add(fieldMeta.getColumnName());
                    entityMeta.addSecureField(new FieldSecureConfig(field, SecureType.ENCRYPT, null, null, 0, 0, 0));
                } else {
                    // 依据加密字段进行脱敏保存
                    entityMeta.addSecureField(new FieldSecureConfig(field, secure.secureType(), secure.sourceField(), secure.maskCode(), secure.headSize(), secure.tailSize(), secure.maskRate()));
                }
            }
        }
        // 加密字段
        if (!secureColumns.isEmpty()) {
            entityMeta.setSecureColumns(secureColumns);
        }
    }
}
Also used : FieldSecureConfig(org.sagacity.sqltoy.config.model.FieldSecureConfig) Secure(org.sagacity.sqltoy.config.annotation.Secure) FieldMeta(org.sagacity.sqltoy.config.model.FieldMeta) SecureConfig(org.sagacity.sqltoy.config.annotation.SecureConfig) FieldSecureConfig(org.sagacity.sqltoy.config.model.FieldSecureConfig) IgnoreCaseSet(org.sagacity.sqltoy.model.IgnoreCaseSet)

Aggregations

FieldSecureConfig (org.sagacity.sqltoy.config.model.FieldSecureConfig)2 ReflectPropsHandler (org.sagacity.sqltoy.callback.ReflectPropsHandler)1 Secure (org.sagacity.sqltoy.config.annotation.Secure)1 SecureConfig (org.sagacity.sqltoy.config.annotation.SecureConfig)1 FieldMeta (org.sagacity.sqltoy.config.model.FieldMeta)1 IgnoreCaseSet (org.sagacity.sqltoy.model.IgnoreCaseSet)1