在HQL order by子句中处理SQL注入

mic*_*man 6 java hibernate hql sql-order-by code-injection

是否有一种简单的方法来处理Hibernate HQL order by子句中的SQL注入.命名参数显然不起作用.

编辑:

随意发布处理此问题的方法.我希望看到其他人的解决方案并从他们那里传授.

感谢您的任何建议和解决方案.

Ral*_*lph 3

您可以使用 Hibernate criteria API 而不是 HQL。

标准 API 检查订单标准是否引用了有效的属性。

如果你尝试这样的事情:

public void testInjection() {
    String orderBy = "this_.type desc, type";

    Criteria crit = this.getSession().createCriteria(DemoEntity.class);
    crit.addOrder(Order.asc(orderBy));      
    crit.list();
}
Run Code Online (Sandbox Code Playgroud)

你会QueryException: "could not resolve property this_ of de.test.DemoEntity" 被 AbstractPropertyMapping 抛出异常。