作 者: 狂逆着风
链 接: http://blog.sina.com.cn/s/blog_47d78bed0100wnrs.html
antisamy是owasp的开源项目,它用来确保用户输入的HTML/CSS符合应用规范的API,可以有效防止xss攻击有.net和java两个版本,.net推荐用antixss来做,请看使用antixss防御xss
antisamy项目地址: https://www.owasp.org/index.php/Category:OWASP_AntiSamy_Project
下载地址: http://code.google.com/p/owaspantisamy/downloads/list
最新版本下载:http://search.maven.org/ 搜索 antisamy 选择最新版下载即可
我们下载antisamy-1.4.4.jar
antisamy-required-libs-1.2.zip
antisamy-slashdot-1.4.4.xml
required-libs解压后,所有jar需要导入项目库,我直接拷贝到/web-inf/lib下了,antisamy-1.4.4解压之后的antisamy-1.4.4.jar也拷贝到此目录
策略文件slashdot.xml放入web路径
输入测试代码
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page language="java" import="org.owasp.validator.html.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String id_d = request.getParameter("id");
if(id_d == null)
id_d = "";
Policy policy = Policy.getInstance("D:\\antisamy\\antisamy-slashdot-1.4.4.xml");
AntiSamy as = new AntiSamy();
CleanResults cr = as.scan(id_d, policy);
String id = cr.getCleanHTML();
out.println(id);
%>
</body>
</html>
id输入test<>
输出时被转义
输入test<script>
被白名单过滤