原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398

根据下载的pdf学习。

第六章 Realm及相关对象(二)

1.AuthenticationToken

由上篇可知,AuthenticationToken出现在UserRealm的方法doGetAuthenticationInfo()中。这个方法是用来验证的,token是验证时所用的参数。


protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token){}

AuthenticationToken是一个接口:

public interface AuthenticationToken extends Serializable {
Object getPrincipal(); //身份
Object getCredentials(); //凭据
}

常见的拓展接口和实现类有:

UsernamePasswordToken的示意代码如下:

所以要实现其他登录方式,比如是telephone/password时,就可以仿照UsernamePasswordToken,实现自己的token。在方法getCredentials()里返回telephone即可。

public class UsernamePasswordToken implements HostAuthenticationToken,RememberMeAuthenticationToken{
private java.lang.String username;
private char[] password;
private boolean rememberMe;
private String host; ...
public java.lang.Object getPrincipal() {
return username;
} public java.lang.Object getCredentials() {
return password;
}
}

2.AuthenticationInfo

由上篇可知,AuthenticationInfo出现在UserRealm的方法doGetAuthenticationInfo()中。是验证方法的返回值。

protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {//认证
...
//交给AuthenticatingRealm使用CredentialsMatcher进行密码匹配,如果觉得人家的不好可以自定义实现
SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(
user.getUsername(), //用户名
user.getPassword(), //密码
ByteSource.Util.bytes(user.getCredentialsSalt()),//salt=username+salt
getName() //realm name
);
return authenticationInfo;
}

AuthenticationInfo是一个接口:

public interface AuthenticationInfo extends Serializable {
PrincipalCollection getPrincipals();
Object getCredentials();
}

常见的拓展接口和实现类有:

SimpleAuthenticationInfo的示意代码如下:

 public class SimpleAuthenticationInfo implements MergableAuthenticationInfo, SaltedAuthenticationInfo {

     protected PrincipalCollection principals;//身份
protected Object credentials;//凭据
protected ByteSource credentialsSalt; public SimpleAuthenticationInfo(PrincipalCollection principals, Object credentials) {
this.principals = new SimplePrincipalCollection(principals);
this.credentials = credentials;
} public SimpleAuthenticationInfo(Object principal, Object credentials, String realmName) {
this.principals = new SimplePrincipalCollection(principal, realmName);
this.credentials = credentials;
} public SimpleAuthenticationInfo(Object principal, Object hashedCredentials, ByteSource credentialsSalt, String realmName) {
this.principals = new SimplePrincipalCollection(principal, realmName);
this.credentials = hashedCredentials;
this.credentialsSalt = credentialsSalt;
} .... }

3.PrincipalCollection

由上篇可知,PrincipalCollection出现在UserRealm的方法doGetAuthorizationInfo()中。这个方法是用来授权的,PrincipalCollection是授权时所用的参数。

protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String userTenant = (String) principals.getPrimaryPrincipal();
...
}

PrincipalCollection是一个接口:

要注意一个问题,因为可以在shiro中配置多个Realm,所以身份信息principal就可以有多个。因此采用PrincipalCollection进行聚合。

在大多数实现中,AuthenticationInfo会进行merge,比如SimpleAuthenticationInfo 会合并多个 Principal为一个 PrincipalCollection。

但是由于内部是Map实现的,所以方法getPrimaryPrincipal()可以看做是返回任意principal。因为map中没有顺序之分的。如果只有一个,那就是返回这一个。

1 public interface PrincipalCollection extends Iterable, Serializable {
2 ...
3 Object getPrimaryPrincipal();
4 }

常见的拓展接口和实现类有:

 4.AuthorizationInfo(授权信息)

由上篇可知,AuthenticationInfo出现在UserRealm的,授权方法doGetAuthorizationInfo()中。是该授权方法的返回值。
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
....
authorizationInfo.addStringPermission(permissionString);
....
return authorizationInfo;
}
AuthorizationInfo是一个接口:
public interface AuthorizationInfo extends Serializable {
Collection<String> getRoles();
Collection<String> getStringPermissions();
Collection<Permission> getObjectPermissions();
}
常见的拓展接口和实现类有:

SimpleAuthorizationInfo的示意代码如下:(getter和setter均省略)
public class SimpleAuthorizationInfo implements AuthorizationInfo {
protected Set<String> roles;
protected Set<String> stringPermissions;
protected Set<Permission> objectPermissions; public SimpleAuthorizationInfo() {
} public SimpleAuthorizationInfo(Set<String> roles) {
this.roles = roles;
} public void addRole(String role) {...}
public void addRoles(Collection<String> roles) {...} public void addStringPermission(String permission) {...}
public void addStringPermissions(Collection<String> permissions) {...} public void addObjectPermission(Permission permission) {...}
public void addObjectPermissions(Collection<Permission> permissions) {...} }

2017.2.7 开涛shiro教程-第六章-Realm及相关对象(二)的更多相关文章

  1. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(四)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(四) 1.Subject的代码结构 ...

  2. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(一)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象 1.用户.角色.权限的关系 用户和角 ...

  3. 2017.2.7 开涛shiro教程-第六章-Realm及相关对象(三)

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第六章 Realm及相关对象(三) 1.准备3个Realm MyR ...

  4. 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(一)服务器端

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 1.OAuth2介 ...

  5. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(二) controller

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第二十一章-授予身份与切换身份(二) 1.回顾 ...

  6. 2017.2.15 开涛shiro教程-第二十一章-授予身份与切换身份(一) table、entity、service、dao

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 第二十一章 授予身份与切换身份(一) 1.使用场景 某个领导因为某 ...

  7. 2017.2.12 开涛shiro教程-第七章-与Web集成

    2017.2.9 开涛shiro教程-第七章-与Web集成(一) 原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. ...

  8. 2017.2.16 开涛shiro教程-第十七章-OAuth2集成(二)客户端

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十七章-OAuth2集成 3.客户端 客户端 ...

  9. 2017.4.12 开涛shiro教程-第十八章-并发登录人数控制

    原博客地址:http://jinnianshilongnian.iteye.com/blog/2018398 根据下载的pdf学习. 开涛shiro教程-第十八章-并发登录人数控制 shiro中没有提 ...

随机推荐

  1. Leetcode 617.合并二叉树

    合并二叉树 给定两个二叉树,想象当你将它们中的一个覆盖到另一个上时,两个二叉树的一些节点便会重叠. 你需要将他们合并为一个新的二叉树.合并的规则是如果两个节点重叠,那么将他们的值相加作为节点合并后的新 ...

  2. TOJ 3750: 二分查找

    3750: 二分查找   Time Limit(Common/Java):3000MS/9000MS     Memory Limit:65536KByteTotal Submit: 1925     ...

  3. 利用js生成二维码

    $('#barcode').qrcode({ width: 300, height: 300, render: !!document.createElement('canvas').getContex ...

  4. hihoCoder offer 收割编程练习赛 83 C 播放列表

    题目 用 $1,2 ,3 \dots, N$ 代表 $N$ 首歌.设想有 $L$ 个格子排成一排,编号 $1$ 到 $L$ .考虑将这些数字挨个填进格子里的情形.假设当前要往第 $i$ 个格子里填一个 ...

  5. hosts修改备份

    # Modified hosts start # Armorgames Start 93.184.220.39 cache.armorgames.com 93.184.220.39 gamemedia ...

  6. Bzoj1195 [HNOI2006]最短母串 [状态压缩]

    Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 1304  Solved: 439 Description 给定n个字符串(S1,S2,„,Sn),要求找 ...

  7. Codevs 搜索刷题 集合篇

    2919 选择题 时间限制: 1 s 空间限制: 16000 KB 题目等级 : 黄金 Gold 题目描述 Description 某同学考试,在N*M的答题卡上写了A,B,C,D四种答案. 他做完了 ...

  8. 30+ Excellent Windows Phone 7 Development Tutorials

    原文发布时间为:2012-01-16 -- 来源于本人的百度文章 [由搬家工具导入] Here are 30+ cool Windows Phone Development articles for ...

  9. [MySQL] xtrabakcup原理

    Xtrabackup InnoDB内部的Redo log, 也叫Transaction log file. 存储每一个InnoDB表纪录的修改日志. 当InnoDB启动时, InnoDB会检查数据文件 ...

  10. [Oracle] Redo&Undo梳理

    Oracle Redo&undo Oracle中的redo和undo是关键技术的核心, 诸如实例恢复, 介质恢复, DataGuard, 闪回机制等都是给予redo和undo的, 所以很有必要 ...