Apache Shiro 使用手册(四)Realm 实现
2015-06-30 09:38
731 查看
在认证、授权内部实现机制中都有提到,最终处理都将交给Real进行处理。因为在Shiro中,最终是通过Realm来获取应用程序中的用户、角色及权限信息的。通常情况下,在Realm中会直接从我们的数据源中获取Shiro需要的验证信息。可以说,Realm是专用于安全框架的DAO.
一、认证实现
正如前文所提到的,Shiro的认证过程最终会交由Realm执行,这时会调用Realm的getAuthenticationInfo(token)方法。
该方法主要执行以下操作:
1、检查提交的进行认证的令牌信息
2、根据令牌信息从数据源(通常为数据库)中获取用户信息
3、对用户信息进行匹配验证。
4、验证通过将返回一个封装了用户信息的AuthenticationInfo实例。
5、验证失败则抛出AuthenticationException异常信息。
而在我们的应用程序中要做的就是自定义一个Realm类,继承AuthorizingRealm抽象类,重载doGetAuthenticationInfo (),重写获取用户信息的方法。
Java代码
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()方法,重写获取用户权限的方法即可。
原文:http://kdboy.iteye.com/blog/1169631
一、认证实现
正如前文所提到的,Shiro的认证过程最终会交由Realm执行,这时会调用Realm的getAuthenticationInfo(token)方法。
该方法主要执行以下操作:
1、检查提交的进行认证的令牌信息
2、根据令牌信息从数据源(通常为数据库)中获取用户信息
3、对用户信息进行匹配验证。
4、验证通过将返回一个封装了用户信息的AuthenticationInfo实例。
5、验证失败则抛出AuthenticationException异常信息。
而在我们的应用程序中要做的就是自定义一个Realm类,继承AuthorizingRealm抽象类,重载doGetAuthenticationInfo (),重写获取用户信息的方法。
Java代码
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; } }
原文:http://kdboy.iteye.com/blog/1169631
相关文章推荐
- Apache Shiro 使用手册(三)Shiro 授权
- Apache Shiro 使用手册(二)Shiro 认证
- Apache Shiro 使用手册(一)Shiro架构介绍
- 关于在局域网中访问Apache服务器的防火墙设置
- Linux+php在apache上以fastcgi模式运行
- Apache与ngnix的对比
- Apache Maven 入门篇 ( 上 )
- Apache的配置优化
- Apache Qpid Broker的安全机制
- Mac OS 10.10.3下Apache + mod_wsgi配置【一】
- svn更新报svn org.apache.subversion.javahl.ClientException,svn: Unable to connect to a repository at URL
- Apache自动将%20转换为空格
- apache 禁止爬虫
- org.apache.poi读取后缀为.xls和.xlsx的excel文件
- java.lang.SecurityException:Servlet of class org.apache.catalina.servlets.InvokerServlet is privileged
- org.apache.jasper.JasperException:An exception occurred processing JSPpage/control/RuZhuControl.jsp
- Apache虚拟主机设置
- apache kafka系列之server.properties配置文件参数说明
- Apache wsgi Flask配置细节
- apachetop 实时监测web服务器运行状况