2017-09-06 93 views
0

我是JPA的新手 - 我已經開始構建一個用戶系統並將編碼bean添加到我的pom中。JPA - Java Spring Boot - 用密碼編碼查找帳戶的問題?

當我在表上搜索一個電子郵件地址時 - 它找到用戶並返回數據。

TblLogin acc = tblLoginRepository.findByEmail(email); 

和我對此的回購如下。

public interface TblLoginRepository extends JpaRepository<TblLogin, String> { 
    TblLogin findByEmail(String email) throws Exception; 
} 

- 當我嘗試做一個查找與口令進行編碼沒有找到用戶 - 我不知道爲什麼的數據顯示正確嗎?

與密碼

TblLogin checkAccount = tblLoginRepository.findByEmailAndPassword(email,passwordEncoder.encode(password)); 

repo2

public interface TblLoginRepository extends JpaRepository<TblLogin, String> { 
    TblLogin findByEmail(String email) throws Exception; 
    TblLogin findByEmailAndPassword(String email, String password) throws Exception; 
} 

-

是不是因爲我的類型,密碼已更改? - 是否應該檢查別的東西...

+0

你究竟想要什麼?什麼是問題? – Balasubramanian

+0

- 好的 - 我正在用戶帳戶上進行數據提取 - 我可以看到,當我嘗試登錄時 - 密碼編碼實際上正在改變 - 因此無法找到結果...是否有一種獲得相同編碼結果的方式來與存儲在db上的內容匹配 - 類似於md5(x) –

+0

編碼如何改變?你是否聲明PasswordEncoding爲bean?你在「保存」過程中使用它嗎? –

回答

0

我不完全知道你是如何實現它的。在實現該功能時確保一些事情。

1 - 註冊豆的PasswordEncoder:

public PassordEncoder encoder() { 
    return new BCryptPasswordEncoder(11, new SecureRandom("seed".getBytes("UTF-8))); 
} 

依賴於定製的 「種子」(出於安全原因)

2 - 確保你是相同的字符的字符集編碼範圍內。 如前所述......我不知道你是如何檢索你的密碼,你是哈希。給一個嘗試建設(醜)如下:

String passwordToHash = new String(password.getBytes("UTF-8")); 

3 - 你如何存儲哈希在你的數據庫?你可以驗證在「保存」過程中生成的哈希以及在該過程之後存儲的哈希是否相同?

希望它有幫助。

+0

1.要我加一個try/catch方法 - 或拋聲明 –

+0

2.我沒有比賽的想法 - 它需要人類可讀的密碼 - 與存儲的密碼(這是我第一次獲得這樣做查找剛在電子郵件) - 所以我得到的電子郵件第一可能的用戶 - 然後執行存儲在輸入的密碼和密碼的支票 - 如果(passwordEncoder.matches(密碼,checkAccount.getPassword())){ –

+0

3。 - 在寄存器映射 - 我藏匿的新帳戶這樣的 - 所以做了檢查,看是否存在郵件 - 然後做一個saveAndFlush - \t \t \t \t //如果沒有創建一些 \t \t \t \t TblLogin newAcc = tblLoginRepository.saveAndFlush(new TblLogin()\t \t \t \t \t \t \t電子郵件, \t \t \t \t \t \t \t passwordEncoder.encode(密碼), \t \t \t \t \t \t \t銷 \t \t \t \t \t \t)); –