接口的getInstance()方法是泛型方法,能夠用來建立SecurityManager接口對象html
T |
getInstance() Returns an instance of the required type. |
能夠保存的全部的認證數據信息程序員
實際上,大多數應用程序程序員不會常常與SecurityManager交互(若是有的話)。大多數應用程序程序員只關心當前執行的用戶的安全操做,一般經過調用SecurityUtils.getSubject()來實現。apache
|
setSecurityManager(SecurityManager securityManager) Sets a VM (static) singleton SecurityManager, specifically for transparent use in the getSubject() implementation. |
應用程序中,經過setSecurityManager把securityManager set進SecurityUtils 而後調用SecurityUtils.getSubject()方法拿到Subjectapi
|
getSubject() Returns the currently accessible Subject available to the calling code depending on runtime environment. |
Subject安全
A Subject
represents state and security operations for a single application user. These operations include authentication (login/logout), authorization (access control), and session access. It is Shiro's primary mechanism for single-user security functionality.session
經過SecurityUtils.getSubject()拿到Subject以後 能夠經過Subject接口進行登陸,受權,認證,等操做。(下面只摘錄了一個登陸的API接口)app
login(AuthenticationToken token) Performs a login attempt for this Subject/user. |
參數token: 身份驗證令牌是用戶在身份驗證嘗試期間提交的賬戶主體和支持憑證的整合。該令牌經過authenticate(token)方法提交給身份驗證者。而後身份驗證器執行身份驗證/登陸過程。
驗證令牌的常見實現包括用戶名/密碼對、X.509證書、PGP密鑰或您能想到的任何東西。令牌能夠是身份驗證器須要的任何東西,以便正確地進行身份驗證框架
下面提供一個簡單的DEMOui
package com.sun.test; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.UsernamePasswordToken; import org.apache.shiro.config.IniSecurityManagerFactory; import org.apache.shiro.subject.Subject; import org.apache.shiro.util.Factory; public class TestLoginDemo { public static void main(String[] args) { // 取得Factory接口對象,主要的目的是經過配置文件加載文件之中的信息,這些信息暫時不可以成爲認證信息 Factory<org.apache.shiro.mgt.SecurityManager> factory = new IniSecurityManagerFactory("classpath:shiro.ini"); // 取得裏面所保存的全部的認證數據信息 org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance(); // 利用一個專門的認證操做的處理類,實現認證處理的具體的實現 SecurityUtils.setSecurityManager(securityManager); // 獲取進行用戶名和密碼認證的接口對象 Subject subject = SecurityUtils.getSubject() ; // 定義了一個Token,裏面保存要登陸的用戶名和密碼信息 UsernamePasswordToken token = new UsernamePasswordToken("admin","hello") ; // 實現用戶登陸處理 subject.login(token); } }
shiro.ini配置this
[users]
admin=hello
上面的權限認證都是在一個配置文件完成的(shiro.ini),若是想實現不一樣用戶數據來源,而且統一這些來源的處理。準備了一個叫Realm的接口
Reaml實現類的受權接口
|
doGetAuthorizationInfo(PrincipalCollection principals) Retrieves the AuthorizationInfo for the given principals from the underlying data store. |
Reaml實現類的認證接口
|
doGetAuthenticationInfo(AuthenticationToken token) Retrieves authentication data from an implementation-specific datasource (RDBMS, LDAP, etc) for the given authentication token. |
介紹完Shiro基礎,下面咱們講一講Shiro在SSM框架的應用(使用Relam)