shiro 有乙個非常強大的功能,就是對加密演算法的封裝。在我們之前的例子中,我們的密碼在 realm 裡返回認證資訊的時候,寫的都是明文。這樣的方式其實是很不安全的。
一般地,密碼這樣的高度敏感的字段,我們應該是使用一種不可破解的演算法加密以後儲存到我們的資料庫中。當使用者登入的時候,在使用者輸入使用者名稱正確(在庫中存在)的前提下,將使用者輸入的密碼使用同樣的演算法加密以後得到的字串和資料庫中的密文密碼進行匹配,匹配成功,則認證通過。
這樣處理的好處是,即使是可以接觸生產環境的開發人員或者是資料庫管理人員,都無法通過資料庫資訊知道真實使用者的密碼。即使是密碼洩露給外部,別人也無法知道密碼的明文是什麼,極大地保護了使用者資訊的安全。
shiro 提供了 passwordservice 及 credentialsmatcher 用於提供加密密碼及驗證密碼服務。
我們先來看乙個例子:
passwordservice service = new defaultpasswordservice();
string str1 = service.encryptpassword("123456");
string str2 = service.encryptpassword("123456");
system.out
.println(str1);
system.out
.println(str2);
// 鹽值是存放在加密以後的密碼中的
boolean boolean1 = service.passwordsmatch("123456",str1);
system.out
.println(boolean1);
boolean boolean2 = service.passwordsmatch("123456",str2);
system.out
.println(boolean2)
我們發現,同樣是「123456」這樣的密碼,使用 defaultpasswordservice 這個實現類的 encryptpassword() 方法加密以後得到的密文都是不一樣的。但是我們可以使用 defaultpasswordservice 這個實現類的 passwordsmatch() 將明文密碼與密文密碼進行匹配,使用這個方法匹配是成功可靠的,這一點我們須要理解。
那麼,我們如何在 realm 中指定 shiro 使用 defaultpasswordservice 這個實現類的 passwordsmatch() 方法進行密碼的匹配呢?
我們可以使用依賴注入的方式,**如下:
自定義 realm 的認證實現部分:
public
class
mypasswordrealm
extends
authorizingrealm
@override
protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception
}
如果我們不指定 realm 使用 passwordmatcher 的實現類的話,預設的匹配方式就是使用字串是否相同這樣的匹配方式,顯然不能達到我們的要求。所以我們要在 realm 中指定 passwordmatcher 的屬性:
# 宣告乙個 shiro 已經有的密碼匹配的類
passwordmatcher=org.apache
.shiro
.authc
.credential
.passwordmatcher
# 宣告自定義的 realm 類
mypasswordrealm=com
.liwei
.realm
.mypasswordrealm
# 將 passwordmatcher 注入到自定義的 realm 類中
mypasswordrealm.credentialsmatcher=$passwordmatcher
# 將自定義的 realm 注入到 securitymanager 中
securitymanager.realms=$mypasswordrealm
這樣,密文密碼匹配的功能就實現了。
我們首先介紹使用 md5 演算法,通過加鹽的方式得到 「123456」 的密文。
string originpassword = "123456";
string salt = "hello";
string md5 = new md5hash(originpassword,salt).tostring();
system.out.println(md5);
接下來編寫自定義 realm 中的認證實現部分。
public
class
mypasswordrealm
extends
authorizingrealm
@override
protected authenticationinfo dogetauthenticationinfo(authenticationtoken authenticationtoken) throws authenticationexception
}
最後,我們要在 shiro.ini 配置檔案中配置密碼匹配的類(配置加密演算法和鹽值):
# 宣告乙個 shiro 已經有的密碼匹配的類
hashmatcher=org.apache
.shiro
.authc
.credential
.hashedcredentialsmatcher
hashmatcher.hashalgorithmname=md5
hashmatcher.hashsalted=hello
# 宣告自定義的 realm 類
mypasswordrealm=com
.liwei
.realm
.mypasswordrealm
# 將 passwordmatcher 注入到自定義的 realm 類中
mypasswordrealm.credentialsmatcher=$hashmatcher
# 將自定義的 realm 注入到 securitymanager 中
securitymanager.realms=$mypasswordrealm
到這裡,加密的部分就簡單介紹完成了,我們看到,主要還是通過配置的方式,使得我們的認證更加安全和規範。 Shiro學習筆記之Shiro加密
雜湊演算法一般用於生成資料的摘要資訊,是一種不可逆的演算法,一般適合儲存密碼之類的資料,常見的雜湊演算法如 md5 sha 等。一般進行雜湊時最好提供乙個 雜訊,這樣可以使破解密碼的難度變大。以下是md5加密演算法的乙個演示 public class tes 5 此介面提供加密密碼和驗證密碼的功能。...
Shiro學習筆記(6) shiro快取
每一次給使用者授權時,都是通過realm從資料庫中查詢許可權,為了避免頻繁的查詢資料庫,shiro給我們提供了快取的能力 使用者認證通過後 第一次授權 通過realm從資料庫中查詢 第二次授權 直接從快取中查詢 引入包 1.shiro ehcache.jar 2.ehcache core.jar 在...
Shiro學習筆記(6) shiro快取
分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!每一次給使用者授權時,都是通過realm從資料庫中查詢許可權,為了避免頻繁的查詢資料庫,shiro給我們提供了快取的能力 使用者認證通過後 第一次授權 通過realm從資料庫中查詢 第二次授權 直接從快取中查詢 引入包 1.shiro ehcach...