ESAPI是owasp提供的一套API級別的web應(yīng)用解決方案。簡單的說,ESAPI就是為了編寫出更加安全的代碼而設(shè)計出來的一些API,方便使用者調(diào)用,從而方便的編寫安全的代碼;下圖顯示提供的API與OWASP列出的10個安全問題的關(guān)聯(lián)關(guān)系:

相關(guān)API介紹可以查看官方文檔:https://www.javadoc.io/doc/org.owasp.esapi/esapi/2.1.0
ESAPI安裝:
下載地址:https://www.owasp.org/index.php/Category:OWASP_Enterprise_Security_API#tab=Downloads
下載Log4j (log4j-1.2.17.zip)(一定要導(dǎo)入這個jar包,沒有會報錯的!)
http://logging.apache.org/log4j/1.2/download.html

在Resources目錄里面導(dǎo)入ESAPI.properties 、 validation.properties 兩個配置文件:

ESAPI使用:
針對XSS漏洞:

<script>alert(1)</script>被encodeForHTML轉(zhuǎn)義結(jié)果:

<a href="javascript:alert(1)" />這種超鏈接要用 encodeForURL接口,否則:

具體demo可以參見:https://github.com/littlebin404/XssTest.git
針對SQL注入:
input = ESAPI.encoder().encodeForSQL(newMySQLCodec(MySQLCodec.Mode.STANDARD),input);
驗證輸入:
input = ESAPI.validator().getValidInput("",input,"Email",11,false);
驗證惡意文件
String inputfilename ="a.txt";
ArrayList<String> allowedExtension = new ArrayList<String>();
allowedExtension.add("jpg");
if(!ESAPI.validator().isValidFileName("upload",inputfilename, allowedExtension,false)){
? System.out.println("文件名不合法!");
}
//獲取隨機文件名
ESAPI.randomizer().getRandomFilename("jpg");
更多參考:
WEB安全編程技術(shù)規(guī)范:https://wenku.baidu.com/view/c24d4e642af90242a895e588.html