Druid 的 WallFilter 抛出 sql injection violation, comment not allow 问题的解决方法

ps:

    https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE-wallfilter可以对应修改参数。

    如需要执行多行语句 , 要设置multiStatementAllow为true

1 现象

查询某个模块数据时,抛出以下异常:

Caused by: java.sql.SQLException: sql injection violation, comment not allow : select count(*) FROM sys_x a WHERE 1=1 --澶囨敞 AND a.organization_id NOT IN( SELECT b.descendant_id FROM sys_y b WHERE b.path_length!=0) 
	at com.alibaba.druid.wall.WallFilter.checkInternal(WallFilter.java:800)
	at com.alibaba.druid.wall.WallFilter.connection_prepareStatement(WallFilter.java:251)
	at com.alibaba.druid.filter.FilterChainImpl.connection_prepareStatement(FilterChainImpl.java:473)
	at com.alibaba.druid.proxy.jdbc.ConnectionProxyImpl.prepareStatement(ConnectionProxyImpl.java:342)
	at com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:349)
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at java.lang.reflect.Method.invoke(Method.java:498)
	at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:239)
	at com.sun.proxy.$Proxy23.prepareStatement(Unknown Source)
	at org.springframework.jdbc.core.PreparedStatementCreatorFactory$PreparedStatementCreatorImpl.createPreparedStatement(PreparedStatementCreatorFactory.java:245)
	at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:583)
	... 59 more
复制代码

2 原因

  1. 在数据源配置时,加上了 Druid 的 wall 过滤器。而它默认的拦截策略是,不允许 SQL 中带有备注。
  2. 在该条 SQL 语句中,果然加了备注。

3 解决

  1. 如果是新项目,SQL 语句较少,那么可以去除语句中的备注。
  2. 如果是老项目,那么就必须对 Druid 的 wall 过滤器进行配置,打开项目的 XML 配置文件,配置 druid 拦截过滤器,允许 SQL 语句中存在注释:
<!--配置 druid 拦截过滤器-->
<bean id="wall-filter-config" class="com.alibaba.druid.wall.WallConfig" init-method="init">
	<!-- 是否允许语句中存在注释-->
	<property name="commentAllow" value="true" />
</bean>
<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
	<property name="config" ref="wall-filter-config" />
</bean>
复制代码

然后在 Druid 数据源配置中,加入 Druid 拦截过滤器:

<!--druid 数据源-->
<bean id="druidDataSource" class="com.alibaba.druid.pool.DruidDataSource">
	...
	<!--配置 Druid 过滤器-->
	<property name="proxyFilters">
		<list>
		        ...
			<ref bean="wall-filter"/>
		</list>
	</property>
        ...
</bean>
复制代码

重启应用,看看问题是不是已经解决啦O(∩_∩)O哈哈~


作者:deniro
链接:https://juejin.im/post/5d340af7e51d455d850d3baf
来源:掘金
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

发布了116 篇原创文章 · 获赞 44 · 访问量 31万+

猜你喜欢

转载自blog.csdn.net/thewindkee/article/details/103353596
今日推荐