防止SQL注入在Hibernate中
发布时间:2020-12-12 08:46:39 所属栏目:MsSql教程 来源:网络整理
导读:我已经使用hibernate与我的数据库进行交互,现在我想让我的数据库层与SQL注入安全,所以我做了一些研究,我发现我的查询应该被参数化,所以这意味着如果我只是结构我的HQL查询如: List mothers = session.createQuery("select mother from Cat as cat join cat.m
我已经使用hibernate与我的数据库进行交互,现在我想让我的数据库层与SQL注入安全,所以我做了一些研究,我发现我的查询应该被参数化,所以这意味着如果我只是结构我的HQL查询如:
List mothers = session.createQuery( "select mother from Cat as cat join cat.mother as mother where cat.name = ?") .setString(0,name) .list(); 然后它被参数化和保护免受SQL注入,或者还有其他我需要做的… 另外一个提到的 – “永远逃避你的数据”怎么可以实现? 解决方法我不知道setString(),但是如果它与setParameter()相同,那么可以这样做,以防止sql注入.更新 通过转义数据,意味着您必须确保您没有在数据库中存储危险值. 例如,如果您传递参数,一个快速的例子 String name = "<script>alert('Hello');</script>"; //insert this name into Mother,and then when you load it from the database,it will be displayed List mothers = session.createQuery( "select mother from Cat as cat join cat.mother as mother where cat.name = ?") .setString(0,name) .list(); 对于您的查询,然后下次从数据库加载它,并将其呈现在您的Web浏览器中,它将运行该脚本. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |