Shiro Review——自定义Realm实现授权
来源:互联网 发布:淘宝店铺不能注册 编辑:程序博客网 时间:2024/05/22 07:41
在自定义Realm中,可以进对数据库的查询,将认证后的用户的资源权限全部查询出来。
/** * 自定义Realm * @author LiuHuiChao * */public class CustomRealm extends AuthorizingRealm{@Overridepublic void setName(String name) {super.setName("customName");}/** * 认证方法 */@Overrideprotected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {//1,从token中取出用户身份信息String userCode=(String)token.getPrincipal();//2,根据用户输入的账号从数据库查询String password="111111";//模拟从库里查询到的密码//2--1,查询不到返回null//2--2,查询到返回AuthenticationInfoSimpleAuthenticationInfo simpleAuthenticationInfo=new SimpleAuthenticationInfo(userCode,password,this.getName());return simpleAuthenticationInfo;}/** * 授权方法 */@Overrideprotected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {//从principals获取主身份信息//将getPrimaryPrincipal方法返回值转换为真实身份类型,(在doGetAuthenticationInfo认证通过,填充到SimpleAuthenticationInfo中身份信息类型)String userCode=(String) principals.getPrimaryPrincipal();//根据身份信息,获取权限信息//连接数据库。。。//模拟从数据库获取到数据List<String> permissionList=new ArrayList<String>();permissionList.add("user:create");//用户的创建权限permissionList.add("items:add");//商品添加权限// and so on...//查到权限数据,返回授权信息(把上面查询到的数据填充)SimpleAuthorizationInfo authorizationInfo=new SimpleAuthorizationInfo();//将上面查询到的授权信息进行填充authorizationInfo.addStringPermissions(permissionList);return authorizationInfo;}}
ini配置:
[main]#自定义realmcustomRealm=cn.itcast.shiro.realm.CustomRealm#将realm设置到securityManagersecurityManager.realms=$customRealm
测试授权:
/** * 自定义realm进行测试 */@Testpublic void testAuthorizationWithCustomRealm(){//创建SecurityManager工厂Factory<SecurityManager> factory=new IniSecurityManagerFactory("classpath:shiro-realm.ini");//创建SecurityManagerSecurityManager securityManager=factory.getInstance();//将SecurityManager设置到系统运行环境,和spring整合后将SecurityManager配置到spring容器中SecurityUtils.setSecurityManager(securityManager);//创建subjectSubject subject=SecurityUtils.getSubject();//执行认证UsernamePasswordToken token=new UsernamePasswordToken("zhangsan","123");try {subject.login(token);} catch (AuthenticationException e) {e.printStackTrace();}System.out.println("认证状态:"+subject.isAuthenticated());/*基于资源的授权 * * 调用isPermitted方法会调用自定义realm来查询权限数据 * */boolean isPermitted=subject.isPermitted("user:create:1");System.out.println("是否有user:create权限:"+isPermitted);boolean isPremittedAll=subject.isPermittedAll("user:create","user:delete");System.out.println("是否有user:create,user:delete权限:"+isPermitted);//使用无返回值的checktry {subject.checkPermission("user:post");} catch (AuthorizationException e) {System.out.println("用户没有user:post权限");e.printStackTrace();}}
授权流程:
1、对subject进行授权,调用方法isPermitted("permission串")2、SecurityManager执行授权,通过ModularRealmAuthorizer执行授权3、ModularRealmAuthorizer执行realm(自定义的CustomRealm)从数据库查询权限数据调用realm的授权方法:doGetAuthorizationInfo 4、realm从数据库查询权限数据,返回ModularRealmAuthorizer5、ModularRealmAuthorizer调用PermissionResolver进行权限串比对6、如果比对后,isPermitted中"permission串"在realm查询到权限数据中,说明用户访问permission串有权限,否则 没有权限,抛出异常。
0 1
- Shiro Review——自定义Realm实现授权
- Shiro Review——自定义Realm实现认证
- Shiro 自定义realm授权与认证的实现
- shiro自定义Realm实现
- shiro授权,自定义realm实现授权,shiro与项目集成,在项目中实现认证及授权
- Shiro入门10:自定义Realm进行授权
- Shiro Review——使用ini文件进行授权测试
- Shiro 学习笔记(3)—— 自定义 Realm
- shiro-realm授权过虑器
- shiro自定义Realm
- Shiro入门-自定义realm
- Shiro自定义Realm
- Shiro 自定义realm认证
- 【shiro】--- 自定义realm
- Shiro(四) 自定义Realm
- 自定义认证授权Realm
- 自定义realm进行授权
- Shiro——Realm数据交互的实现
- 设计模式-单例模式
- <Sicily>Funny Game
- 掌握CoordinatorLayout
- 智慧源自发问,问题重于回答。
- 算法
- Shiro Review——自定义Realm实现授权
- 【mysql】数据库迁移方法
- 设计模式分类
- Android之GLES2.0显示立方体各面不同图片测试代码
- OpenCL、OpenGL和DirectX三者的区别
- 系统负载能力浅析
- Nexus 手动更新索引
- eclipse添加AndroidAnnotations
- 313. Super Ugly Number