在认证、授权内部实现机制中都有提到,最终处理都将交给Real进行处理。因为在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。通常情况下,在Realm中会直接从我们的数据源中获取Shiro需要的验证信息。可以说,Realm是专用于安全框架的DAO.
一、认证实现
正如前文所提到的,Shiro的认证过程最终会交由Realm执行,这时会调用Realm的getAuthenticationInfo(token)方法。
该方法主要执行以下操作:
1、检查提交的进行认证的令牌信息
2、根据令牌信息从数据源(通常为数据库)中获取用户信息
3、对用户信息进行匹配验证。
4、验证通过将返回一个封装了用户信息的AuthenticationInfo实例。
5、验证失败则抛出AuthenticationException异常信息。
而在我们的应用程序中要做的就是自定义一个Realm类,继承AuthorizingRealm抽象类,重载doGetAuthenticationInfo (),重写获取用户信息的方法。
protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
User user = accountManager.findUserByUserName(token.getUsername());
if (user != null) {
return new SimpleAuthenticationInfo(user.getUserName(), user.getPassword(), getName());
} else {
return null;
}
}
二、授权实现
而授权实现则与认证实现非常相似,在我们自定义的Realm中,重载doGetAuthorizationInfo()方法,重写获取用户权限的方法即可。
protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
String userName = (String) principals.fromRealm(getName()).iterator().next();
User user = accountManager.findUserByUserName(userName);
if (user != null) {
SimpleAuthorizationInfo info = new SimpleAuthorizationInfo();
for (Group group : user.getGroupList()) {
info.addStringPermissions(group.getPermissionList());
}
return info;
} else {
return null;
}
}
分享到:
相关推荐
Apache_Shiro_使用手册(四)Realm_实现
Apache Shiro中文版使用手册!
Apache Shiro使用手册 共22页.pdf
Apache Shiro使用手册.pdf 学习shiro 入门之用
Apache_Shiro_使用手册Apache_Shiro_使用手册Apache_Shiro_使用手册
Apache_Shiro_使用手册(一)Shiro架构介绍
Apache_Shiro参考手册中文版 Introduction to Apache Shiro What is Apache Shiro? Apache Shiro 是一个强大而灵活的开源安全框架,它干净利落地处理身份认证,授权,企业会话管理和加密。 Apache Shiro 的首要目标...
Shiro核心组件Aplication,Subject,SecurityManager, Realm, Authenticator, Authorizer. 介绍了Shiro认证,授权的实现及其机制原理。给了具体操作步骤及代码。
Apache Shiro 中文手册,想了解权限方面知识的小伙伴们下载吧。
Apache Shiro。 官网 Apache Shiro API。 Apache Shiro开发文档。
Apache Shiro 是一个框架,可用于身份验证和授权。Apache Shiro的教程(PDF),相关jar包,源码等。
Apache Shiro 开发使用手册,介绍了shiro的架构,认证,授权,实现,配置等,希望能够为大家使用shiro的时候带来帮助。
采用SpringBoot + Apache Shiro + Mybatis Plus + Thymeleaf 实现的内容管理系统(附带权限管理),是搭建博客、网站的不二之选。 技术栈:Spring Boot、Apache Shiro、MyBatis-Plus、Alibaba Druid、Redis、MySQL、...
因为在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。通常情况下,在Realm中会直接从我们的数据源中获取Shiro需要的验证信息。可以说,Realm是专用于安全框架的DAO. 一、认证实现 正如前文...
Apache_Shiro参考手册中文版,官方原版,pdf格式,方便查看,可复制!
Apache Shiro 的首要目标是易于使用和理解。安全有时候是很复杂的,甚至是痛苦的,但它没有必要这样。框架应 该尽可能掩盖复杂的地方,露出一个干净而直观的 API,来简化开发人员在使他们的应用程序安全上的努力。 ...
ApacheShiro使用手册和Apache Shiro Reference Documentation 该文件下载了别人上传的两个文件,一个10分,一个30分,为了自己以后下载不花那么多分,也不让一些没有多少分的朋友能够下载,所以将别人资源下载后,将...
apache shiro 实例 apache shiro 实例
apache-shiro中文开发技术手册,很实用