This commit is contained in:
温少
2012-03-17 13:51:46 +08:00
parent 67d4b78620
commit 12a485e921
6 changed files with 102 additions and 67 deletions
@@ -32,6 +32,55 @@ public class WallConfig {
protected final Set<String> permitNames = new HashSet<String>();
protected final Set<String> permitObjects = new HashSet<String>();
private String dir;
private boolean loadDefault = true;
private boolean loadExtend = true;
public WallConfig(){
}
public WallConfig(String dir){
this.dir = dir;
this.init();
}
public String getDir() {
return dir;
}
public void setDir(String dir) {
this.dir = dir;
}
public void init() {
if (loadDefault) {
loadDefault(dir);
}
if (loadExtend) {
loadExtend(dir);
}
}
public boolean isLoadDefault() {
return loadDefault;
}
public void setLoadDefault(boolean loadDefault) {
this.loadDefault = loadDefault;
}
public boolean isLoadExtend() {
return loadExtend;
}
public void setLoadExtend(boolean loadExtend) {
this.loadExtend = loadExtend;
}
public void loadDefault(String dir) {
if (dir.endsWith("/")) {
dir = dir.substring(0, dir.length() - 1);
@@ -151,6 +200,8 @@ public class WallConfig {
this.functionCheck = functionCheck;
}
// ///////////////////
public Set<String> getPermitFunctions() {
return permitFunctions;
}
@@ -26,23 +26,49 @@ public class WallFilter extends FilterAdapter {
private WallProvider provider;
private boolean loadDefault;
private boolean loadExtend;
private boolean logViolation = false;
private WallConfig config = new WallConfig();
private String dbType;
private WallConfig config;
@Override
public void init(DataSourceProxy dataSource) {
this.dataSource = dataSource;
provider = createWallProvider(dataSource.getDbType());
String dbType = this.dbType;
if (this.dbType == null || this.dbType.trim().length() == 0) {
this.dbType = dataSource.getDbType();
}
if (JdbcUtils.MYSQL.equals(dbType)) {
provider = new MySqlWallProvider(config);
if (config == null) {
config = new WallConfig(MySqlWallProvider.DEFAULT_CONFIG_DIR);
}
} else if (JdbcUtils.ORACLE.equals(dbType)) {
provider = new OracleWallProvider(config);
if (config == null) {
config = new WallConfig(OracleWallProvider.DEFAULT_CONFIG_DIR);
}
} else {
throw new IllegalStateException("dbType not support : " + dbType);
}
this.inited = true;
}
public String getDbType() {
return dbType;
}
public void setDbType(String dbType) {
this.dbType = dbType;
}
public boolean isLogViolation() {
return logViolation;
}
@@ -63,30 +89,12 @@ public class WallFilter extends FilterAdapter {
this.config = config;
}
public boolean isLoadDefault() {
return loadDefault;
}
public void checkInit() {
if (inited) {
throw new DruidRuntimeException("wall filter is inited");
}
}
public void setLoadDefault(boolean loadDefault) {
checkInit();
this.loadDefault = loadDefault;
}
public boolean isLoadExtend() {
return loadExtend;
}
public void setLoadExtend(boolean loadExtend) {
checkInit();
this.loadExtend = loadExtend;
}
@Override
public ConnectionProxy connection_connect(FilterChain chain, Properties info) throws SQLException {
return chain.connection_connect(info);
@@ -234,17 +242,4 @@ public class WallFilter extends FilterAdapter {
}
}
public WallProvider createWallProvider(String dbType) {
WallProvider provider;
if (JdbcUtils.MYSQL.equals(dbType)) {
provider = new MySqlWallProvider(config, this.loadDefault, this.loadExtend);
} else if (JdbcUtils.ORACLE.equals(dbType)) {
provider = new OracleWallProvider(config, this.loadDefault, this.loadExtend);
} else {
throw new IllegalStateException("dbType not support : " + dbType);
}
return provider;
}
}
@@ -8,24 +8,14 @@ import com.alibaba.druid.sql.parser.SQLStatementParser;
public class MySqlWallProvider extends WallProvider {
public final static String DEFAULT_CONFIG_DIR = "META-INF/druid/wall/mysql";
public MySqlWallProvider(){
this(new WallConfig());
this(new WallConfig(DEFAULT_CONFIG_DIR));
}
public MySqlWallProvider(WallConfig config){
this(config, true, true);
}
public MySqlWallProvider(WallConfig config, boolean loadDefault, boolean loadExtend){
super(config);
if (loadDefault) {
config.loadDefault("META-INF/druid/wall/mysql");
}
if (loadExtend) {
config.loadExtend("META-INF/druid/wall/mysql");
}
}
@Override
@@ -8,27 +8,16 @@ import com.alibaba.druid.sql.parser.SQLStatementParser;
public class OracleWallProvider extends WallProvider {
public final static String DEFAULT_CONFIG_DIR = "META-INF/druid/wall/oracle";
public OracleWallProvider(){
this(new WallConfig());
this(new WallConfig(DEFAULT_CONFIG_DIR));
}
public OracleWallProvider(WallConfig config){
this(config, true, true);
}
public OracleWallProvider(WallConfig config, boolean loadDefault, boolean loadExtend){
super(config);
if (loadDefault) {
config.loadDefault("META-INF/druid/wall/oracle");
}
if (loadExtend) {
config.loadExtend("META-INF/druid/wall/oracle");
}
}
@Override
public SQLStatementParser createParser(String sql) {
return new OracleStatementParser(sql);
@@ -50,6 +50,8 @@ import com.alibaba.druid.mapping.DruidMappingException;
*/
public final class JdbcUtils {
public static final String MOCK = "mock";
public static final String HSQL = "hsql";
public static final String DB2 = "db2";
@@ -353,7 +355,7 @@ public final class JdbcUtils {
}
}
public static String read(Reader reader) {
try {
final int DEFAULT_BUFFER_SIZE = 1024 * 4;
@@ -472,7 +474,7 @@ public final class JdbcUtils {
} else if (rawUrl.startsWith("jdbc:jtds:")) {
return "jtds";
} else if (rawUrl.startsWith("jdbc:fake:") || rawUrl.startsWith("jdbc:mock:")) {
return "mock";
return MOCK;
} else if (rawUrl.startsWith("jdbc:postgresql:")) {
return POSTGRESQL;
} else if (rawUrl.startsWith("jdbc:hsqldb:")) {
@@ -9,9 +9,16 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/
<bean id="test-filter" class="com.alibaba.druid.bvt.pool.SpringFilterTest$TestFilter">
</bean>
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
</bean>
<bean id="wall-filter-config">
<property name="dir" value="META-INF/druid/wall/mysql" />
</bean>
<bean id="wall-filter" class="com.alibaba.druid.filter.wall.WallFilter">
<property name="dbType" value="mysql" />
</bean>
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="url">
@@ -27,6 +34,7 @@ http://www.springframework.org/schema/aop http://www.springframework.org/schema/
<list>
<ref bean="test-filter"/>
<ref bean="stat-filter"/>
<ref bean="wall-filter"/>
</list>
</property>
</bean>