Apache Shiro 使用手册(四)Realm 实现
2015-06-11 14:39
513 查看
Apache Shiro 使用手册(四)Realm 实现
博客分类:开发
安全框架Shiro
在认证、授权内部实现机制中都有提到,最终处理都将交给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()方法,重写获取用户权限的方法即可。
Java代码
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;
}
}
17
顶
2
踩
分享到:
Apache Shiro 使用手册(五)Shiro 配置说
... | Apache Shiro 使用手册(三)Shiro 授权
2011-09-09 22:03
浏览 24208
评论(5)
收藏
分类:开源软件
相关推荐
评论
5 楼 jayyunfei 2014-10-08 引用
cnaaaa 写道
大吓: 我情况是这样的
@Autowired(required = true)
private UserMapper userMapper;
/**
* 认证回调函数, 登录时调用.
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
User user = userMapper.findUserByLoginName(token.getUsername());
其中 的 userMapper是用mybatis生成的userMapper
但在doGetAuthenticationInfo引用 的时候 为null 在其他控制器里是正常的,是什么问题???
有没有注入,在配置文件中,这个shiro应该有个配置文件的,你把这个usermapper注入到你重写的类中在配置文件中配置,应该就可以了。
4 楼 cnaaaa 2014-09-30 引用
大吓: 我情况是这样的
@Autowired(required = true)
private UserMapper userMapper;
/**
* 认证回调函数, 登录时调用.
*/
@Override
protected AuthenticationInfo doGetAuthenticationInfo(
AuthenticationToken authcToken) throws AuthenticationException {
UsernamePasswordToken token = (UsernamePasswordToken) authcToken;
User user = userMapper.findUserByLoginName(token.getUsername());
其中 的 userMapper是用mybatis生成的userMapper
但在doGetAuthenticationInfo引用 的时候 为null 在其他控制器里是正常的,是什么问题???
3 楼 jayyunfei 2013-12-13 引用
我是改变 写道
在applicationContext-shiro.xml:
<!-- 項目自定义的Realm, 所有accountService依赖的dao都需要用depends-on声明 -->
<bean id="shiroDbRealm" class="com.inmabeidou.inma.service.account.ShiroDbRealm" depends-on="userDao, uscaDao">
<property name="userService" ref="userService"/>
</bean>
谢谢,虽然问题已经解决,还是谢谢。
2 楼 我是改变 2013-12-11 引用
在applicationContext-shiro.xml:
<!-- 項目自定义的Realm, 所有accountService依赖的dao都需要用depends-on声明 -->
<bean id="shiroDbRealm" class="com.inmabeidou.inma.service.account.ShiroDbRealm" depends-on="userDao, uscaDao">
<property name="userService" ref="userService"/>
</bean>
1 楼 jayyunfei 2013-07-05 引用
请教一个问题:
我在重写这个doGetAuthenticationInfo方法的时候我把userServiceImpl注入了自定的Realm类为什么调用不了其中的方法,总是不能执行,但是这些方法在其他类中就能执行。
相关文章推荐
- Apache Shiro 使用手册(二)Shiro 认证
- Preventing Web Attacks with Apache
- 双倍提升Apache Spark排序性能
- maven执行update命令时报org/apache/maven/shared/filtering/MavenFilteringException错误
- Apache Shiro(四)Realm 实现
- Apache Shiro(三)Shiro 授权
- Apache Shiro(二)Shiro 认证
- org.apache.subversion.javahl.ClientException: svn: E155021: This client is too old to work with the
- 配置fail2ban对Apache服务器进行安全防护的方法
- debian 配置LAMP
- 在Apache服务器上安装SSL支持的教程
- Intra-cluster Replication in Apache Kafka
- ERROR org.apache.hadoop.hdfs.server.datanode.DataNode: Incompatible namespaceIDs
- Apache Qpid Broker云
- 安装xampp后apache不能启动解决方法
- win7旗舰版(64位)配置apache2.4.12(64位)+PHP5.6.9(64位) 的PHP学习环境
- apache2.4 虚拟主机配置
- Apache2.4的简介和安装
- Apache Hadoop 和Hadoop生态圈
- Apache your port 80 is not actually used