前端防止xss攻击的方法:
过滤非法字符,例如:
// 过滤XSS反射型漏洞
filterInputTxt: function (html) {
html = html.replace(/(.*<[^>]+>.*)/g,""); // HTML标记
html = html.replace(/([\r\n])[\s]+/g, ""); // 换行、空格
html = html.replace(//g, ""); // HTML注释
html = html.replace(/['"‘’“”!@#$%^&*{}!¥()()×+=]/g, ""); // 非法字符
html = html.replace("alert","");
html = html.replace("eval","");
html = html.replace(/(.*javascript.*)/gi,"");
if (html === "") {
html = "你好";
}
return html;
}
辰迅云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读: 如何过滤xss攻击