mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -59,6 +59,8 @@ import com.alibaba.druid.proxy.jdbc.StatementProxy;
|
||||
public interface Filter {
|
||||
|
||||
void init(DataSourceProxy dataSource);
|
||||
|
||||
void destory();
|
||||
|
||||
ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException;
|
||||
|
||||
|
||||
@@ -67,6 +67,11 @@ public abstract class FilterAdapter extends NotificationBroadcasterSupport imple
|
||||
public void init(DataSourceProxy dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void destory() {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Array callableStatement_getArray(FilterChain chain, CallableStatementProxy statement, int parameterIndex)
|
||||
|
||||
@@ -123,7 +123,7 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void init(DataSourceProxy dataSource) {
|
||||
public synchronized void init(DataSourceProxy dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
|
||||
ConcurrentMap<String, JdbcDataSourceStat> dataSourceStats = JdbcStatManager.getInstance().getDataSources();
|
||||
@@ -136,6 +136,19 @@ public class StatFilter extends FilterEventAdapter implements StatFilterMBean {
|
||||
}
|
||||
this.dataSourceStat = stat;
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void destory() {
|
||||
if (dataSource == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
ConcurrentMap<String, JdbcDataSourceStat> dataSourceStats = JdbcStatManager.getInstance().getDataSources();
|
||||
String url = dataSource.getUrl();
|
||||
dataSourceStats.remove(url);
|
||||
|
||||
dataSource = null;
|
||||
}
|
||||
|
||||
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
|
||||
ConnectionProxy connection = null;
|
||||
|
||||
@@ -32,8 +32,6 @@ public class WallConfig {
|
||||
protected final Set<String> permitNames = new HashSet<String>();
|
||||
protected final Set<String> permitObjects = new HashSet<String>();
|
||||
|
||||
|
||||
|
||||
public void loadDefault(String dir) {
|
||||
if (dir.endsWith("/")) {
|
||||
dir = dir.substring(0, dir.length() - 1);
|
||||
@@ -49,7 +47,7 @@ public class WallConfig {
|
||||
if (dir.endsWith("/")) {
|
||||
dir = dir.substring(0, dir.length() - 1);
|
||||
}
|
||||
|
||||
|
||||
loadResource(getPermitNames(), dir + "permit-name.txt");
|
||||
loadResource(getPermitSchemas(), dir + "permit-schema.txt");
|
||||
loadResource(getPermitFunctions(), dir + "permit-function.txt");
|
||||
@@ -172,7 +170,7 @@ public class WallConfig {
|
||||
public Set<String> getPermitObjects() {
|
||||
return permitObjects;
|
||||
}
|
||||
|
||||
|
||||
public boolean isPermitObjects(String name) {
|
||||
name = WallVisitorUtils.form(name);
|
||||
return permitObjects.contains(name);
|
||||
@@ -182,7 +180,7 @@ public class WallConfig {
|
||||
name = WallVisitorUtils.form(name);
|
||||
return this.permitSchemas.contains(name);
|
||||
}
|
||||
|
||||
|
||||
public boolean isPermitFunction(String name) {
|
||||
name = WallVisitorUtils.form(name);
|
||||
return this.permitFunctions.contains(name);
|
||||
|
||||
@@ -8,6 +8,8 @@ import com.alibaba.druid.filter.FilterAdapter;
|
||||
import com.alibaba.druid.filter.FilterChain;
|
||||
import com.alibaba.druid.filter.wall.spi.MySqlWallProvider;
|
||||
import com.alibaba.druid.filter.wall.spi.OracleWallProvider;
|
||||
import com.alibaba.druid.logging.Log;
|
||||
import com.alibaba.druid.logging.LogFactory;
|
||||
import com.alibaba.druid.proxy.jdbc.CallableStatementProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
|
||||
import com.alibaba.druid.proxy.jdbc.DataSourceProxy;
|
||||
@@ -18,15 +20,19 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class WallFilter extends FilterAdapter {
|
||||
|
||||
private boolean inited = false;
|
||||
private final static Log LOG = LogFactory.getLog(WallFilter.class);
|
||||
|
||||
private WallProvider provider;
|
||||
private boolean inited = false;
|
||||
|
||||
private boolean loadDefault;
|
||||
private WallProvider provider;
|
||||
|
||||
private boolean loadExtend;
|
||||
private boolean loadDefault;
|
||||
|
||||
private WallConfig config = new WallConfig();
|
||||
private boolean loadExtend;
|
||||
|
||||
private boolean logViolation = false;
|
||||
|
||||
private WallConfig config = new WallConfig();
|
||||
|
||||
@Override
|
||||
public void init(DataSourceProxy dataSource) {
|
||||
@@ -37,6 +43,14 @@ public class WallFilter extends FilterAdapter {
|
||||
this.inited = true;
|
||||
}
|
||||
|
||||
public boolean isLogViolation() {
|
||||
return logViolation;
|
||||
}
|
||||
|
||||
public void setLogViolation(boolean logViolation) {
|
||||
this.logViolation = logViolation;
|
||||
}
|
||||
|
||||
public WallProvider getProvider() {
|
||||
return provider;
|
||||
}
|
||||
@@ -210,7 +224,14 @@ public class WallFilter extends FilterAdapter {
|
||||
}
|
||||
|
||||
public void check(ConnectionProxy connection, String sql) throws SQLException {
|
||||
provider.check(sql, true);
|
||||
try {
|
||||
provider.check(sql, true);
|
||||
} catch (WallRuntimeException e) {
|
||||
if (isLogViolation()) {
|
||||
LOG.error("sql injection violation : " + sql, e);
|
||||
}
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
|
||||
public WallProvider createWallProvider(String dbType) {
|
||||
|
||||
@@ -687,6 +687,10 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
notEmptySignalCount++;
|
||||
|
||||
this.closed = true;
|
||||
|
||||
for (Filter filter : filters) {
|
||||
filter.destory();
|
||||
}
|
||||
} finally {
|
||||
lock.unlock();
|
||||
}
|
||||
|
||||
@@ -671,6 +671,10 @@ public class SchemaStatVisitor extends SQLASTVisitorAdapter {
|
||||
public boolean visit(SQLExprTableSource x) {
|
||||
if (isSimpleExprTableSource(x)) {
|
||||
String ident = x.getExpr().toString();
|
||||
|
||||
if (variants.containsKey(ident)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (subQueryMap.containsKey(ident)) {
|
||||
return false;
|
||||
|
||||
@@ -391,7 +391,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
|
||||
|
||||
// 25 -
|
||||
new ArrayType<Long>(SimpleType.LONG, true), //
|
||||
new ArrayType<Long>(SimpleType.STRING, true), //
|
||||
new ArrayType<String>(SimpleType.STRING, false), //
|
||||
};
|
||||
|
||||
String[] indexNames = {
|
||||
@@ -490,6 +490,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
|
||||
map.put("InTransactionCount", getInTransactionCount());
|
||||
|
||||
map.put("Histogram", this.histogram.toArray());
|
||||
|
||||
map.put("LastSlowParameters", lastSlowParameters);
|
||||
|
||||
return map;
|
||||
|
||||
Reference in New Issue
Block a user