Fork me on GitHub

node防止sql注入 xss攻击和密码加密

1.sql注入:窃取数据库内容

select * from users where username='李四'  -- '  and password =''123'  //不用密码登录

select * from users where username='李四';delete from users; -- ' and password =''123'  //直接删除用户

可以使用escape:mysql.escape

使用: username = escape(username)


2.xss攻击:窃取前端Cookie内容 node中使用xss插件

引用: const xss = require('xss');
使用: xss(对应要防御的内容)
  


3.密码加密:保障用户信息安全

nodejs 自带的加密 crypto
//引用:
const crypto = require('crypto');

//定义密匙
const SECRET_KEY = 'Lhsuper_685#@'

//md5加密

function md5(content){
    let md5 = crypto.createHash('md5');
    return md5.update(content).digest('hex');
}

//加密函数
function genPassword(password){
    const str = `password = ${password}&key=${SECRET_KEY}`
    return md5(str)
}

使用:genPassword('123')

  

 

posted @ 2020-03-10 15:10  欢欢11  阅读(1018)  评论(0编辑  收藏  举报