Spring boot 初学

Jsoup 防止富文本 XSS 攻击

2016-11-12  本文已影响2902人  jnil

服务器处理富文本编辑器提交的内容时, 因排版的需求不能对 HTML 标签进行转义, 但为了防止 XSS 攻击, 又必须过滤掉其中的 JS 代码, 在 Java 中使用 Jsoup 正好可以满足此要求

白名单对象 标签 说明
none 只保留标签内文本内容
simpleText b,em,i,strong,u 简单的文本标签
basic a,b,blockquote,br,cite,code,dd,
dl,dt,em,i,li,ol,p,pre,q,small,span,
strike,strong,sub,sup,u,ul
基本使用的标签
basicWithImages basic 的基础上添加了 img 标签
及 img 标签的 src,align,alt,height,width,title 属性
基本使用的加上 img 标签
relaxed a,b,blockquote,br,caption,cite,
code,col,colgroup,dd,div,dl,dt,
em,h1,h2,h3,h4,h5,h6,i,img,li,
ol,p,pre,q,small,span,strike,strong,
sub,sup,table,tbody,td,tfoot,th,thead,tr,u,ul
在 basicWithImages 的基础上又增加了一部分部分标签

** 如果没有图片上传的需求, 使用 basic, 否则使用 basicWithImages**

上一篇 下一篇

猜你喜欢

热点阅读