MD5散列算法
散列算法:
通常需要對密碼進行散列,常用的有md5,sha
對md5密碼,如果知道散列后的值可以通過窮舉算法,得到md5密碼對應的明文。建議對md5進行散列時加salt(鹽),進行加密相當于對原始+鹽進行散列
正常使用時散列方法:
在程序中對原始密碼+鹽進行散列,將散列值存儲到數據庫中,并且還要講鹽也要存儲在數據庫中,如果進行密碼對比時,使用相同方法,將原始密碼+鹽進行散列,進行對比
散列方法測試
public static void main(String[] args) {//原始密碼String source = "111111";//鹽String salt = "wasd";//散列次數int hashIterations = 2;//散列一次:1b2814e7f4cbb32fea953252e45fface//散列兩次:174aec66f457f5169dbb1922393b6b4c//構造方法中://第一個參數:明文,原始密碼//第二個參數:鹽,通過使用隨機數//第三個參數:散列的次數,比如散列兩次,相當于md5(md5(''))Md5Hash md5Hash = new Md5Hash(source, salt, hashIterations);String password_md5 = md5Hash.toString();System.out.println(password_md5);//第一個參數:散列算法SimpleHash simpleHash = new SimpleHash("md5", source, salt, hashIterations);System.out.println(simpleHash.toString());}ini配置文件整合和為讀取數據庫
[main] #定義憑證匹配器 credentialsMatcher=org.apache.shiro.authc.credential.HashedCredentialsMatcher #散列算法 credentialsMatcher.hashAlgorithmName=md5 #散列次數 credentialsMatcher.hashIterations=2#將憑證匹配器設置到realm customRealm=cn.dinggc.shiro.realm.CustomRealMd5 customRealm.credentialsMatcher=$credentialsMatcher securityManager.realms=$customRealm自定義realmmd5
public class CustomRealMd5 extends AuthorizingRealm{//設置realm的名稱@Overridepublic void setName(String name) {super.setName("customRealmMd5");}//用于認證@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {// TODO Auto-generated method stub//token是用戶輸入的//第一步從token中取出身份信息String userCode = (String)token.getPrincipal();//第二步:根據用戶輸入的userCode從數據庫查詢//...//模擬從數據庫查詢密碼,散列值String password = "174aec66f457f5169dbb1922393b6b4c";//從數據庫中獲取saltString salit = "wasd";//如果查詢不到返回null//數據庫中用戶賬號是zhangsan/*if(!userCode.equals("zhangsansan")) {return null;}*///如果查詢到返回認證信息AuthenticationInfoSimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(userCode, password,ByteSource.Util.bytes(salit),this.getName());return simpleAuthenticationInfo;}//用于授權@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection arg0) {// TODO Auto-generated method stubreturn null;} }md5測試
// 用戶登錄和退出@Testpublic void testCustomRealmMd5() {// 創建securityManager工廠,用過ini配置文件創建securityManager工廠Factory<SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro-realm-md5.ini");// 創建SecurityManagerSecurityManager securityManager = factory.getInstance();// 將securityManager設置到當前的運行環境中SecurityUtils.setSecurityManager(securityManager);// 從SecurityUtils里邊創建一個subjectSubject subject = SecurityUtils.getSubject();// 在認證提交前準備token(令牌)UsernamePasswordToken token = new UsernamePasswordToken("zhangsan", "111111");try {// 執行認證提交subject.login(token);} catch (AuthenticationException e) {// TODO Auto-generated catch blocke.printStackTrace();}//是否認證通過boolean isAuthenticated = subject.isAuthenticated();System.out.println("是否認證通過: " + isAuthenticated);//退出操作subject.logout();//是否認證通過isAuthenticated = subject.isAuthenticated();System.out.println("是否認證通過: " + isAuthenticated);}總結
- 上一篇: Java从零开始(二) Tomacat
- 下一篇: 发短信接口获取验证码