4.1 根对象SecurityManager

从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的;也就是所有操作都是自它开始的,这个对象是线程安全且真个应用只需要一个即可,因此Shiro提供了SecurityUtils让我们绑定它为全局的,方便后续操作。

因为Shiro的类都是POJO的,因此都很容易放到任何IoC容器管理。但是和一般的IoC容器的区别在于,Shiro从根对象securityManager开始导航;Shiro支持的依赖注入:public空参构造器对象的创建、setter依赖注入。

1、纯Java代码写法(com.github.zhangkaitao.shiro.chapter4.NonConfigurationCreateTest):

  1. DefaultSecurityManager securityManager = new DefaultSecurityManager();
  2. //设置authenticator
  3. ModularRealmAuthenticator authenticator = new ModularRealmAuthenticator();
  4. authenticator.setAuthenticationStrategy(new AtLeastOneSuccessfulStrategy());
  5. securityManager.setAuthenticator(authenticator);
  6. //设置authorizer
  7. ModularRealmAuthorizer authorizer = new ModularRealmAuthorizer();
  8. authorizer.setPermissionResolver(new WildcardPermissionResolver());
  9. securityManager.setAuthorizer(authorizer);
  10. //设置Realm
  11. DruidDataSource ds = new DruidDataSource();
  12. ds.setDriverClassName("com.mysql.jdbc.Driver");
  13. ds.setUrl("jdbc:mysql://localhost:3306/shiro");
  14. ds.setUsername("root");
  15. ds.setPassword("");
  16. JdbcRealm jdbcRealm = new JdbcRealm();
  17. jdbcRealm.setDataSource(ds);
  18. jdbcRealm.setPermissionsLookupEnabled(true);
  19. securityManager.setRealms(Arrays.asList((Realm) jdbcRealm));
  20. //将SecurityManager设置到SecurityUtils 方便全局使用
  21. SecurityUtils.setSecurityManager(securityManager);
  22. Subject subject = SecurityUtils.getSubject();
  23. UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
  24. subject.login(token);
  25. Assert.assertTrue(subject.isAuthenticated());

2.1、等价的INI配置(shiro-config.ini)

  1. [main]
  2. #authenticator
  3. authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
  4. authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
  5. authenticator.authenticationStrategy=$authenticationStrategy
  6. securityManager.authenticator=$authenticator
  7. #authorizer
  8. authorizer=org.apache.shiro.authz.ModularRealmAuthorizer
  9. permissionResolver=org.apache.shiro.authz.permission.WildcardPermissionResolver
  10. authorizer.permissionResolver=$permissionResolver
  11. securityManager.authorizer=$authorizer
  12. #realm
  13. dataSource=com.alibaba.druid.pool.DruidDataSource
  14. dataSource.driverClassName=com.mysql.jdbc.Driver
  15. dataSource.url=jdbc:mysql://localhost:3306/shiro
  16. dataSource.username=root
  17. #dataSource.password=
  18. jdbcRealm=org.apache.shiro.realm.jdbc.JdbcRealm
  19. jdbcRealm.dataSource=$dataSource
  20. jdbcRealm.permissionsLookupEnabled=true
  21. securityManager.realms=$jdbcRealm

即使没接触过IoC容器的知识,如上配置也是很容易理解的:

1、对象名=全限定类名  相对于调用public无参构造器创建对象

2、对象名.属性名=值    相当于调用setter方法设置常量值

3、对象名.属性名=$对象引用    相当于调用setter方法设置对象引用

2.2、Java代码(com.github.zhangkaitao.shiro.chapter4.ConfigurationCreateTest)

  1. Factory<org.apache.shiro.mgt.SecurityManager> factory =
  2. new IniSecurityManagerFactory("classpath:shiro-config.ini");
  3. org.apache.shiro.mgt.SecurityManager securityManager = factory.getInstance();
  4. //将SecurityManager设置到SecurityUtils 方便全局使用
  5. SecurityUtils.setSecurityManager(securityManager);
  6. Subject subject = SecurityUtils.getSubject();
  7. UsernamePasswordToken token = new UsernamePasswordToken("zhang", "123");
  8. subject.login(token);
  9. Assert.assertTrue(subject.isAuthenticated());

如上代码是从Shiro INI配置中获取相应的securityManager实例:

1、默认情况先创建一个名字为securityManager,类型为org.apache.shiro.mgt.DefaultSecurityManager的默认的SecurityManager,如果想自定义,只需要在ini配置文件中指定“securityManager=SecurityManager实现类”即可,名字必须为securityManager,它是起始的根;

2、IniSecurityManagerFactory是创建securityManager的工厂,其需要一个ini配置文件路径,其支持“classpath:”(类路径)、“file:”(文件系统)、“url:”(网络)三种路径格式,默认是文件系统;

3、接着获取SecuriyManager实例,后续步骤和之前的一样。

从如上可以看出Shiro INI配置方式本身提供了一个简单的IoC/DI机制方便在配置文件配置,但是是从securityManager这个根对象开始导航。

4.2 INI配置

ini配置文件类似于Java中的properties(key=value),不过提供了将key/value分类的特性,key是每个部分不重复即可,而不是整个配置文件。如下是INI配置分类:

  1. [main]
  2. #提供了对根对象securityManager及其依赖的配置
  3. securityManager=org.apache.shiro.mgt.DefaultSecurityManager
  4. …………
  5. securityManager.realms=$jdbcRealm
  6. [users]
  7. #提供了对用户/密码及其角色的配置,用户名=密码,角色1,角色2
  8. username=password,role1,role2
  9. [roles]
  10. #提供了角色及权限之间关系的配置,角色=权限1,权限2
  11. role1=permission1,permission2
  12. [urls]
  13. #用于web,提供了对web url拦截相关的配置,url=拦截器[参数],拦截器
  14. /index.html = anon
  15. /admin/** = authc, roles[admin], perms["permission1"]

[main]部分

提供了对根对象securityManager及其依赖对象的配置。

创建对象

  1. securityManager=org.apache.shiro.mgt.DefaultSecurityManager

其构造器必须是public空参构造器,通过反射创建相应的实例。

 

常量值setter注入

  1. dataSource.driverClassName=com.mysql.jdbc.Driver
  2. jdbcRealm.permissionsLookupEnabled=true

会自动调用jdbcRealm.setPermissionsLookupEnabled(true),对于这种常量值会自动类型转换。

 

对象引用setter注入

  1. authenticator=org.apache.shiro.authc.pam.ModularRealmAuthenticator
  2. authenticationStrategy=org.apache.shiro.authc.pam.AtLeastOneSuccessfulStrategy
  3. authenticator.authenticationStrategy=$authenticationStrategy
  4. securityManager.authenticator=$authenticator

会自动通过securityManager.setAuthenticator(authenticator)注入引用依赖。

嵌套属性setter注入

  1. securityManager.authenticator.authenticationStrategy=$authenticationStrategy

也支持这种嵌套方式的setter注入。

byte数组setter注入

  1. #base64 byte[]
  2. authenticator.bytes=aGVsbG8=
  3. #hex byte[]
  4. authenticator.bytes=0x68656c6c6f

默认需要使用Base64进行编码,也可以使用0x十六进制。

Array/Set/List setter注入

  1. authenticator.array=1,2,3
  2. authenticator.set=$jdbcRealm,$jdbcRealm

多个之间通过“,”分割。

Map setter注入

  1. authenticator.map=$jdbcRealm:$jdbcRealm,1:1,key:abc

即格式是:map=key:value,key:value,可以注入常量及引用值,常量的话都看作字符串(即使有泛型也不会自动造型)。

实例化/注入顺序

  1. realm=Realm1
  2. realm=Realm12
  3. authenticator.bytes=aGVsbG8=
  4. authenticator.bytes=0x68656c6c6f

后边的覆盖前边的注入。

测试用例请参考配置文件shiro-config-main.ini。

[users]部分

配置用户名/密码及其角色,格式:“用户名=密码,角色1,角色2”,角色部分可省略。如:

  1. [users]
  2. zhang=123,role1,role2
  3. wang=123

密码一般生成其摘要/加密存储,后续章节介绍。

[roles]部分

配置角色及权限之间的关系,格式:“角色=权限1,权限2”;如:

  1. [roles]
  2. role1=user:create,user:update
  3. role2=*

如果只有角色没有对应的权限,可以不配roles,具体规则请参考授权章节。

[urls]部分

配置url及相应的拦截器之间的关系,格式:“url=拦截器[参数],拦截器[参数],如:

  1. [urls]
  2. /admin/** = authc, roles[admin], perms["permission1"]

具体规则参见web相关章节。

shiro中INI配置的更多相关文章

  1. Apache shiro集群实现 (二) shiro 的INI配置

    Apache shiro集群实现 (一) shiro入门介绍 Apache shiro集群实现 (二) shiro 的INI配置 Apache shiro集群实现 (三)shiro身份认证(Shiro ...

  2. 第四章:shiro的INI配置

    4.1 根对象SecurityManager 从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全 ...

  3. shiro中anon配置不生效

    再配置shiro的时候,如下代码要注意: 1.下述代码中必须是LinkedHashMap 而不能是HashMap. 2.anon定义必须在authc之前 否则anon定义不生效   @Bean     ...

  4. Shiro中Realm

    6.1 Realm [2.5 Realm]及[3.5 Authorizer]部分都已经详细介绍过Realm了,接下来再来看一下一般真实环境下的Realm如何实现. 1.定义实体及关系   即用户-角色 ...

  5. 跟开涛老师学shiro -- INI配置

    之前章节我们已经接触过一些INI配置规则了,如果大家使用过如spring之类的IoC/DI容器的话,Shiro提供的INI配置也是非常类似的,即可以理解为是一个IoC/DI容器,但是区别在于它从一个根 ...

  6. shiro.ini 配置详解

    引用: [1]开涛的<跟我学shiro> [2]<SpringMVC整合Shiro> [3]<shiro简单配置> [4]Apache shiro集群实现 (一) ...

  7. Shiro ini配置

    Shiro.ini配置: ini配置文件类似Java中的properties(key = value),不过提供了key/value分类的特性,每个部分的key不重复即可 在eclipse中设置打开方 ...

  8. 第四章 INI配置——《跟我学Shiro》

    转发地址:https://www.iteye.com/blog/jinnianshilongnian-2020820 第四章 INI配置——<跟我学Shiro> 博客分类: 跟我学Shir ...

  9. Shiro学习(4)INI配置

    之前章节我们已经接触过一些INI配置规则了,如果大家使用过如spring之类的IoC/DI容器的话,Shiro提供的INI配置也是非常类似的,即可以理解为是一个IoC/DI容器,但是区别在于它从一个根 ...

随机推荐

  1. 集合01_List

    List集合总览 元素有序,可重复,可通过索引访问 增加了通过索引操作集合的方法,如: Object get(int index) Object remove(int index) void sort ...

  2. Codeforces 855C. Helga Hufflepuff's Cup----树形DP

    z最近在学习树形DP...好难啊. 在cf上找到了一题c题当模版马克一下. 题目不贴了..>>http://codeforces.com/problemset/problem/855/C& ...

  3. 在 2016 年学 JavaScript 是一种什么样的体验?(React从入门到放弃)

    jquery 年代 vs 前端模块化 http://blog.csdn.net/offbye/article/details/52793921 ++ 嘿,我最近接到一个 Web 项目,不过老实说,我这 ...

  4. 在C#中理解和实现策略模式的绝对入门教程

    介绍 本文的目的是理解战略模式的基础知识,并试图了解何时可以使用,并有一个基本的实现,以便更好地理解.在现实世界的应用中,这是无法实施战略模式的,所采用的例子也远没有实际可行.这篇文章的想法只是为了说 ...

  5. android studio 的基本使用和建立一个小项目

    https://github.com/allenxieyusheng/Android-Studio

  6. 树莓派 无屏幕 安装Ubuntu系统 无头安装 无显示器 用网线

    能看到此篇博客的人说明都尝试失败了,会发现内存卡刷入Ubuntu后,无法通过ssh操作树莓派.是因为官方的ubuntu系统在初次运行时需要设定一些东西,类似windows第一次启动也需要设置那样,如果 ...

  7. hdu 6170 Two strings dp

    Two strings Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Prob ...

  8. Easyui使用心得(1)--DateGrid表格

    最近一直在用easyui这个控件,有一点心得,在这里和大家分享一下,也是对自己工作的一个小小的总结,希望可以形成一个完整的Easyui的笔记体系,可以方便更多的人 因为自己也是在摸索中前进,难免有遗漏 ...

  9. Hadoop 学习路线

    大数据实时计算工程师/Hadoop工程师/数据分析师职业路线图 描述 本路线图是一个专门针对大数据实时处理.Hadoop工程师和数据分析师所设计的课程体系介绍,在实时计算方向主要包括了从数据收集框架. ...

  10. linux 环境下如何完全卸载postgres

    完全删除postgres 小笔记: 1.查看版本号和系统类别:cat  /etc/redhat-realease; 2.如果是redhat:(yum install) a.yum 删除软件包: yum ...