在PHP中,我们有一个名为strip_tags
的函数,它可以去除所有标签,同时也可以排除某些标签不被去除。
我的问题是,在Java中是否有类似的功能?
clean
方法:String unsafe =
"<p><a href='http://example.com/' onclick='stealCookies()'>Link</a></p>";
String safe = Jsoup.clean(unsafe, Whitelist.basic());
// now: <p><a href="http://example.com/" rel="nofollow">Link</a></p>
OWASP Anit-Samy项目可以实现这一点(以及更多)https://www.owasp.org/index.php/Category:OWASP_AntiSamy_Project
如果需要更简单的验证,请使用ESAPI验证器http://owasp-esapi-java.googlecode.com/svn/trunk_doc/latest/org/owasp/esapi/Validator.html
WhiteList.none
开始,并执行addTags("P")
来创建这样的白名单。 - aioobe