Commit 6b608d56 by 郑冰晶

优化异常告警

parent 444c3842
......@@ -42,7 +42,8 @@ public class DataSourceSecurityAutoConfiguration implements ApplicationContextAw
public void afterSingletonsInstantiated() {
log.debug(">>>>>>>>>>>>>"+JSON.toJSONString(properties));
if(properties == null || properties.getSecurityRules() == null || properties.getSecurityRules().size() == 0){
throw new RuntimeException("DataSourceSecurityProperties is null!");
// throw new RuntimeException("DataSourceSecurityProperties is null!");
return;
}
Map<String,Map<String,Map<String,String>>> dbRules = properties.getSecurityRules().get("securityRules");
......
......@@ -9,7 +9,7 @@ import com.secoo.mall.datasource.security.algorithm.encrypt.EncryptAlgorithm;
import com.secoo.mall.datasource.security.config.DataSourceSecurityProperties;
import com.secoo.mall.datasource.security.constant.PropertyProviderType;
import com.secoo.mall.datasource.security.constant.SymbolConstants;
import com.secoo.mall.datasource.security.exception.SecurityException;
import com.secoo.mall.datasource.security.exception.SecurityBizException;
import com.secoo.mall.datasource.security.factory.EncryptAlgorithmFactory;
import com.secoo.mall.datasource.security.rule.ColumnRule;
import com.secoo.mall.datasource.security.rule.DbRule;
......@@ -41,7 +41,7 @@ public class ApolloPropertyProviderAlgorithm implements PropertyProviderAlgorith
Config appConfig = ConfigService.getConfig(DATASOURCE_SECURITY_APOLLO_NAMESPACE);
Set<String> propertyNames = appConfig.getPropertyNames();
if (CollectionUtil.isEmpty(propertyNames)) {
throw new SecurityException("!!! Can not find apollo security rules !!!");
throw new SecurityBizException("!!! Can not find apollo security rules !!!");
}
//用数据源名字为key,进行属性分组
List<String[]> propertyNameSections = propertyNames.stream()
......@@ -135,7 +135,7 @@ public class ApolloPropertyProviderAlgorithm implements PropertyProviderAlgorith
}
dbRules.add(dbRule);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new SecurityException("!!! Load security rule from apollo error !!!",e);
throw new SecurityBizException("!!! Load security rule from apollo error !!!",e);
}
}
......
package com.secoo.mall.datasource.security.exception;
public class SecurityException extends RuntimeException{
public class SecurityBizException extends RuntimeException{
public SecurityException(String message) {
public SecurityBizException(String message) {
super(message);
}
public SecurityException(Throwable e) {
public SecurityBizException(Throwable e) {
super(e);
}
public SecurityException(String message, Throwable e) {
public SecurityBizException(String message, Throwable e) {
super(message, e);
}
}
......@@ -18,6 +18,8 @@ import com.alibaba.druid.sql.dialect.mysql.ast.statement.MySqlUpdateStatement;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.Utils;
import com.secoo.mall.datasource.security.algorithm.encrypt.AESEncryptAlgorithm;
import com.secoo.mall.datasource.security.exception.SecurityBizException;
import com.secoo.mall.datasource.security.rule.ColumnRule;
import com.secoo.mall.datasource.security.rule.DbRule;
import com.secoo.mall.datasource.security.rule.TableRule;
......@@ -277,9 +279,10 @@ public class SecurityFilter extends FilterEventAdapter {
String plainText = columnRule.getEncryptAlgorithm().decrypt(cipherText);
log.debug("字段解密:columnRule={},cipherText={},plainText={}", columnRule, cipherText, plainText);
return plainText;
} catch (GeneralSecurityException e) {
} catch (Exception e) {
String errorMsg = "字段解密异常:columnRule="+columnRule.toString()+",cipherText="+cipherText;
throw new SecurityException(errorMsg,e);
log.error(errorMsg);
throw new SecurityBizException(errorMsg,e);
}
}
......@@ -457,9 +460,10 @@ public class SecurityFilter extends FilterEventAdapter {
String cipherText = columnRule.getEncryptAlgorithm().encrypt(plainText);
preparedStatement.setObject(index + 1,cipherText);
log.debug("字段加密:columnRule={},plainText={},cipherText={}", columnRule, plainText, cipherText);
} catch (SQLException | GeneralSecurityException e) {
} catch (Exception e) {
String errorMsg = "字段加密异常:columnRule="+columnRule+",plainText="+plainText;
throw new SecurityException(errorMsg,e);
log.error(errorMsg);
throw new SecurityBizException(errorMsg,e);
}
}
......@@ -516,8 +520,17 @@ public class SecurityFilter extends FilterEventAdapter {
SQLUpdateStatement _stmt = (SQLUpdateStatement) stmt;
System.out.println(visitor.getColumns());
System.out.println(_stmt.getItems());
SQLExpr sqlExpr = SQLUtils.toSQLExpr(sql,"sql");
System.out.println(sqlExpr);
}
String cipherText = "定时任务";
AESEncryptAlgorithm aesEncryptAlgorithm = new AESEncryptAlgorithm();
aesEncryptAlgorithm.getProps().setProperty(AESEncryptAlgorithm.ENCRYPT_KEY,"123");
aesEncryptAlgorithm.init();
try {
aesEncryptAlgorithm.decrypt(cipherText);
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
System.out.println("");
}
}
......@@ -4,7 +4,7 @@ import com.alibaba.druid.filter.AutoLoad;
import com.secoo.mall.datasource.security.algorithm.property.PropertyProviderAlgorithm;
import com.secoo.mall.datasource.security.config.DataSourceSecurityProperties;
import com.secoo.mall.datasource.security.constant.PropertyProviderType;
import com.secoo.mall.datasource.security.exception.SecurityException;
import com.secoo.mall.datasource.security.exception.SecurityBizException;
import com.secoo.mall.datasource.security.factory.PropertyProviderAlgorithmFactory;
import com.secoo.mall.datasource.security.rule.DbRule;
import org.apache.commons.lang3.StringUtils;
......@@ -44,9 +44,10 @@ public class SecurityFilterContext {
PropertyProviderAlgorithm propertyProviderAlgorithm = PropertyProviderAlgorithmFactory.getObject(propertyProvider,new Properties());
DataSourceSecurityProperties dataSourceSecurityProperties = propertyProviderAlgorithm.load();
if(dataSourceSecurityProperties == null){
// log.warn("!!! Can not find security rules !!!");
throw new SecurityException("!!! Can not find security rules !!!");
log.error("!!! Can not find security rules !!!");
throw new SecurityBizException("!!! Can not find security rules !!!");
}
this.dbRules = dataSourceSecurityProperties.getRules();
}
private Set<DbRule> dbRules;
......
package com.secoo.mall.datasource.security.spi;
import com.secoo.mall.datasource.security.exception.SecurityBizException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
......@@ -54,7 +56,7 @@ public class SecurityServiceLoader {
try {
return clazz.newInstance();
} catch (final InstantiationException | IllegalAccessException ex) {
throw new SecurityException(String.format("Can not find public default constructor for SPI class `%s`", clazz.getName()), ex);
throw new SecurityBizException(String.format("Can not find public default constructor for SPI class `%s`", clazz.getName()), ex);
}
}
}
package com.secoo.mall.datasource.security.spi;
import com.secoo.mall.datasource.security.exception.SecurityBizException;
import java.util.Optional;
import java.util.Properties;
......@@ -49,7 +51,7 @@ public final class TypedSPIRegistry {
if (result.isPresent()) {
return result.get();
}
throw new SecurityException(String.format("No implementation class load from SPI `%s` with type `%s`.", typedSPIClass.getName(), type));
throw new SecurityBizException(String.format("No implementation class load from SPI `%s` with type `%s`.", typedSPIClass.getName(), type));
}
/**
......@@ -64,7 +66,7 @@ public final class TypedSPIRegistry {
if (serviceInstance.isPresent()) {
return serviceInstance.get();
}
throw new SecurityException(String.format("No implementation class load from SPI `%s`.", typedSPIClass.getName()));
throw new SecurityBizException(String.format("No implementation class load from SPI `%s`.", typedSPIClass.getName()));
}
private static <T extends TypedSPI> void convertPropertiesValueType(final Properties props, final T service) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment