第五章:shiro密碼加密
在涉及到密碼存儲問題上,應該加密/生成密碼摘要存儲,而不是存儲明文密碼。比如之前的600w csdn賬號泄露對用戶可能造成很大損失,因此應加密/生成不可逆的摘要方式存儲。
5.1 編碼/解碼
Shiro提供了base64和16進制字符串編碼/解碼的API支持,方便一些編碼解碼操作。Shiro內部的一些數據的存儲/表示都使用了base64和16進制字符串。
String str = "hello"; String base64Encoded = Base64.encodeToString(str.getBytes()); String str2 = Base64.decodeToString(base64Encoded); Assert.assertEquals(str, str2);//Junit4中的方法
通過如上方式可以進行base64編碼/解碼操作,更多API請參考其Javadoc。
String str = "hello"; String hexEncoded = Hex.encodeToString(str.getBytes()); String str2 = new String(Hex.decode(hexEncoded.getBytes())); Assert.assertEquals(str, str2);
通過如上方式可以進行16進制字符串編碼/解碼操作,更多API請參考其Javadoc。
還有一個可能經常用到的類CodecSupport,提供了toBytes(str, "utf-8") / toString(bytes, "utf-8")用於在byte數組/String之間轉換。
5.2 散列算法
散列算法一般用於生成數據的摘要信息,是一種不可逆的算法,一般適合存儲密碼之類的數據,常見的散列算法如MD5、SHA等。一般進行散列時最好提供一個salt(鹽),比如加密密碼“admin”,產生的散列值是“21232f297a57a5a743894a0e4a801fc3”,可以到一些md5解密網站很容易的通過散列值得到密碼“admin”,即如果直接對密碼進行散列相對來說破解更容易,此時我們可以加一些只有系統知道的幹擾數據,如用戶名和ID(即鹽);這樣散列的對象是“密碼+用戶名+ID”,這樣生成的散列值相對來說更難破解。
Shiro還提供了通用的散列支持:
String str = "hello"; String salt = "123"; //內部使用MessageDigest String simpleHash = new SimpleHash("MD5", str, salt,1024).toString();
通過調用SimpleHash時指定散列算法,其內部使用了Java的MessageDigest實現。
HashedCredentialsMatcher實現密碼驗證服務
Shiro提供了CredentialsMatcher的散列實現HashedCredentialsMatcher,它只用於密碼驗證,且可以提供自己的鹽,而不是隨機生成鹽,且生成密碼散列值的算法需要自己寫,因為能提供自己的鹽。
1.ini配置(shiro-hashedCredentialsMatcher.ini)
[main] credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher credentialsMatcher.hashAlgorithmName=md5 credentialsMatcher.hashIterations=2 credentialsMatcher.storedCredentialsHexEncoded=true myRealm=com.github.zhangkaitao.shiro.chapter5.hash.realm.MyRealm2 myRealm.credentialsMatcher=$credentialsMatcher securityManager.realms=$myRealm
1、通過credentialsMatcher.hashAlgorithmName=md5指定散列算法為md5,需要和生成密碼時的一樣;
2、credentialsMatcher.hashIterations=2,散列叠代次數,需要和生成密碼時的意義;
3、credentialsMatcher.storedCredentialsHexEncoded=true表示是否存儲散列後的密碼為16進制,需要和生成密碼時的一樣,默認是base64;
此處最需要註意的就是HashedCredentialsMatcher的算法需要和生成密碼時的算法一樣。另外HashedCredentialsMatcher會自動根據AuthenticationInfo的類型是否是SaltedAuthenticationInfo來獲取credentialsSalt鹽。
2、自定義Realm繼承AuthorizingRealm實現doGetAuthenticationInfo方法
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException { String username = "liu"; //用戶名及salt1 String password = "202cb962ac59075b964b07152d234b70"; //加密後的密碼 String salt2 = username ; SimpleAuthenticationInfo ai = new SimpleAuthenticationInfo(username, password, getName()); ai.setCredentialsSalt(ByteSource.Util.bytes(username+salt2)); //鹽是用戶名+隨機數 return ai; }
密碼重試次數限制
如在1個小時內密碼最多重試5次,如果嘗試次數超過5次就鎖定1小時,1小時後可再次重試,如果還是重試失敗,可以鎖定如1天,以此類推,防止密碼被暴力破解。我們通過繼承HashedCredentialsMatcher,且使用Ehcache記錄重試次數和超時時間。
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) { String username = (String)token.getPrincipal(); //retry count + 1 Element element = passwordRetryCache.get(username); if(element == null) { element = new Element(username , new AtomicInteger(0)); passwordRetryCache.put(element); } AtomicInteger retryCount = (AtomicInteger)element.getObjectValue(); if(retryCount.incrementAndGet() > 5) { //if retry count > 5 throw throw new ExcessiveAttemptsException(); } boolean matches = super.doCredentialsMatch(token, info); if(matches) { //clear retry count passwordRetryCache.remove(username); } return matches; }
如上代碼邏輯比較簡單,即如果密碼輸入正確清除cache中的記錄;否則cache中的重試次數+1,如果超出5次那麽拋出異常表示超出重試次數了。
第五章:shiro密碼加密