mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improve WallFilter
This commit is contained in:
@@ -1,19 +1,46 @@
|
||||
package com.alibaba.druid.wall;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.concurrent.atomic.AtomicLongFieldUpdater;
|
||||
|
||||
public class WallDenyStat {
|
||||
|
||||
private volatile long denyCount;
|
||||
|
||||
final static AtomicLongFieldUpdater<WallDenyStat> denyCountUpdater = AtomicLongFieldUpdater.newUpdater(WallDenyStat.class,
|
||||
"denyCount");
|
||||
private volatile long lastDenyTimeMillis;
|
||||
|
||||
private volatile long resetCount;
|
||||
|
||||
final static AtomicLongFieldUpdater<WallDenyStat> denyCountUpdater = AtomicLongFieldUpdater.newUpdater(WallDenyStat.class,
|
||||
"denyCount");
|
||||
|
||||
final static AtomicLongFieldUpdater<WallDenyStat> resetCountUpdater = AtomicLongFieldUpdater.newUpdater(WallDenyStat.class,
|
||||
"resetCount");
|
||||
|
||||
public long incrementAndGetDenyCount() {
|
||||
lastDenyTimeMillis = System.currentTimeMillis();
|
||||
return denyCountUpdater.incrementAndGet(this);
|
||||
}
|
||||
|
||||
public long getDenyCount() {
|
||||
return denyCount;
|
||||
}
|
||||
|
||||
public Date getLastDenyTime() {
|
||||
if (lastDenyTimeMillis <= 0) {
|
||||
return null;
|
||||
}
|
||||
return new Date(lastDenyTimeMillis);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
lastDenyTimeMillis = 0;
|
||||
denyCount = 0;
|
||||
resetCountUpdater.incrementAndGet(this);
|
||||
}
|
||||
|
||||
public long getResetCount() {
|
||||
return this.resetCount;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
import com.alibaba.druid.wall.spi.OracleWallProvider;
|
||||
import com.alibaba.druid.wall.spi.SQLServerProvider;
|
||||
import com.alibaba.druid.wall.spi.SQLServerWallProvider;
|
||||
import com.alibaba.druid.wall.violation.SyntaxErrorViolation;
|
||||
|
||||
public class WallFilter extends FilterAdapter implements WallFilterMBean {
|
||||
@@ -86,10 +86,10 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
|
||||
provider = new OracleWallProvider(config);
|
||||
} else if (JdbcUtils.SQL_SERVER.equals(dbType) || JdbcUtils.JTDS.equals(dbType)) {
|
||||
if (config == null) {
|
||||
config = new WallConfig(SQLServerProvider.DEFAULT_CONFIG_DIR);
|
||||
config = new WallConfig(SQLServerWallProvider.DEFAULT_CONFIG_DIR);
|
||||
}
|
||||
|
||||
provider = new SQLServerProvider(config);
|
||||
provider = new SQLServerWallProvider(config);
|
||||
} else {
|
||||
throw new IllegalStateException("dbType not support : " + dbType + ", url " + dataSource.getUrl());
|
||||
}
|
||||
|
||||
@@ -24,6 +24,7 @@ import java.util.concurrent.ConcurrentMap;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.parser.NotAllowCommentException;
|
||||
import com.alibaba.druid.sql.parser.ParserException;
|
||||
import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
import com.alibaba.druid.sql.parser.Token;
|
||||
@@ -49,7 +50,9 @@ public abstract class WallProvider {
|
||||
|
||||
private final ConcurrentMap<String, WallDenyStat> deniedTables = new ConcurrentHashMap<String, WallDenyStat>();
|
||||
private final ConcurrentMap<String, WallDenyStat> deniedFunctions = new ConcurrentHashMap<String, WallDenyStat>();
|
||||
private final ConcurrentMap<String, WallDenyStat> deniedSchemas = new ConcurrentHashMap<String, WallDenyStat>();
|
||||
private final ConcurrentMap<String, WallDenyStat> deniedSchemas = new ConcurrentHashMap<String, WallDenyStat>();
|
||||
|
||||
public final WallDenyStat commentDeniedStat = new WallDenyStat();
|
||||
|
||||
public WallProvider(WallConfig config){
|
||||
this.config = config;
|
||||
@@ -129,6 +132,10 @@ public abstract class WallProvider {
|
||||
return result.getViolations().isEmpty();
|
||||
}
|
||||
|
||||
public void incrementCommentDeniedCount() {
|
||||
this.commentDeniedStat.incrementAndGetDenyCount();
|
||||
}
|
||||
|
||||
public boolean checkDenyFunction(String functionName) {
|
||||
if (functionName == null) {
|
||||
return true;
|
||||
@@ -147,7 +154,7 @@ public abstract class WallProvider {
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
public boolean checkDenySchema(String schemaName) {
|
||||
if (schemaName == null) {
|
||||
return true;
|
||||
@@ -213,7 +220,7 @@ public abstract class WallProvider {
|
||||
String formName = WallVisitorUtils.form(tableName);
|
||||
return deniedTables.get(formName);
|
||||
}
|
||||
|
||||
|
||||
public WallDenyStat getDenniedSchemaStat(String schemaName) {
|
||||
if (schemaName == null) {
|
||||
return null;
|
||||
@@ -223,6 +230,10 @@ public abstract class WallProvider {
|
||||
return deniedSchemas.get(formName);
|
||||
}
|
||||
|
||||
public WallDenyStat getCommentDenyStat() {
|
||||
return this.commentDeniedStat;
|
||||
}
|
||||
|
||||
public WallCheckResult check(String sql) {
|
||||
WallCheckResult result = new WallCheckResult();
|
||||
|
||||
@@ -245,6 +256,10 @@ public abstract class WallProvider {
|
||||
|
||||
try {
|
||||
parser.parseStatementList(result.getStatementList());
|
||||
} catch (NotAllowCommentException e) {
|
||||
result.getViolations().add(new SyntaxErrorViolation(e, sql));
|
||||
incrementCommentDeniedCount();
|
||||
return result;
|
||||
} catch (Exception e) {
|
||||
result.getViolations().add(new SyntaxErrorViolation(e, sql));
|
||||
return result;
|
||||
|
||||
@@ -17,7 +17,7 @@ package com.alibaba.druid.wall;
|
||||
|
||||
import com.alibaba.druid.wall.spi.MySqlWallProvider;
|
||||
import com.alibaba.druid.wall.spi.OracleWallProvider;
|
||||
import com.alibaba.druid.wall.spi.SQLServerProvider;
|
||||
import com.alibaba.druid.wall.spi.SQLServerWallProvider;
|
||||
|
||||
public class WallUtils {
|
||||
|
||||
@@ -42,12 +42,12 @@ public class WallUtils {
|
||||
}
|
||||
|
||||
public static boolean isValidateSqlServer(String sql) {
|
||||
SQLServerProvider provider = new SQLServerProvider();
|
||||
SQLServerWallProvider provider = new SQLServerWallProvider();
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
|
||||
public static boolean isValidateSqlServer(String sql, WallConfig config) {
|
||||
SQLServerProvider provider = new SQLServerProvider(config);
|
||||
SQLServerWallProvider provider = new SQLServerWallProvider(config);
|
||||
return provider.checkValid(sql);
|
||||
}
|
||||
}
|
||||
|
||||
+3
-3
@@ -30,18 +30,18 @@ import com.alibaba.druid.wall.WallVisitor;
|
||||
* @version 1.0, 2012-3-17
|
||||
* @see
|
||||
*/
|
||||
public class SQLServerProvider extends WallProvider {
|
||||
public class SQLServerWallProvider extends WallProvider {
|
||||
|
||||
public final static String DEFAULT_CONFIG_DIR = "META-INF/druid/wall/sqlserver";
|
||||
|
||||
/**
|
||||
* @param config
|
||||
*/
|
||||
public SQLServerProvider(){
|
||||
public SQLServerWallProvider(){
|
||||
this(new WallConfig(DEFAULT_CONFIG_DIR));
|
||||
}
|
||||
|
||||
public SQLServerProvider(WallConfig config){
|
||||
public SQLServerWallProvider(WallConfig config){
|
||||
super(config);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user