mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor
This commit is contained in:
@@ -0,0 +1,90 @@
|
||||
package com.alibaba.druid.filter.wall;
|
||||
|
||||
public class WallConfig {
|
||||
|
||||
private boolean selelctAllow = true;
|
||||
private boolean selectWhereAlwayTrueCheck = true;
|
||||
private boolean selectHavingAlwayTrueCheck = true;
|
||||
|
||||
private boolean deleteAllow = true;
|
||||
private boolean deleteWhereAlwayTrueCheck = true;
|
||||
|
||||
private boolean updateAllow = true;
|
||||
private boolean updateWhereAlayTrueCheck = true;
|
||||
|
||||
private boolean insertAllow = true;
|
||||
private boolean mergeAllow = true;
|
||||
|
||||
public boolean isSelelctAllow() {
|
||||
return selelctAllow;
|
||||
}
|
||||
|
||||
public void setSelelctAllow(boolean selelctAllow) {
|
||||
this.selelctAllow = selelctAllow;
|
||||
}
|
||||
|
||||
public boolean isSelectWhereAlwayTrueCheck() {
|
||||
return selectWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public void setSelectWhereAlwayTrueCheck(boolean selectWhereAlwayTrueCheck) {
|
||||
this.selectWhereAlwayTrueCheck = selectWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isSelectHavingAlwayTrueCheck() {
|
||||
return selectHavingAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public void setSelectHavingAlwayTrueCheck(boolean selectHavingAlwayTrueCheck) {
|
||||
this.selectHavingAlwayTrueCheck = selectHavingAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isDeleteAllow() {
|
||||
return deleteAllow;
|
||||
}
|
||||
|
||||
public void setDeleteAllow(boolean deleteAllow) {
|
||||
this.deleteAllow = deleteAllow;
|
||||
}
|
||||
|
||||
public boolean isDeleteWhereAlwayTrueCheck() {
|
||||
return deleteWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public void setDeleteWhereAlwayTrueCheck(boolean deleteWhereAlwayTrueCheck) {
|
||||
this.deleteWhereAlwayTrueCheck = deleteWhereAlwayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isUpdateAllow() {
|
||||
return updateAllow;
|
||||
}
|
||||
|
||||
public void setUpdateAllow(boolean updateAllow) {
|
||||
this.updateAllow = updateAllow;
|
||||
}
|
||||
|
||||
public boolean isUpdateWhereAlayTrueCheck() {
|
||||
return updateWhereAlayTrueCheck;
|
||||
}
|
||||
|
||||
public void setUpdateWhereAlayTrueCheck(boolean updateWhereAlayTrueCheck) {
|
||||
this.updateWhereAlayTrueCheck = updateWhereAlayTrueCheck;
|
||||
}
|
||||
|
||||
public boolean isInsertAllow() {
|
||||
return insertAllow;
|
||||
}
|
||||
|
||||
public void setInsertAllow(boolean insertAllow) {
|
||||
this.insertAllow = insertAllow;
|
||||
}
|
||||
|
||||
public boolean isMergeAllow() {
|
||||
return mergeAllow;
|
||||
}
|
||||
|
||||
public void setMergeAllow(boolean mergeAllow) {
|
||||
this.mergeAllow = mergeAllow;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -18,7 +18,7 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
public class WallFilter extends FilterAdapter {
|
||||
|
||||
private boolean inited = false;
|
||||
private boolean inited = false;
|
||||
|
||||
private WallProvider provider;
|
||||
|
||||
@@ -26,7 +26,7 @@ public class WallFilter extends FilterAdapter {
|
||||
|
||||
private boolean loadExtend;
|
||||
|
||||
private boolean checkSelectAlwayTrueCondition = true;
|
||||
private WallConfig config = new WallConfig();
|
||||
|
||||
@Override
|
||||
public void init(DataSourceProxy dataSource) {
|
||||
@@ -41,6 +41,14 @@ public class WallFilter extends FilterAdapter {
|
||||
return provider;
|
||||
}
|
||||
|
||||
public WallConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public void setConfig(WallConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public boolean isLoadDefault() {
|
||||
return loadDefault;
|
||||
}
|
||||
@@ -208,15 +216,13 @@ public class WallFilter extends FilterAdapter {
|
||||
public WallProvider createWallProvider(String dbType) {
|
||||
WallProvider provider;
|
||||
if (JdbcUtils.MYSQL.equals(dbType)) {
|
||||
provider = new MySqlWallProvider(this.loadDefault, this.loadExtend);
|
||||
provider = new MySqlWallProvider(config, this.loadDefault, this.loadExtend);
|
||||
} else if (JdbcUtils.ORACLE.equals(dbType)) {
|
||||
provider = new OracleWallProvider(this.loadDefault, this.loadExtend);
|
||||
provider = new OracleWallProvider(config, this.loadDefault, this.loadExtend);
|
||||
} else {
|
||||
throw new IllegalStateException("dbType not support : " + dbType);
|
||||
}
|
||||
|
||||
provider.setCheckSelectAlwayTrueCondition(this.checkSelectAlwayTrueCondition);
|
||||
|
||||
return provider;
|
||||
}
|
||||
|
||||
|
||||
@@ -14,28 +14,28 @@ import com.alibaba.druid.sql.parser.Token;
|
||||
public abstract class WallProvider {
|
||||
|
||||
// Dummy value to associate with an Object in the backing Map
|
||||
private static final Object PRESENT = new Object();
|
||||
private static final Object PRESENT = new Object();
|
||||
|
||||
private LinkedHashMap<String, Object> whiteList;
|
||||
|
||||
private int whileListMaxSize = 1024;
|
||||
private int whileListMaxSize = 1024;
|
||||
|
||||
private int whiteSqlMaxLength = 1024; // 1k
|
||||
private int whiteSqlMaxLength = 1024; // 1k
|
||||
|
||||
protected final Set<String> permitFunctions = new HashSet<String>();
|
||||
protected final Set<String> permitTables = new HashSet<String>();
|
||||
protected final Set<String> permitSchemas = new HashSet<String>();
|
||||
protected final Set<String> permitNames = new HashSet<String>();
|
||||
protected final Set<String> permitObjects = new HashSet<String>();
|
||||
protected final Set<String> permitFunctions = new HashSet<String>();
|
||||
protected final Set<String> permitTables = new HashSet<String>();
|
||||
protected final Set<String> permitSchemas = new HashSet<String>();
|
||||
protected final Set<String> permitNames = new HashSet<String>();
|
||||
protected final Set<String> permitObjects = new HashSet<String>();
|
||||
|
||||
private boolean checkSelectAlwayTrueCondition = true;
|
||||
private final WallConfig config;
|
||||
|
||||
public boolean isCheckSelectAlwayTrueCondition() {
|
||||
return checkSelectAlwayTrueCondition;
|
||||
public WallProvider(WallConfig config){
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
public void setCheckSelectAlwayTrueCondition(boolean checkSelectAlwayTrueCondition) {
|
||||
this.checkSelectAlwayTrueCondition = checkSelectAlwayTrueCondition;
|
||||
public WallConfig getConfig() {
|
||||
return config;
|
||||
}
|
||||
|
||||
public synchronized void addWhiteSql(String sql) {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.filter.wall.spi;
|
||||
|
||||
import com.alibaba.druid.filter.wall.WallConfig;
|
||||
import com.alibaba.druid.filter.wall.WallProvider;
|
||||
import com.alibaba.druid.filter.wall.WallVisitor;
|
||||
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
|
||||
@@ -7,11 +8,17 @@ import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
|
||||
public class MySqlWallProvider extends WallProvider {
|
||||
|
||||
public MySqlWallProvider(){
|
||||
this(true, true);
|
||||
public MySqlWallProvider() {
|
||||
this(new WallConfig());
|
||||
}
|
||||
|
||||
public MySqlWallProvider(boolean loadDefault, boolean loadExtend){
|
||||
public MySqlWallProvider(WallConfig config){
|
||||
this(config, true, true);
|
||||
}
|
||||
|
||||
public MySqlWallProvider(WallConfig config, boolean loadDefault, boolean loadExtend){
|
||||
super(config);
|
||||
|
||||
if (loadDefault) {
|
||||
loadDefault();
|
||||
}
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.alibaba.druid.filter.wall.spi;
|
||||
|
||||
import com.alibaba.druid.filter.wall.WallConfig;
|
||||
import com.alibaba.druid.filter.wall.WallProvider;
|
||||
import com.alibaba.druid.filter.wall.WallVisitor;
|
||||
import com.alibaba.druid.sql.dialect.oracle.parser.OracleStatementParser;
|
||||
@@ -8,10 +9,16 @@ import com.alibaba.druid.sql.parser.SQLStatementParser;
|
||||
public class OracleWallProvider extends WallProvider {
|
||||
|
||||
public OracleWallProvider(){
|
||||
this(true, true);
|
||||
this(new WallConfig());
|
||||
}
|
||||
|
||||
public OracleWallProvider(boolean loadDefault, boolean loadExtend){
|
||||
public OracleWallProvider(WallConfig config){
|
||||
this(config, true, true);
|
||||
}
|
||||
|
||||
public OracleWallProvider(WallConfig config, boolean loadDefault, boolean loadExtend){
|
||||
super(config);
|
||||
|
||||
if (loadDefault) {
|
||||
loadDefault();
|
||||
}
|
||||
|
||||
@@ -58,17 +58,19 @@ public class WallVisitorUtils {
|
||||
if (x == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
WallProvider provider = visitor.getProvider();
|
||||
if (!provider.isCheckSelectAlwayTrueCondition()) {
|
||||
if (!provider.getConfig().isSelectHavingAlwayTrueCheck()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (Boolean.TRUE == getValue(x)) {
|
||||
if (x instanceof SQLBinaryOpExpr) {
|
||||
SQLBinaryOpExpr binaryOpExpr = (SQLBinaryOpExpr) x;
|
||||
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality || binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual) {
|
||||
if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr && binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
|
||||
if (binaryOpExpr.getOperator() == SQLBinaryOperator.Equality
|
||||
|| binaryOpExpr.getOperator() == SQLBinaryOperator.NotEqual) {
|
||||
if (binaryOpExpr.getLeft() instanceof SQLIntegerExpr
|
||||
&& binaryOpExpr.getRight() instanceof SQLIntegerExpr) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user