diff --git a/src/main/java/com/alibaba/druid/wall/WallConfig.java b/src/main/java/com/alibaba/druid/wall/WallConfig.java index c215c8c44a..2078222e8a 100644 --- a/src/main/java/com/alibaba/druid/wall/WallConfig.java +++ b/src/main/java/com/alibaba/druid/wall/WallConfig.java @@ -17,9 +17,11 @@ package com.alibaba.druid.wall; import com.alibaba.druid.wall.spi.WallVisitorUtils; +import java.util.Properties; import java.util.Set; import java.util.concurrent.ConcurrentSkipListSet; +import static com.alibaba.druid.util.Utils.getBoolean; import static com.alibaba.druid.wall.spi.WallVisitorUtils.loadResource; public class WallConfig implements WallConfigMBean { @@ -124,7 +126,7 @@ public class WallConfig implements WallConfigMBean { private int insertValuesCheckSize = 3; public WallConfig(){ - + this.configFromProperties(System.getProperties()); } public boolean isCaseConditionConstAllow() { @@ -787,4 +789,36 @@ public class WallConfig implements WallConfigMBean { this.insertValuesCheckSize = insertValuesCheckSize; } + public void configFromProperties(Properties properties) { + { + String tenantColumn = properties.getProperty("druid.wall.tenantColumn"); + if (tenantColumn != null) { + this.setTenantColumn(tenantColumn); + } + } + { + Boolean selelctAllow = getBoolean(properties, "druid.wall.selelctAllow"); + if (selelctAllow != null) { + this.setSelelctAllow(selelctAllow); + } + } + { + Boolean updateAllow = getBoolean(properties, "druid.wall.updateAllow"); + if (updateAllow != null) { + this.setUpdateAllow(updateAllow); + } + } + { + Boolean deleteAllow = getBoolean(properties, "druid.wall.deleteAllow"); + if (deleteAllow != null) { + this.setDeleteAllow(deleteAllow); + } + } + { + Boolean insertAllow = getBoolean(properties, "druid.wall.insertAllow"); + if (insertAllow != null) { + this.setInsertAllow(insertAllow); + } + } + } } diff --git a/src/main/java/com/alibaba/druid/wall/WallFilter.java b/src/main/java/com/alibaba/druid/wall/WallFilter.java index e71dc6779e..bfa376cf61 100644 --- a/src/main/java/com/alibaba/druid/wall/WallFilter.java +++ b/src/main/java/com/alibaba/druid/wall/WallFilter.java @@ -98,11 +98,9 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean { this.throwException = value; } } - { - String tenantColumn = properties.getProperty("druid.wall.tenantColumn"); - if (tenantColumn != null) { - this.config.setTenantColumn(tenantColumn); - } + + if (this.config != null) { + this.config.configFromProperties(properties); } } @@ -161,7 +159,7 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean { } else { throw new IllegalStateException("dbType not support : " + dbType + ", url " + dataSource.getUrl()); } - + provider.setName(dataSource.getName()); this.inited = true; diff --git a/src/test/java/com/alibaba/druid/bvt/filter/wall/WallConfigTest.java b/src/test/java/com/alibaba/druid/bvt/filter/wall/WallConfigTest.java new file mode 100644 index 0000000000..1cbe664088 --- /dev/null +++ b/src/test/java/com/alibaba/druid/bvt/filter/wall/WallConfigTest.java @@ -0,0 +1,84 @@ +package com.alibaba.druid.bvt.filter.wall; + +import java.util.Properties; + +import org.junit.Assert; + +import com.alibaba.druid.wall.WallConfig; + +import junit.framework.TestCase; + +public class WallConfigTest extends TestCase { + public void test_selectAllow() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.selelctAllow", "true"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertTrue(config.isSelelctAllow()); + } + + public void test_selectAllow_false() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.selelctAllow", "false"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertFalse(config.isSelelctAllow()); + } + + public void test_deleteAllow() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.deleteAllow", "true"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertTrue(config.isDeleteAllow()); + } + + public void test_deleteAllow_false() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.deleteAllow", "false"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertFalse(config.isDeleteAllow()); + } + + public void test_updateAllow() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.updateAllow", "true"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertTrue(config.isUpdateAllow()); + } + + public void test_updateAllow_false() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.updateAllow", "false"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertFalse(config.isUpdateAllow()); + } + + + public void test_insertAllow() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.insertAllow", "true"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertTrue(config.isInsertAllow()); + } + + public void test_insertAllow_false() throws Exception { + Properties properties = new Properties(); + properties.setProperty("druid.wall.insertAllow", "false"); + WallConfig config = new WallConfig(); + config.configFromProperties(properties); + + Assert.assertFalse(config.isInsertAllow()); + } +}