shiro之自定義realm
阿新 • • 發佈:2018-12-31
Shiro認證過程 建立SecurityManager---》主體提交認證---》SecurityManager認證---》Authenticsto認證---》Realm驗證 Shiro授權過程 建立SecurityManager---》主體授權---》ecurityManager授權---》Authorizer授權---》Realm獲取角色許可權資料
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <parent> <artifactId>ylht-shiro</artifactId> <groupId>com.ylht</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion> <artifactId>shiro-test</artifactId> <dependencies> <!-- https://mvnrepository.com/artifact/org.apache.shiro/shiro-core --> <dependency> <groupId>org.apache.shiro</groupId> <artifactId>shiro-core</artifactId> <version>1.4.0</version> </dependency> <!-- https://mvnrepository.com/artifact/junit/junit --> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.7</version> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java --> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.45</version> </dependency> <!-- https://mvnrepository.com/artifact/com.alibaba/druid --> <dependency> <groupId>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.6</version> </dependency> </dependencies> </project>
2.自定義realm(自定義realm可以的編寫可以參考原始碼)
package com.ylht.shiro.realm; 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.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 CustomerRealm extends AuthorizingRealm { { super.setName("customRealm"); } //該方法用來授權 protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { //1.從認證資訊中獲取使用者名稱 String username = (String) principalCollection.getPrimaryPrincipal(); //2.從資料庫或者快取中獲取使用者角色資料 Set<String> roles = getRolesByUserName(username); //3.從資料庫或者快取中獲取使用者許可權資料 Set<String> permissions = getPermissionsByUserName(username); SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo(); simpleAuthorizationInfo.setRoles(roles); simpleAuthorizationInfo.setStringPermissions(permissions); return simpleAuthorizationInfo; } //該方法用來認證 protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { //1.從認證資訊中獲取使用者名稱 String username = (String) authenticationToken.getPrincipal(); //2.通過使用者名稱到資料庫中獲取憑證 String password = getPwdByUserName(username); if (null == password) { return null; } SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo( username, password, "customRealm"); simpleAuthenticationInfo.setCredentialsSalt(ByteSource.Util.bytes("zzz")); return simpleAuthenticationInfo; } //模擬資料庫 private String getPwdByUserName(String username) { Map<String, String> userMap = new HashMap<String, String>(16); userMap.put("kk", "bdd170a94d02707687abc802b2618e19"); return userMap.get(username); } //模擬資料庫 private Set<String> getRolesByUserName(String username) { Set<String> sets = new HashSet<String>(); sets.add("admin"); sets.add("user"); return sets; } //模擬資料庫 private Set<String> getPermissionsByUserName(String username) { Set<String> sets = new HashSet<String>(); sets.add("user:select"); sets.add("user:update"); return sets; } }
3.測試類(加密方式,鹽等)
package com.ylht.shiro.test; import com.ylht.shiro.realm.CustomerRealm; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.authc.credential.HashedCredentialsMatcher; import org.apache.shiro.authz.AuthorizationException; import org.apache.shiro.crypto.hash.Md5Hash; import org.apache.shiro.mgt.DefaultSecurityManager; import org.apache.shiro.subject.Subject; import org.junit.Test; public class CustomRealmTest { @Test public void testCustomRealm() { //建立JdbcRealm物件 CustomerRealm customerRealm = new CustomerRealm(); //設定JdbcRealm屬性 //1.建立SecurityManager物件 DefaultSecurityManager securityManager = new DefaultSecurityManager(); //securityManager物件設定realm securityManager.setRealm(customerRealm); //shiro加密 HashedCredentialsMatcher matcher = new HashedCredentialsMatcher(); //加密方式 matcher.setHashAlgorithmName("md5"); //加密次數 matcher.setHashIterations(2); //customerRealm設定matcher customerRealm.setCredentialsMatcher(matcher); //2.主題提交認證 SecurityUtils.setSecurityManager(securityManager); Subject subject = SecurityUtils.getSubject(); //token UsernamePasswordToken token = new UsernamePasswordToken("kk", "123456", false); //認證 subject.login(token); boolean flag = subject.isAuthenticated(); if (flag) { System.out.println("使用者認證通過"); } else { System.out.println("使用者認證失敗"); } //角色驗證 try { subject.checkRole("admin"); System.out.println("角色驗證通過"); } catch (AuthorizationException e) { System.out.println("角色驗證失敗"); e.printStackTrace(); } //角色許可權驗證 try { subject.checkPermission("user:select"); System.out.println("角色許可權驗證通過"); } catch (AuthorizationException e) { System.out.println("角色許可權驗證失敗"); e.printStackTrace(); } } public static void main(String[] args) { //Md5Hash md5Hash = new Md5Hash("123456","zzz"); Md5Hash md5Hash = new Md5Hash("123456"); System.out.println(md5Hash); Md5Hash md5Hash1 = new Md5Hash(md5Hash); System.out.println(md5Hash1.toString()); } }