Shiro常用的类
————————首先是配置shiro的几个类
1.LifecycleBeanPostProcessor 这个类是shiro的生命周期bean
2.DefaultAdvisorAutoProxyCreator 代理对象 主要为shiro配置代理
3.CredentialsMatcher 凭证匹配器所用到的类(一般我们用自己实现的凭证匹配器)
4.AuthRealm 安全域 也用自己实现的域
5.DefaultWebSecurityManager shiro的核心 安全管理器
6.AuthorizationAttributeSourceAdvisor shiro的权限管理器
7.ShiroFilterFactoryBean shiro的过滤器链
8.ShiroDialect 这个只在SpringBoot中配置标签用
————————其次是几个重要的API 我们一一过一遍
1.Subject 主体接口 先从他的api中看一下
Object getPrincipal();
PrincipalCollection getPrincipals(); //凭证集合
boolean isPermitted(String var1); //是否被允许
boolean isPermitted(Permission var1);
void checkPermission(String var1) throws AuthorizationException;
void checkPermission(Permission var1) throws AuthorizationException;
boolean hasRole(String var1); //是否有**权限
boolean isAuthenticated();
boolean isRemembered();
Session getSession(); //获取Session
Session getSession(boolean var1);
void logout(); //登出
void login(AuthenticationToken var1) throws AuthenticationException; //登录
等等
可以看出来 subject就是我们操作的入口,subject中可以看到shiro的基本几个功能,比如权限 和认证 还有session 和 remember me ……等等。!!!注意了重要的方法是login(token)方法 这个方法就是验证的入口。hasRole、hasPermission验证目前用户是否有角色或权限。
2.SecurityUtils 安全管理器工具类 从当前线程中获取主体Subject 具体代码如下:
private static SecurityManager securityManager; //安全管理器
public SecurityUtils() {
}
/**
从当前的线程中获取主体
**/
public static Subject getSubject() {
Subject subject = ThreadContext.getSubject();
if(subject == null) {
subject = (new Builder()).buildSubject();
ThreadContext.bind(subject);
}
return subject;
}
3.AuthenticationToken 和UsernamePasswordToken UsernamePasswordToken 是AuthenticationToken 的子类,我们先看一下AuthenticationToken 的源代码:
Object getPrincipal(); //身份
Object getCredentials(); //凭证
没错 ,所谓Token就是用户通过Web页面获取到的username和password 当然你也可以指定别的为Token的身份和凭证,不过没人那么干。
再看一下UsernamePassword的API:
private String username; //用户名
private char[] password; //密码
private boolean rememberMe; //是否记住密码
private String host; //主机
这个就比较算是我们常用的一种Token 因为比较符合我们的习惯 有用户名和密码 ,这里为什么密码是char[]类型而不是String类型呢?这是因为:String在Java中是不可变对象,这就意味着一旦创建了一个字符串,如果另一个进程把尝试内存的数据导出(dump),在GC进行垃圾回收之前该字符串会一直保留在内存中,那么该进程就可以轻易的读取到该字符串。
4.SimpleCredentialsMatcher 简单凭证匹配器,此类为了实现将用户输入的Token和真实的info 进行凭证的对比 ,底层调用了equals方法 一样,则能证明密码正确,否则登录失败呀。 看一下他的API:
/**
获取用户输入的token 获取用户正确的info
**/
protected Object getCredentials(AuthenticationToken token) {
return token.getCredentials();
}
protected Object getCredentials(AuthenticationInfo info) {
return info.getCredentials();
}
//进行验证 进行匹配
public boolean doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) {
Object tokenCredentials = this.getCredentials(token);
Object accountCredentials = this.getCredentials(info);
return this.equals(tokenCredentials, accountCredentials);
}
//还有一个equals方法 我没有写 主要是比较
一般我们都自定义一个凭证比较器 我的叫做AuthCredentialsMatcher去继承SimpleCredentialsMatcher
实现他的doCredentialsMatch(AuthenticationToken token, AuthenticationInfo info) 在这个方法中可以加入自己的一些业务判断 ….具体看自己了 这个是login验证的最底层了。
5.PrincipalCollection 接口 主要的实现类SimplePrincipalCollection 作用之一是存放 Principal 这个大家用该都不陌生了 用户的身份 可以是用户名/手机号/邮箱地址
6.AuthenticationInfo 用户身份验证类 主要实现类是 SimpleAuthenticationInfo 这个类相当重要 是实现我们用户验证机制的。他有三个属性
protected PrincipalCollection principals; 用户的身份集合
protected Object credentials; 用户的凭证 (密码)
protected ByteSource credentialsSalt; 凭证加密的盐
从数据库中查出用户的真实正确的信息就是要在此进行封装成SimpleAuthenticationInfo (身份,凭证,realm名)对象,才能去凭证匹配器中进行比较。
7.AuthorizationInfo 用户权限认证类 主要的实现类是SimpleAuthorizationInfo,我们一般用这个实现类 先看下AuthorizationInfo 的API :
Collection<String> getRoles();
Collection<String> getStringPermissions();
Collection<Permission> getObjectPermissions();
没错就是你登录成功之后,获取你的所有权限和所有角色信息。
SimpleAuthorizationInfo的API:
//看起来也是大同小异 主要是CURD你的权限
protected Set<String> roles;
protected Set<String> stringPermissions;
protected Set<Permission> objectPermissions;