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

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):

Java代码  
  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)

Java代码  
  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)

Java代码  
  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配置分类:

Java代码  
  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及其依赖对象的配置。

创建对象

Java代码  
  1. securityManager=org.apache.shiro.mgt.DefaultSecurityManager

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

 

常量值setter注入

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

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

 

对象引用setter注入

Java代码  
  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注入

Java代码  
  1. securityManager.authenticator.authenticationStrategy=$authenticationStrategy

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

byte数组setter注入

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

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

Array/Set/List setter注入

Java代码  
  1. authenticator.array=1,2,3
  2. authenticator.set=$jdbcRealm,$jdbcRealm

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

Map setter注入

Java代码  
  1. authenticator.map=$jdbcRealm:$jdbcRealm,1:1,key:abc

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

实例化/注入顺序

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

后边的覆盖前边的注入。

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

[users]部分

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

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

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

[roles]部分

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

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

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

[urls]部分

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

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

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

Shiro学习(4)INI配置的更多相关文章

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

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

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

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

  3. shiro学习笔记(四) ini配置以及加解密

    INI配置 从之前的Shiro架构图可以看出,Shiro是从根对象SecurityManager进行身份验证和授权的:也就是所有操作都是自它开始的,这个对象是线程安全且真个应用只需要一个即可,因此Sh ...

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

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

  5. 第四章:shiro的INI配置

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

  6. shiro中INI配置

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

  7. Shiro ini配置

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

  8. Java安全框架 Apache Shiro学习-1-ini 配置

    简单登录流程: 1.  SecurityManager   2.  SecurityUtils.setSecurityManager 3.  SecurityUtils.getSubject     ...

  9. Apache Shiro 学习记录5

    本来这篇文章是想写从Factory加载ini配置到生成securityManager的过程的....但是貌似涉及的东西有点多...我学的又比较慢...很多类都来不及研究,我又怕等我后面的研究了前面的都 ...

随机推荐

  1. Java中"String.equals()“和"=="的区别

    Do NOT use the `==`` operator to test whether two strings are equal! It only determines whether or n ...

  2. 卸载Anaconda

    conda install anaconda-clean anaconda-clean --yes Anaconda 安装的时候在 .bash_profile中添加了变量: export PATH=& ...

  3. redis相关笔记(二.集群配置及使用)

    redis笔记一 redis笔记二 redis笔记三 1.配置:在原redis-sentinel文件夹中添加{8337,8338,8339,8340}文件夹,且复制原8333中的配置 在上述8333配 ...

  4. cocos2D-X call JNIHelper

    #ifndef _WIN32 JNIEnv *j = JniHelper::getEnv(); if (j == nullptr || j == NULL) {test += "JNIEnv ...

  5. 使用JAVA如何对图片进行格式检查以及安全检查处理

    一.通常情况下,验证一个文件是否图片,可以通过以下三种方式: 1).判断文件的扩展名是否是要求的图片扩展名 这种判断是用得比较多的一种方式,不过这种方式非常的不妥,别人稍微的把一个不是图片的文件的扩展 ...

  6. OC学习篇之---单例模式

    在之前的一片文章中介绍了对象的拷贝相关知识:http://blog.csdn.net/jiangwei0910410003/article/details/41926531,今天我们来看一下OC中的单 ...

  7. SCP-Py-002

    项目编号:Py-002 项目等级:EuclidKeter 特殊收容措施: Py-002-1目前被映射在Researcher Kevin的服务器位于Site-Pyproject地下防无线电渗透室且被切断 ...

  8. Database基础(一):构建MySQL服务器、 数据库基本管理 、MySQL 数据类型、表结构的调整

    一.构建MySQL服务器 目标: 本案例要求熟悉MySQL官方安装包的使用,快速构建一台数据库服务器: 安装MySQL-server.MySQl-client软件包 修改数据库用户root的密码 确认 ...

  9. kubernetes批量删除pod

    监控页面出现看到有运行失败的pod 1) 查看有哪些不运行的podcustom-metrics-apiserver日志占满空间被驱逐 [root@hadoop03 ~]# kubectl get po ...

  10. Source Insight下载及注册码

    下载地址:http://www.sourceinsight.com/down35.html 注册码: SI3US-205035-36448 SI3US-466908-65897 SI3US-36893 ...