且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何防止SQL注入在休眠?

更新时间:2023-12-02 09:24:22

  Query q = sessionFactory.getCurrentSession()。createQuery(from LoginInfo where userName =:name); 
q.setParameter(name,userName);
列表< LoginInfo> loginList = q.list();

您还有其他选择,请看这个不错的文章来自mkyong。


I am developing an application using hibernate.When I try to create a Login page, The problem of Sql Injection arises. I have the following code:

@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;   
          }
      }

How will i prevent Sql Injection in this scenario ?The create table syntax of loginInfo table is as follows:

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();

You other have options too, see this nice article from mkyong.