這里我們以md5加密方法舉例,首先我們寫一個main方法測試我們的密碼經過md5加密之后的得到什么樣的字符串:
/** * 書寫方法測試Md5Hash將密碼“houru”加密之后的密文 * 但是僅僅加密還是不夠的,別人知道你的加密算法之后還是可以輕易破解密碼的,因此我們還要“加鹽” * 加鹽:(調味)就是我們在加密密碼的基礎上在增加一些其他元素 * @param a */ public static void main(String a[]){ Md5Hash md5Hash1=new Md5Hash("houru");//只加密不加鹽 Md5Hash md5Hash2=new Md5Hash("houru","jiayan");//加密又加鹽 System.err.println(md5Hash1.toString()); System.err.println(md5Hash2.toString()); //沒有加鹽的加密結果:8a126ba89f60b97abf6185cd666ed8b4 // 加鹽的加密結果: b7f30984e630bd6bd18f0b4a3196a257 }
下面的代碼在上一篇博客基礎上修改:
MyrealmTest.java:
package com.shiro.shiroframe; import com.shiro.myrealm.CustomRealm; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.subject.Subject; import org.junit.jupiter.api.Test; public class MyrealmTest { //引入我們自定義的realm CustomRealm customRealm = new CustomRealm(); @Test public void MyrealmTest() { //引入加密工具類HashedCredentialsMatcher: HashedCredentialsMatcher hashedCredentialsMatcher=new HashedCredentialsMatcher(); //設置我們要采用的加密方法的名稱: hashedCredentialsMatcher.setHashAlgorithmName("md5"); //設置加密的次數: hashedCredentialsMatcher.setHashIterations(1); //給我們的自定義的realm設置hashedCredentialsMatcher對象 customRealm.setCredentialsMatcher(hashedCredentialsMatcher); DefaultSecurityManager defaultSecurityManager = new DefaultSecurityManager(); defaultSecurityManager.setRealm(customRealm); SecurityUtils.setSecurityManager(defaultSecurityManager); Subject subject = SecurityUtils.getSubject(); UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken("miyue", "houru"); subject.login(usernamePasswordToken); System.err.println(subject.isAuthenticated()); subject.checkRoles("admin"); subject.checkPermission("user:add"); } }
CustomRealm.java:
package com.shiro.myrealm; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAuthenticationInfo; import org.apache.shiro.authz.AuthorizationInfo; import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.crypto.hash.Hash; import org.apache.shiro.crypto.hash.Md5Hash; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.util.ByteSource; import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Set; public class CustomRealm extends AuthorizingRealm { //認證方法 @Override protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { /** * 重寫認證方法 */ //1、從主體傳過來的認證信息中獲取用戶名 String username = (String) authenticationToken.getPrincipal(); //2、通過用戶名到數據庫獲取憑證 String password = getPassWordByUsername(username); if (password == null) { return null; } SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo("miyue", password, "test"); //注意,如果我們采用了加鹽的方式加密,那么我們要給simpleAuthenticationInfo設置鹽: simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("jiayan")); return simpleAuthenticationInfo; } //授權方法 @Override protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { /** * 重新授權方法 */ String username = (String) principalCollection.getPrimaryPrincipal(); //從角色和緩存中獲取角色數據 Set<String> roles = getRolesByUsername(username); //從角色和緩存中獲取權限數據 Set<String> permission = getPermissionsByUsername(username); SimpleAuthorizationInfo simpleAuthorizationInfo=new SimpleAuthorizationInfo(); simpleAuthorizationInfo.setRoles(roles); simpleAuthorizationInfo.setStringPermissions(permission); return simpleAuthorizationInfo; } //下面使用map,set模擬數據庫數據返回 Map<String, String> map = new HashMap<String, String>(); { // map.put("miyue", "houru"); //模擬數據庫返回的密文 map.put("miyue", "b7f30984e630bd6bd18f0b4a3196a257"); } private String getPassWordByUsername(String username) { return map.get(username) == null ? null : map.get(username); } private Set<String> getRolesByUsername(String username) { Set<String> set = new HashSet<>(); set.add("admin"); set.add("user"); return set; } private Set<String> getPermissionsByUsername(String username) { Set<String> set = new HashSet<>(); set.add("user:delete"); set.add("user:add"); return set; } }