前端防止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;
}