如何通过JPA和Hibernate防止SQL注入?

我正在使用hibernate模式开发应用程序。当我尝试创建登录页面时,出现Sql Injection问题。我有以下代码:

@Component

@Transactional(propagation = Propagation.SUPPORTS)

public class LoginInfoDAOImpl implements LoginInfoDAO{

@Autowired

private SessionFactory sessionFactory;

@Override

public LoginInfo getLoginInfo(String userName,String password){

List<LoginInfo> loginList = sessionFactory.getCurrentSession().createQuery("from LoginInfo where userName='"+userName+"' and password='"+password+"'").list();

if(loginList!=null )

return loginList.get(0);

else return null;

}

}

在这种情况下,如何防止Sql Injection?loginInfo表的创建表语法如下:

create table login_info

(user_name varchar(16) not null primary key,

pass_word varchar(16) not null);

回答:

Query q = sessionFactory.getCurrentSession().createQuery("from LoginInfo where userName = :name");

q.setParameter("name", userName);

List<LoginInfo> loginList = q.list();

您还有其他选择,请参阅mkyong的这篇不错的文章。

以上是 如何通过JPA和Hibernate防止SQL注入? 的全部内容, 来源链接: utcz.com/qa/435211.html

回到顶部