你好我試圖通過它的密碼哈希和用戶名組合以檢索來自DB一個簡單的對象....HQL查詢只是不uniqueResult飛()
username = request.getParameter("username");
password = request.getParameter("password");
// Converting request password to hash
String passwordHash = SecurityUtil.convertStringToSHA1(password);
Session hs = HibernateUtil.getSessionFactory().getCurrentSession();
hs.beginTransaction();
//query for a single result of matching username with password
//Query hquery = hs.createQuery("from User where id = :id");
//hquery.setParameter("id", 33l); <- THIS QUERY WORKS
Query hquery = hs.createQuery("from User where password = :password and userName = :userName"); //<- password is a sha1 hash in the db
hquery.setString("userName", username);
hquery.setParameter("password", passwordHash);
User user = (User)hquery.uniqueResult(); //<- Always NULL WHY There should be a unique result.
log.info("UR: "+username);
log.info("PR: "+passwordHash);
if(user!=null) {
//never reached
log.info("UDB: "+user.getUserName());
log.info("PDB: "+user.getPassword());
} else {
always reaches here
}
什麼想法?或者這是否意味着沒有獨特的結果?
我的SHA1哈希不鹹,但是密碼哈希值,從一個隨機字符串使用Apache公地應該或多或少的無碰撞產生的...
而且這是我第一次做的Hibernate的東西,所以我可能在某處出錯...
您運行的是哪個數據庫?在hibernate配置中用'hibernate.show_sql = true'和'hibernate.format_sql = true'打開SQL日誌記錄,並檢查你得到的SQL查詢。確保傳遞給查詢的密碼散列與數據庫中的密碼相同(也許這是一個字符串轉義問題)。 – tscho
是啊,我一直在考慮逃避問題,感謝在日誌記錄上的頭......我會試試看。我在mysql上運行.... – mahatmanich