防止SQL注入在Hibernate中

前端之家收集整理的这篇文章主要介绍了防止SQL注入在Hibernate中前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经使用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浏览器中,它将运行该脚本.
您需要确保您的框架转义所有非法字符,即:更改<到<然后将其插入数据库.
如果您的框架不这样做,您必须手动执行.
那里有大量图书馆正确地为您释放代码.看看this question例如和那里的答案.

原文链接:https://www.f2er.com/mssql/75429.html

猜你在找的MsSQL相关文章