本文是對張開濤老師的shiro Realm以及相關對象教材、博客及代碼的提煉 僅自己學習和項目集成使用 如有版權問題 請聯繫我刪除
張老師原貼:http://jinnianshilongnian.iteye.com/blog/2022468
一、Realm
1.1 Realm:提供待驗證數據的比對值,即安全數據源。
自定義Realm類要實現Realm接口:
public class MyRealm1 implements Realm {
String getName(); //返回一個唯一的Realm名字
boolean supports(AuthenticationToken token); //判斷此Realm是否支持此Token
AuthenticationInfo getAuthenticationInfo(AuthenticationToken token)
throws AuthenticationException; //根據Token獲取認證信息
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) //授權
}
1.2 說明:
1.2.1、doGetAuthenticationInfo 獲取身份驗證相關信息
/1.2.2、doGetAuthorizationInfo 獲取授權信息:PrincipalCollection 是一個身份集合。可以調用 getPrimaryPrincipal 等方法得到之前傳入的用戶名,再根據用戶名調用 UserService 接口獲取角色及權限信息
1.3分類:
org.apache.shiro.realm.text.IniRealm:[users]部分指定用戶名/密碼及其角色;[roles]部分指定角色即權限信息;
org.apache.shiro.realm.text.PropertiesRealm: user.username=password,role1,role2指定用戶名/密碼及其角色;role.role1=permission1,permission2指定角色及權限信息;
org.apache.shiro.realm.jdbc.JdbcRealm:通過sql查詢相應的信息
….
二、Authenticator及AuthenticationStrategy
Authenticator:職責是驗證用戶帳號,是Shiro API中身份驗證核心的入口點,如果驗證成功,將返回AuthenticationInfo驗證信息
AuthenticationStrategy:指定驗證規則,包括但不限於FirstSuccessfulStrategy、AtLeastOneSuccessfulStrategy、AllSuccessfulStrategy:
三、Authorizer、PermissionResolver及RolePermissionResolver
1、Authorizer:
1.1 職責是進行授權(訪問控制),是Shiro API中授權核心的入口點,其提供了相應的角色/權限判斷接口。
1.2 SecurityManager繼承了Authorizer接口,且提供了ModularRealmAuthorizer用於多Realm時的授權匹配。
2、PermissionResolver:PermissionResolver用於解析權限字符串到Permission實例
3、RolePermissionResolver:用於根據角色解析相應的權限集合
四、AuthenticationToken、AuthenticationInfo、PrincipalCollection
1、 AuthenticationToken接口用於收集用戶提交的身份(如用戶名)及憑據(如密碼)
public interface AuthenticationToken extends Serializable {
Object getPrincipal(); //身份
Object getCredentials(); //憑據
}
常用實現類:UsernamePasswordToken
1、 AuthenticationInfo:
2.1身份驗證信息類,有兩個作用
2.1.1如果 Realm 是 AuthenticatingRealm 子類,則提供給 AuthenticatingRealm 內部使用的CredentialsMatcher 進行憑據驗證; (如果沒有繼承它需要在自己的 Realm中自己實現驗證);
2.1.2提供給 SecurityManager 來創建 Subject(提供身份信息)。
2.2 常見子接口和實現類:MergableAuthenticationInfo、Account、SimpleAuthenticationInfo
2、 PrincipalCollection:用於聚合身份信息,通過getPrimaryPrincipal等方法獲取驗證成功的身份。
五、AuthorizationInfo
AuthorizationInfo 用於聚合授權信息
public interface AuthorizationInfo extends Serializable {
Collection<String> getRoles(); //獲取角色字符串信息
Collection<String> getStringPermissions(); //獲取權限字符串信息
Collection<Permission> getObjectPermissions(); //獲取 Permission 對象信息
}
常用實現類:SimpleAuthorizationInfo
六、Subject
1、 Subject 是 Shiro 的核心對象,基本所有身份驗證、授權都是通過 Subject 完成,一般的用法:
1.1身份信息獲取
Object getPrincipal(); //Primary Principal PrincipalCollection getPrincipals(); // PrincipalCollection |
1.2身份驗證
void login(AuthenticationToken token) throws AuthenticationException; boolean isAuthenticated(); boolean isRemembered(); |
1.3角色/權限授權驗證
hasRole*、checkRole*進行角色驗證,isPermitted*、checkPermission*進行權限驗證 |
1.4會話
Session getSession(); |
1.5退出
void logout(); |
1.6 RunAs、多線程:略
2、 Subject 自己不會實現相應的身份驗證/授權邏輯,而是通過 DelegatingSubject 委託給SecurityManager 實現;及可以理解爲 Subject 是一個面門。
3、 對於 Subject 的構建一般沒必要我們去創建;一般通過 SecurityUtils.getSubject()獲取。