【问题标题】:org.hibernate.session prepared statementorg.hibernate.session 准备好的语句
【发布时间】:2016-03-23 03:45:31
【问题描述】:

一定有办法在hibernate中准备我们的SQL查询吧?

因此,我们最终不会编写出容易受到此类 SQL 注入攻击的代码...

public List<Book> findByAutor(String author) {
    String qry = "from Book where author=\'"+author+"\'"; 
    @SuppressWarnings("unchecked")
    List<Book> books = (List<Book>) getCurrentSession().createQuery(qry).list();
    return books; 
}

因为用户可以输入类似的内容:

"Paulo Coelho\' or ''='"

作为作者并获取我们数据库中的所有书籍...

可能很简单,但我在互联网上找不到它:/

【问题讨论】:

标签: java hibernate prepared-statement


【解决方案1】:

尝试使用这样的东西,你可以set字符串和check它:

 String qry = "from Book where author= ?"; 
  @SuppressWarnings("unchecked")
  List<Book> books = (List<Book>) getCurrentSession().createQuery(qry)
  .setString(0, author)
  .list();

欲了解更多信息,请查看link

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-01
    • 2015-06-27
    • 2011-09-16
    • 1970-01-01
    相关资源
    最近更新 更多