接口的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)