利用spring AOP 實現 sql注入檢測

2021-09-23 06:00:15 字數 2524 閱讀 3094

轉 

什麼是sql注入?

所謂sql注入,就是通過把sql命令插入到web表單提交或輸入網域名稱或頁面請求的查詢字串,最終達到欺騙伺服器執行惡意的sql命令。具體來說,它是利用現有應用程式,將(惡意的)sql命令注入到後台資料庫引擎執行的能力,它可以通過在web表單中輸入(惡意)sql語句得到乙個存在安全漏洞的**上的資料庫,而不是按照設計者意圖去執行sql語句。 [1] 比如先前的很多影視**洩露vip會員密碼大多就是通過web表單遞交查詢字元暴出的,這類表單特別容易受到sql注入式攻擊

sql注入導致原因?sql注入的產生原因通常表現在以下幾方面:

①不當的型別處理;

②不安全的資料庫配置;

③不合理的查詢集處理;

④不當的錯誤處理;

⑤轉義字元處理不合適;

⑥多個提交處理不當。

簡單例子

利用spring的aop做了乙個簡單的檢測sql注入的例子

@aspect

@component

public class logaspect

//前置通知

@before("weblog()")

public void before(joinpoint joinpoint)throws throwable

//後置通知

@afterreturning(returning = "ret", pointcut = "weblog()")

public void afterreturning(object ret) throws throwable

//後置異常通知

@afterthrowing("weblog()")

public void returnthrowing(joinpoint joinpoint)

//最終通知

@after("weblog()")

public void after(joinpoint joinpoint)

//環繞通知

@around("weblog()")

public object arround(proceedingjoinpoint pjp)

if (!illegalstrfilterutil.isillegalstr(str))

object o = pjp.proceed();

logger.info("方法環繞proceed,結果是 :" + o);

return o;

} catch (throwable e) }}

檢測工具類

public class illegalstrfilterutil 

input = input.touppercase();

if (input.indexof("delete") >= 0 || input.indexof("ascii") >= 0 || input.indexof("update") >= 0 || input.indexof("select") >= 0

|| input.indexof("'") >= 0 || input.indexof("substr(") >= 0 || input.indexof("count(") >= 0 || input.indexof(" or ") >= 0

|| input.indexof(" and ") >= 0 || input.indexof("drop") >= 0 || input.indexof("execute") >= 0 || input.indexof("exec") >= 0

|| input.indexof("truncate") >= 0 || input.indexof("into") >= 0 || input.indexof("declare") >= 0 || input.indexof("master") >= 0)

logger.info("通過sql檢測");

return true;

}/**

* 對非法字元進行檢測

** @param input

* @return

* true 表示引數不包含非法字元

* false 表示引數包含非法字元

*/public static boolean isillegalstr(string input)

input = input.trim();

pattern compile = pattern.compile(regx, pattern.case_insensitive);

matcher matcher = compile.matcher(input);

logger.info("通過字串檢測");

return matcher.find();}}

利用Spring AOP實現業務和異常日誌記錄

實際上這個確實非常好用。最近碰到乙個問題,就是發現以前action中的日誌記錄的不夠完善,需要在所有action中的每個介面改下呼叫日誌的方法,這種工作量太大而且毫無意義,因此就想到用aop。當然也可以用 通過aop把所有action中的介面作為切點,設定對應的切面和方法,讓介面返回後進行返回通知,...

Spring AOP實現方式

spring中4中aop區別 1.advisor類 需要依賴spring介面,spring版本比較低時可以採取這種方式 2.適合使用者從低版本spring遷移到高版本spring,重複利用以前advice類情況 3.使用配置檔案形式定義切面,比較推薦的一種方式,使用於對維護性要求較高的情況 4.as...

Spring AOP簡單實現

下面用乙個簡單示例演示aop的使用,在執行目標方法前執行日誌輸出。這也是aop最重要的作用,分離與業務無關的 建立target類,是被 的物件,有乙個execute方法。package com.home.web.manager public class target 建立通知類,可以攔截目標物件的e...