Linux 拨号vps windows公众号手机端

django如何应对sql注入攻击

lewis 7年前 (2018-10-05) 阅读数 14 #VPS/云服务器
文章标签 djangosql注入

django应对sql注入攻击的方法:

1.使用django自带的数据库API,它会根据数据库的转换规则,自动转义特殊的SQL参数。

2.在cursor.execute()的sql语句中使用“%s”,而不要在sql内直接添加参数,例如:

from django.db import connection

def user_contacts(request):

user = request.GET['username']

sql = "SELECT * FROM user_contacts WHERE username = %s"

cursor = connection.cursor()

cursor.execute(sql, [user])

# ... do something with the results

版权声明

本文仅代表作者观点,不代表米安网络立场。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。

热门