improve wall filter

This commit is contained in:
wenshao
2013-04-05 22:32:54 +08:00
parent fe4c7a13aa
commit 707fe5214e
4 changed files with 37 additions and 35 deletions
@@ -232,6 +232,7 @@ public final class DruidStatManagerFacade {
return newMap;
}
@SuppressWarnings({ "rawtypes", "unchecked" })
private static List<Map<String, Object>> mergeNamedList(List listA, List listB) {
Map<String, Map<String, Object>> mapped = new HashMap<String, Map<String, Object>>();
for (Object item : (List) listA) {
@@ -92,6 +92,10 @@ public class WallContext {
public static void clearContext() {
contextLocal.remove();
}
public static void setContext(WallContext context) {
contextLocal.set(context);
}
public WallSqlStat getSqlState() {
return sqlState;
@@ -42,18 +42,20 @@ import com.alibaba.druid.wall.violation.SyntaxErrorViolation;
public class WallFilter extends FilterAdapter implements WallFilterMBean {
private final static Log LOG = LogFactory.getLog(WallFilter.class);
private final static Log LOG = LogFactory.getLog(WallFilter.class);
private boolean inited = false;
private boolean inited = false;
private WallProvider provider;
private WallProvider provider;
private String dbType;
private String dbType;
private WallConfig config;
private WallConfig config;
private volatile boolean logViolation = false;
private volatile boolean throwException = true;
private volatile boolean logViolation = false;
private volatile boolean throwException = true;
public final static String ATTR_CONTEXT = "wall.ctx";
@Override
public void init(DataSourceProxy dataSource) {
@@ -162,8 +164,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public void statement_addBatch(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
chain.statement_addBatch(statement, sql);
@@ -300,20 +301,23 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
WallContext originalContext = WallContext.current();
try {
WallContext.create(dbType);
sql = check(sql);
return chain.statement_execute(statement, sql);
} finally {
WallContext.clearContext();
if (originalContext != null) {
WallContext.setContext(originalContext);
}
}
}
@Override
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys)
throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_execute(statement, sql, autoGeneratedKeys);
@@ -325,8 +329,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, int columnIndexes[])
throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_execute(statement, sql, columnIndexes);
@@ -338,8 +341,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public boolean statement_execute(FilterChain chain, StatementProxy statement, String sql, String columnNames[])
throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_execute(statement, sql, columnNames);
@@ -351,8 +353,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public ResultSetProxy statement_executeQuery(FilterChain chain, StatementProxy statement, String sql)
throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_executeQuery(statement, sql);
@@ -363,8 +364,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql) throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_executeUpdate(statement, sql);
@@ -376,8 +376,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int autoGeneratedKeys)
throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_executeUpdate(statement, sql, autoGeneratedKeys);
@@ -389,8 +388,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
@Override
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, int columnIndexes[])
throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_executeUpdate(statement, sql, columnIndexes);
@@ -399,11 +397,16 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
}
}
private void setWallContext(StatementProxy statement) {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext context = WallContext.create(dbType);
statement.getAttributes().put(ATTR_CONTEXT, context);
}
@Override
public int statement_executeUpdate(FilterChain chain, StatementProxy statement, String sql, String columnNames[])
throws SQLException {
String dbType = statement.getConnectionProxy().getDirectDataSource().getDbType();
WallContext.createIfNotExists(dbType);
setWallContext(statement);
try {
sql = check(sql);
return chain.statement_executeUpdate(statement, sql, columnNames);
@@ -355,7 +355,7 @@ public abstract class WallProvider {
WallContext originalContext = WallContext.current();
try {
WallContext.create(dbType);
WallContext.createIfNotExists(dbType);
return checkInternal(sql);
} finally {
if (originalContext == null) {
@@ -364,17 +364,11 @@ public abstract class WallProvider {
}
}
public WallCheckResult checkInternal(String sql) {
private WallCheckResult checkInternal(String sql) {
checkCount.incrementAndGet();
WallContext context = WallContext.current();
if (context != null && !this.dbType.equals(context.getDbType())) {
WallContext.clearContext();
}
WallContext.createIfNotExists(this.dbType);
if (config.isDoPrivilegedAllow() && ispPivileged()) {
return new WallCheckResult();
}