sql injection violation, multi-statement not allow 最終解決方案

解題思路:

1. 代碼報錯,引發異常操做是批量更新,定義以下與Druid有關Bean:mysql

<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="driverClassName" value="${db.driverClassName}"/>
    <property name="url" value="${db.url}"/>
    <property name="username" value="${db.username}"/>
    <property name="password" value="${db.passwrod}"/>
    <property name="initialSize" value="3"/>
    <property name="minIdle" value="3"/>
    <property name="maxActive" value="20"/>
    <property name="maxWait" value="60000"/>
    <property name="filters" value="stat,wall"/>
</bean>

<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
    <property name="slowSqlMillis" value="30000"/>
    <property name="logSlowSql" value="true"/>
    <property name="mergeSql" value="true"/>
</bean>

<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
     <property name="dbType" value="mysql"/>
</bean>

2. 瀏覽了第一篇博客,哦,原來是沒有定義,WallConfig ,以及allowMultiQueries=true,隨後加之。(使用SpringBoot的哥們應該就可解決此問題了。(純屬猜想))sql

<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
    <property name="dbType" value="mysql"/>
    <property name="config" ref="wall-config"/>
</bean>

<bean id="wall-config" class="com.alibaba.druid.wall.WallConfig">
    <!-- 批量sql -->
    <property name="multiStatementAllow" value="true"/>
</bean>

3. 而後繼續報錯,閱過N篇博客後,發現第二篇與第三篇博客有殊途同歸之處,二者都設置了一個叫proxyFilters的屬性,第二篇博客中實際上是覆蓋掉Druid中原有的proxyFilters,經研究,dataSoruce中filters(stat,wall),其實並非咱們本身定義的Bean(其實他媽仍是本身對框架不夠了解),而是默認生成的,這個默認生成是從proxyFilters中獲取的,因此咱們設置的multiStatementAllow沒有任何卵用,結合第三篇,就是將咱們本身的stat,wall注入了proxyFilters。數據庫

<property name="filters" value="stat,wall"/>
<!-- druid -->
<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
    <property name="slowSqlMillis" value="30000"/>
    <property name="logSlowSql" value="true"/>
    <property name="mergeSql" value="true"/>
</bean>

<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
    <property name="dbType" value="mysql"/>
    <property name="config" ref="wall-config"/>
</bean>

<bean id="wall-config" class="com.alibaba.druid.wall.WallConfig">
    <!-- 批量sql -->
    <property name="multiStatementAllow" value="true"/>
</bean>

4. 最終Druid相關配置(proxyFilters必定要放在filters上面,filters初始化時沒有proxyFilters,就會本身生成默認的,就會致使咱們的proxyFilters注入失敗,請參考第二篇博客):框架

<!-- druid -->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
   <property name="driverClassName" value="${db.driverClassName}"/>
   <property name="url" value="${db.url}"/>
   <property name="username" value="${db.username}"/>
   <property name="password" value="${db.passwrod}"/>
   <property name="initialSize" value="3"/>
   <property name="minIdle" value="3"/>
   <property name="maxActive" value="20"/>
   <property name="maxWait" value="60000"/>
   <property name="proxyFilters">
       <list>
           <ref bean="stat-filter"/>
           <ref bean="wall-filter"/>
       </list>
   </property>
   <property name="filters" value="stat,wall,slf4j"/>
</bean>

<bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
   <property name="slowSqlMillis" value="30000"/>
   <property name="logSlowSql" value="true"/>
   <property name="mergeSql" value="true"/>
</bean>

<bean id="wall-filter" class="com.alibaba.druid.wall.WallFilter">
   <property name="dbType" value="mysql"/>
   <property name="config" ref="wall-config"/>
</bean>

<bean id="wall-config" class="com.alibaba.druid.wall.WallConfig">
    <!-- 批量sql -->
    <property name="multiStatementAllow" value="true"/>
</bean>

5. 最後不要忘了:allowMultiQueries=true(寫在數據庫鏈接串後邊的東西。)ui

相關文章
相關標籤/搜索